1

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!

Tsabary
  • 3,119
  • 2
  • 24
  • 66

1 Answers1

0
  //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();
}
Arwy Shelke
  • 333
  • 1
  • 2
  • 12
  • 1
    Thank you for your answer Arwy but it is the same as the one you gave to my previous question which I've attached here. My problem is that I am not sure where and how to use the code in your answer. Ive tried but couldn't get it to work. Looking at the code I've attached here, are you able to help me figure it out? – Tsabary May 07 '19 at 10:22