Android, front and back camera Orientation , Landscape
Asked Answered
Q

3

10

In my camera app, you can switch between the front and back camera. When I take picture with the back camera the picture is the same like the preview shows. But when I switch to the front camera, the picture is mirrorrd.I think it has something to do that front and back camera is in landscape mode. I have tried almost every given answers on SO.

It would really help if someone could point me in the right directions.

Quinary answered 30/11, 2011 at 20:10 Comment(0)
Q
20

I found the answer , doing mCamera.setDisplayOrientationa(int degrees); did not help. I had to mirror the generated bitmap in order to get the result I wanted. I used the Matrix method to a achieve this .

float[] mirrorY = { -1, 0, 0, 0, 1, 0, 0, 0, 1};
Matrix matrix = new Matrix();
Matrix matrixMirrorY = new Matrix();
matrixMirrorY.setValues(mirrorY);

matrix.postConcat(matrixMirrorY);

image = Bitmap.createBitmap(mBitmap, 0, 0, frame.getWidth(), frame.getHeight(), matrix, true)
Quinary answered 1/12, 2011 at 20:40 Comment(3)
Interestingly, I had to rotate around z axis. If anyone knows how to do this with camera2 API please answer.Lop
@RushiMThakker Sorry m8, bin out of the Android development scene for quite a while now.Quinary
@MaikelBollemeijer np m8, I solved it. Your answer was helpful. Thanks.Lop
H
4

The issue with front camera was figured out to be specific to android 4.0+. So after you decoded the byte-array in method "onPictureTaken"

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

    Bitmap photo = BitmapFactory.decodeByteArray(data,0,data.length);
    photo = rotateBitmap(photo);  //.....do some stuff     }

just call rotateBitmap to rotate the bitmap

    private Bitmap rotateBitmap(Bitmap bitmap){

    Matrix rotateRight = new Matrix();
    rotateRight.preRotate(90);

    if(android.os.Build.VERSION.SDK_INT > 13 && CameraActivity.frontCamera)
    {
        float[] mirrorY = { -1, 0, 0, 0, 1, 0, 0, 0, 1};
        rotateRight = new Matrix();
        Matrix matrixMirrorY = new Matrix();
        matrixMirrorY.setValues(mirrorY);

        rotateRight.postConcat(matrixMirrorY);

        rotateRight.preRotate(270);
    }

    final Bitmap rImg= Bitmap.createBitmap(bitmap, 0, 0,
            bitmap.getWidth(), bitmap.getHeight(), rotateRight, true);
    return rImg;
}
Homothermal answered 22/6, 2015 at 10:47 Comment(2)
How is your answer different from the already provided accepted answer ?Quinary
This is much more readable and understandable for beginners. This is why I added this full answer. In above answer is not clearly said where to do this. Besides in above answer is missing method "preRotate()".Homothermal
C
0

I think you are looking for setDisplayOrientation(int). There is a function they have that might help on the dev site:

http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation%28int%29

Chemurgy answered 30/11, 2011 at 20:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.