我的代码中旋转在我的应用程序中捕获的景观图像的正确点是什么?



我没有太多与相机和文件一起工作的经验。我已经集成了Camerakit的库来捕获图像,这是我当前的代码:

captureButton.setOnClickListener {
            cameraKitView.captureImage() { _, p1 ->
                val timeStamp = System.currentTimeMillis().toString()
                val fileName = "Dere$timeStamp.jpg"
                val path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + File.separator + "Dere"
                val outputDir = File(path)
                outputDir.mkdir()
                val savedPhoto = File(path + File.separator + fileName)

                try {
                    val outputStream = FileOutputStream(savedPhoto.path)
                    outputStream.write(p1)
                    outputStream.close()
                    mActivity.sendBroadcast(
                        Intent(
                            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                            Uri.fromFile(savedPhoto)
                        )
                    )

                    // Here I'm already loading the image into an mage view for the user to apprve the photo
                    Glide.with(mActivity).load(savedPhoto)
                                .into(mActivity.photoEditorFragment.view!!.photo_editor_image)

                    // at this point I save this photo with some extra details that were collected to the local room database
                    val localImagePost = LocalImagePost(
                        timeStamp.toLong(),
                        location.longitude,
                        location.latitude,
                        savedPhoto.path,
                        "",
                        "",
                        true
                    ) 

                    localImageViewModel.insert(localImagePost)
                    sharedViewModelLocalImagePost.sharedImagePostObject.postValue(localImagePost)

                } catch (e: java.io.IOException) {
                    e.printStackTrace()
                }
            }
}

p1是一个旁观。我之前在这里问了一个问题,如果我将活动的方向限制在我的清单中,我可以在应用程序中拍摄的照片的方向吗?但是我不知道如何以及在我的代码中使用它。我是否会从刚刚创建的第一个文件中创建一个全新的文件,然后删除第一个文件?还是我只是从创建旋转的文件开始?我有点迷路了,很感激任何帮助,谢谢!

  //SOLUTION 1->
  //find the Orientation Using ExifInterface
   try{
        File imageFile = new File(imagePath);
        ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
        int orientation = exif.getAttributeInt(
                            ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL);
 //From the orientation value you can convert(Rotate) image according to orientation and can be set it to the ImageView later
         int rotate = 0;
         switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                 rotate = 270;
                 break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                 rotate = 180;
                 break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                 rotate = 90;
                 break;
          }
        }
   catch (Exception e) {
   }
  // Image rotation //
  Matrix matrix = new Matrix();
  matrix.postRotate(orientation);
  Bitmap cropped = Bitmap.createBitmap(scaled, x, y, width, height, matrix, true);  

// SOLUTION 2
ExifInterface oldExif = new ExifInterface(oldImagePath);
String exifOrientation = oldExif.getAttribute(ExifInterface.TAG_ORIENTATION);
if (exifOrientation != null) {
   ExifInterface newExif = new ExifInterface(imagePath);
  newExif.setAttribute(ExifInterface.TAG_ORIENTATION, exifOrientation);
  newExif.saveAttributes();
}

最新更新