react-native-vision-camera icon indicating copy to clipboard operation
react-native-vision-camera copied to clipboard

fix: add rotation when flipping an image

Open jphilipstevens opened this issue 3 years ago • 5 comments

Based on SO: https://stackoverflow.com/a/28657257/12270852

What I noticed is that the image is rotated. This worked on my pixel 2 XL. I am not sure exactly why the rotation is needed. any help would be appreciated

What

jphilipstevens avatar Jun 25 '21 16:06 jphilipstevens

Hey! Thanks for your PR. I'm currently not at home so I can't test the change, but it doesn't really look right to me to force-rotate it by 90 degrees as the images currently look correct on my Phone. With your PR they might look correct on yours, but will be incorrect on mine. I see two solutions:

  • Fully implement rotation screen rotation, there might be some adjustments needed in configureSession()
  • Create a bug report at the CameraX google issue tracker, might be a bug specifically for the Pixel 2

mrousavy avatar Jun 26 '21 07:06 mrousavy

Hey! Thanks for your PR. I'm currently not at home so I can't test the change, but it doesn't really look right to me to force-rotate it by 90 degrees as the images currently look correct on my Phone. With your PR they might look correct on yours, but will be incorrect on mine. I see two solutions:

  • Fully implement rotation screen rotation, there might be some adjustments needed in configureSession()
  • Create a bug report at the CameraX google issue tracker, might be a bug specifically for the Pixel 2

Yup, I agree. I didn't quite know what was going on here with why the rotation was needed. I wanted to kick this or off to start hammering out a solution. I was looking at Androids docs and implemented their Orientation Listener but what I notice was orientation was 0.

My team will test out on a few devices and I'm going to try and carve out time to dig into this.

jphilipstevens avatar Jun 26 '21 15:06 jphilipstevens

So Something i noticed while testing:

Test case: use front-facing camera, do not run fun flipImage Result: Image is mirrored as expected

Test Case: Results: Image is rotated 90 degrees. Below is what I changed for testing:

fun flipImage(imageBytes: ByteArray): ByteArray {
  val bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
  val matrix = Matrix()
  // What if we just save the image back? without any transformations?
  // matrix.preScale(-1f, 1f)
  val newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
  val stream = ByteArrayOutputStream()
  newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)
  return stream.toByteArray()
}

I am not really up to speed on Android development yet, but this seems weird. Since the Constructor for a matrix creates an identity matrix i would expect the image to not be changed

jphilipstevens avatar Jun 28 '21 10:06 jphilipstevens

Hi @jphilipstevens even I'm facing same issue on Pixel and other devices which run core android captured image is rotating. Have you found a fix for that ??

coolvasanth avatar Sep 14 '21 17:09 coolvasanth

@coolvasanth the above fix seems to work but I got here with trial and error. I have not had the chance to go deep into the fix yet.

jphilipstevens avatar Oct 01 '21 16:10 jphilipstevens

Hey, thanks for the PR but I think this has been overridden because of the Camera2 rewrite! There is a new issue to fix orientation, maybe this PR can be relevant there: #1891

mrousavy avatar Sep 30 '23 13:09 mrousavy