Search This Blog

Support my cause by downloading

Thursday, 15 September 2016

Stop front facing camera from mirroring (inverting) image programmatically?

While taking picture from front camera, its shows preview like a mirror. Your left ear will be shown as right in camera preview. Referhow to take picture from Camera and SurfaceView for camera basics.

If you pic example from snapchat, they simply show picture same as like camera preview was showing. That feature looks good. User want the exact picture which one’s seeing in preview, if it translate it can completely change image/picture look.
When you capture bitmap from byte array, you can translate it back to what was showing in camera preview. Look at code below –

Matrix matrix = new Matrix();
if (face) { // reverse flipping of image should only need to be handle for front facing camera
    matrix.preScale(1.0f, -1.0f);
}

Apply matrix to bitmap and create a new one –
Bitmap new_bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), 
bitmap.getHeight(), matrix, true); 
// bitmap is old bitmap which we created from byte array of camera stream


See how to reverse flip video taken from front facing camera. 


Android News and source code