0

I know there are already questions like this, but none solved it. I took some help and figured out basic code that I need to change the camera with respect to the display.

   cameraInfo=new Camera.CameraInfo();
                camera.getCameraInfo(camId,cameraInfo);
                int angle=0;
                int rotation = getActivity().getWindowManager().getDefaultDisplay()
                        .getRotation();
                switch(rotation)
                {
                    case Surface.ROTATION_0:
                        parameters.setPreviewSize(s.width, s.height);
                        Log.i(TAGR, "THE ROATTION IS 0");
                        camera.setDisplayOrientation(90);
                        break;
                    case Surface.ROTATION_90:
                        parameters.setPreviewSize(s.width, s.height);
                        Log.i(TAGR, "THE ROATTION IS 90");
                        camera.setDisplayOrientation(0);

                        break;
                    case Surface.ROTATION_180:
                        parameters.setPreviewSize(s.width, s.height);
                        Log.i(TAGR, "THE ROATTION IS 180");
                        camera.setDisplayOrientation(0);

                        break;
                    case Surface.ROTATION_270:
                        parameters.setPreviewSize(s.width, s.height);
                        Log.i(TAGR, "THE ROATTION IS 270");
                        camera.setDisplayOrientation(180);
                        break;
                    default:
                        camera.setDisplayOrientation(0);
                        break;
                }

            }

However, I am unable to get the correct display preview with respect to the rotation of the device. I have tried several different combination of angles. However, none gave me the correct output. I am attaching the images of my preview. Please explain the solution to the problem. Moreover, I want to know what position of my device corresponds to angles 0,90,180, and 270. In other words, is 0 corresponds to portrait or landscape orientation?.

Images : The landscape is inverted in this one. I tried setting the rotation by 180 when ROTATION_180. Doing so did not work out, as the other orientation (in landscape) became inverted The landscape in this orientation is inverted

The upside down orientation does not do anything. The desired result is to bring the click button down with correct preview. I don't have any idea why this is not working. I have tried several values in each rotation, but none solved this issue.

This is the upside down orientation of my phone

I am a beginner, so please explain in detail. Thanks.

WOW
  • 99
  • 1
  • 10

2 Answers2

1

You should check CameraInfo.orientation for the device. Note that this property gives rotation from the natural display orientation, given for the device by Display.getRotation().

If your custom camera app uses android.hardware.camera2 API, this compensation will happen automatically.

If your custom camera activity does not lock the screen orientation, then it will be subject to "reverse landscape" glitch, and needs special treatment which involves OrientationEventListener.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • That worked flawlessly for the inverted portion. However, the upside down portion is still having the same problem. I tried debugging and changing the values the preview. It is changing when I put different values. So, I think the preview is fine. For Instance, I put 90 when the rotation was 270. As a result, my upside down orientation along with my landscape orientation ( corresponds to 180 degrees ) is inverted. I don't understand what is wrong with this orientation. Can you explain please? Thanks a lot for helping me out – WOW Nov 09 '15 at 21:28
  • Does your include `android:screenOrientation="fullSensor"` in **AndroidManifest.mk**? – Alex Cohn Nov 10 '15 at 07:54
  • 1
    Ha Ha! just did that and it worked like a charm. I had this "full Sensor" previously, but when I checked again it was not there. Thank you very much for helping me out! – WOW Nov 10 '15 at 16:22
0

You can rotate the image like this before preview:

  public static Bitmap rotateImage(Bitmap bitmapSrc) {
    Matrix matrix = new Matrix();
    matrix.postRotate(previewRotation);
    return Bitmap.createBitmap(bitmapSrc, 0, 0,
            bitmapSrc.getWidth(), bitmapSrc.getHeight(), matrix, true);
}

"previewRotation" can be got from:

    private static void setCameraDisplayOrientation(Context mContext, android.hardware.Camera.CameraInfo info) {
    int rotation = ((CameraActivity) mContext).getWindowManager().getDefaultDisplay()
            .getRotation();
    int degrees = 0;
    switch (rotation) {
        case Surface.ROTATION_0:
            degrees = 0;
            break;
        case Surface.ROTATION_90:
            degrees = 90;
            break;
        case Surface.ROTATION_180:
            degrees = 180;
            break;
        case Surface.ROTATION_270:
            degrees = 270;
            break;
    }

    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        previewRotation = (info.orientation + degrees) % 360;
        previewRotation = (360 - previewRotation) % 360;  // compensate the mirror
    } else {  // back-facing
        previewRotation = (info.orientation - degrees + 360) % 360;
    }
    mCameraInstance.setDisplayOrientation(previewRotation);
}
android_eng
  • 1,370
  • 3
  • 17
  • 40