3

Basically i create a simple android camera app not adding video recording , i followed this link android camera app tutorial link

the problem is when i capture image from front camera , after click capture button the image showing mirrored just like we stand front of mirror. e.g i have right side arrow image but when i capture, it preview left side arrow.Sorry for English.

Mr Umar
  • 41
  • 1
  • 3

3 Answers3

8

When you display the bitmap, you may use scale attributes in imageView

android:scaleX="-1" //To flip horizontally or
android:scaleY="-1" //To flip verticallyenter code here

You may also want to look into this post and this tutorial

Update 1: You can flip the image in preview in a similar manner. You could also create a helper function to flip the bitmap and then set it to the imageView.

Helper function to flip the image could be found here. For your code you may just need the following:

public static Bitmap flip(Bitmap src, int type) {
            // create new matrix for transformation
            Matrix matrix = new Matrix();

            matrix.preScale(-1.0f, 1.0f);

            // return transformed image
            return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
        }

and then in your previewCaptureImage method, instead of this imgPreview.setImageBitmap(bitmap); , use the flip method and then set it to imageView like this

imgPreview.setImageBitmap(flip(bitmap));
Community
  • 1
  • 1
skbrhmn
  • 1,124
  • 1
  • 14
  • 36
  • when i capture image , image show with two buttons retake and ok . At that screen the image also mirror at that screen do you know how to flip this too. I hope you got my point. – Mr Umar Jan 27 '17 at 12:49
  • Thanks, I have edited my answer and added some more specific code. See if this works. Also, if an answer or comment helps you, please upvote it or select is as the correct answer. – skbrhmn Jan 27 '17 at 17:16
  • is there any better solution , cause byte[] array's length can be large like 70000, and the above function takes 10-20 seconds to filp image. – Ankush Shrivastava Oct 22 '20 at 19:22
8
val outputOptions = ImageCapture
        .OutputFileOptions
        .Builder(photoFile)
        .setMetadata(ImageCapture.Metadata().also {
            it.isReversedHorizontal = CameraSelector.LENS_FACING_FRONT == lensFacing
        }).build()
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Hevre Lee
  • 81
  • 1
  • 1
0

you need to pass metadata by isReversedHorizontal to true with the outputOptions as of you are trying to capture an image through the front camera and front camera always in mirror form.

val metadata = ImageCapture.Metadata().apply {
                    isReversedHorizontal = true
                }
                val outputOptions = ImageCapture.OutputFileOptions.Builder(photoFile)
                    .setMetadata(metadata)
                    .build()

                imageCapture.takePicture(
                    outputOptions, cameraExecutor, object : ImageCapture.OnImageSavedCallback {
                        override fun onError(exc: ImageCaptureException) {
                            Log.e(TAG, "Photo capture failed: ${exc.message}", exc)
                        }

                        override fun onImageSaved(output: ImageCapture.OutputFileResults) {
                            val savedUri = output.savedUri ?: Uri.fromFile(photoFile)
                            Log.d(TAG, "Photo capture succeeded: $savedUri")
                        }
                    })
Nikul Vaghani
  • 21
  • 1
  • 1