Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mirror the front facing camera in Android

Tags:

android

camera

When you take a picture with the front facing camera in Android the preview is reflected along the Y axis to make the image seen appear as if the user was looking in the mirror. I want to undo this effect (apply a second reflection) or just stop the one thats done automatically.

I though to use this:

Camera mCamera;
....
mCamera.setPreviewCallback(...);

But I dont really know what to do with the overriding of

onPreviewFrame(byte[] data, Camera camera){...}

Whats the best way I can achieve what I've described?

Note I am trying to apply this effect to the live preview, not images that are already taken.

like image 723
Eduardo Avatar asked Nov 16 '13 22:11

Eduardo


People also ask

How do I make my front camera mirrored?

Open the Settings app and tap Camera. Turn on the switch for Mirror Front Camera or Mirror Front Photos.


2 Answers

First when you open your camera instance with Camera.open() you should open front camera with Camera.open(getSpecialFacingCamera())

private int getSpecialFacingCamera() {
    int cameraId = -1;
    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(i, info);
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            cameraId = i;
            break;
        }
    }
    return cameraId;
}

Then in your callback method where camera data is converting in to image you can use this code to keep it normal

public void onPictureTaken(byte[] data, Camera camera){
            Bitmap newImage = null;
            Bitmap cameraBitmap;
            if (data != null) {
                cameraBitmap = BitmapFactory.decodeByteArray(data, 0, (data != null) ? data.length : 0);
                if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                    // use matrix to reverse image data and keep it normal
                    Matrix mtx = new Matrix();
                    //this will prevent mirror effect
                    mtx.preScale(-1.0f, 1.0f);
                    // Setting post rotate to 90 because image will be possibly in landscape
                    mtx.postRotate(90.f);
                    // Rotating Bitmap , create real image that we want
                    newImage = Bitmap.createBitmap(cameraBitmap, 0, 0, cameraBitmap.getWidth(), cameraBitmap.getHeight(), mtx, true);
                }else{// LANDSCAPE MODE
                    //No need to reverse width and height
                    newImage = Bitmap.createScaledBitmap(cameraBitmap, screenWidth, screenHeight, true);
                    cameraBitmap = newImage;
                }
            }
        }

you can pass newImage in canvas and create jpeg image and save it on device. Do not forgot Camera is deprecated in Api level 21...

like image 124
Stoycho Andreev Avatar answered Sep 22 '22 14:09

Stoycho Andreev


You can use Matrix to flip the image data, something like:

byte[] baImage = null;
Size size = camera.getParameters().getPreviewSize();
ByteArrayOutputStream os = new ByteArrayOutputStream();
YuvImage yuv = new YuvImage(data, ImageFormat.NV21, size.width, size.height, null);
yuv.compressToJpeg(new Rect(0, 0, size.width, size.height), 100, os);
baImage = os.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length);
Matrix matrix = new Matrix(); 
matrix.preScale(-1.0f, 1.0f); 
Bitmap mirroredBitmap = Bitmap.createBitmap(bitmap, 0, 0, size.width, size.height, matrix, false);
like image 40
vitriolix Avatar answered Sep 22 '22 14:09

vitriolix