2

How to show mirror image on both front and back facing camera mode.

I know that this bitmap can be mirrored like below

BitmapDrawable flip(BitmapDrawable d)
{
    Matrix m = new Matrix();
    m.preScale(-1, 1);
    Bitmap src = d.getBitmap();
    Bitmap dst = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, false);
    dst.setDensity(DisplayMetrics.DENSITY_DEFAULT);
    return new BitmapDrawable(dst);
}

But how to use it to implement mirroring for camera X

Vikas Acharya
  • 3,550
  • 4
  • 19
  • 52
  • What are you trying to mirror exactly? The preview stream? The image capture result? – Husayn Hakeem Nov 25 '20 at 17:24
  • @HusaynHakeem both preview & img capt I need to flip I mean mirror. If it's flipped it should preview & capture flipped image else it should take normal image as it is. – Vikas Acharya Nov 26 '20 at 03:07
  • 2
    Mirroring the preview can be tricky if you're using `PreviewView`, it can potentially be done by forcing it to use a `TextureView`, getting access to it and updating its transformation matrix to include horizontal mirroring. I also wonder if just mirroring it would work by using `View.setScaleX(-1)`. Both approaches are hacks though. For ImageCapture, if you're passing output file options to the `takePicture()` method, you can use [`setReversedHorizontal(true)`](https://developer.android.com/reference/androidx/camera/core/ImageCapture.Metadata#setReversedHorizontal(boolean)). – Husayn Hakeem Nov 26 '20 at 04:21
  • @HusaynHakeem ```val outputOptions = ImageCapture.OutputFileOptions.Builder(photoFile) var metaData = ImageCapture.Metadata() metaData.isReversedVertical = true outputOptions.setMetadata(metaData).build()``` I tried this but not working – Vikas Acharya Nov 26 '20 at 06:06
  • 1
    @HusaynHakeem `viewFinder.scaleX = -1f` also didn't worked – Vikas Acharya Nov 26 '20 at 06:17
  • For image capture, you used the wrong method, you should use `setReversedHorizontal()` instead of `setReversedVertical()`. – Husayn Hakeem Nov 26 '20 at 17:48
  • @HusaynHakeem In kotlin `setReversedHorizontal()` property syntax is `isReversedHorizontal` and it's not working – Vikas Acharya Nov 27 '20 at 06:46
  • 2
    PreviewView#setScaleX(-1) should work. Vikas, are you using PreviewView? Do you mind sharing your code? – Xi 张熹 Dec 01 '20 at 23:44
  • @Xi张熹 Alas I'm on the same boat and `viewFinder.scaleX = -1f` doesn't do anything. Where exactly should we call it? I'm trying before using the Builder for creating the preview use case. – rdxdkr Jan 05 '21 at 00:26
  • @HusaynHakeem In case `scaleX = -1f` isn't going to work, can you post the code for mirroring the View like you suggested above? Why `TextureView` and not `SurfaceView`? – rdxdkr Jan 05 '21 at 00:35
  • @Xi 张熹 I changed my requirement itself because of this issue. Now, I don't have that code. Kindly check whether this will work if you are building any related projects. And do confirm if you don't mind. This feature would be very great if it's working fine. – Vikas Acharya Jan 06 '21 at 07:34
  • @Xi张熹 is it possible to do hflip with videoCapture now? I think currently we can hflip imageCapture. Temp solution for videoCapture maybe using external library such as ffmpeg. – K.Sopheak Mar 06 '23 at 08:44
  • 1
    @K.Sopheak Currently you will have to do it with OpenGL. But soon we are adding a "mirroring" feature to VideoCapture to take care of this. Please stay tuned. – Xi 张熹 Mar 06 '23 at 15:05

0 Answers0