amrit singh amrit singh - 10 months ago 48
Java Question

How to mirror a rear camera preview?

I was trying to get the preview of the rear camera mirrored... I mean the preview should mirror images vertically; it should swap right-to-left like the front camera...
I don't need the normal preview i need th image like if it's seen from the front camera and i'm trying the SurfaceTexture.
Is there someone who knows anything about it?
Any type of help is appreciated!

Answer Source

This is what I did. I converted the array of pixels to a Bitmap and flipped it. I noticed that I also had to rotate my image. If you need to do that you can do so with the rotateBitmap method below. I rotated it before flipping it. Since my application uses both cameras, I first do a check to see if it is using the front facing camera.

public Bitmap flip(Bitmap d)
    Matrix m = new Matrix();
    m.preScale(-1, 1);
    Bitmap dst = Bitmap.createBitmap(d, 0, 0, d.getWidth(), d.getHeight(), m, false);
    return dst;

public Bitmap rotateBitmap(Bitmap source, float angle)
    //Rotates Bitmap
    Matrix matrix = new Matrix();
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);

So it would look like this.

if(orienation == Orienation.SELFIE)
    bMap = rotateBitmap(bMap, 270);
    bMap = flip(bMap);

Those are just some custom fields I used in my application. I think you get the idea and can cater it however you need.