What is the right point in my code to rotate an landscape image captured in my app?

人走茶凉 提交于 2019-12-11 07:37:17

问题


I don't have much experience working with the camera and files in general. I've integrated CameraKit's library to capture images, and this is my current code:

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 is a ByteArray. I've asked a question before here Can I get the orientation of a photo taken in my app if I limit the activity's orientation to portrait only in my manifest? but I can't figure out how and where to use it in my code. Do I create a brand new file FROM the first file I just created, and then delete the first one? Or do I just start off by creating the rotated file? I'm a bit lost, would appreciate any help, thanks!


回答1:


  //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();
}


来源:https://stackoverflow.com/questions/56020041/what-is-the-right-point-in-my-code-to-rotate-an-landscape-image-captured-in-my-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!