0
votes

I'm working on implementing the camera into my app. Everything works well except for one thing, when rotating the display from landscape to reversed landscape, the surfaceview is not updated (same when rotated from reversed landscape to normal landscape). surfaceChanged is not called, therefor, the preview shows upside down. If I rotate the display from portrait to either landscape or reversed landscape it works perfectly.

I have tried to catch this using onConfigurationChange in my CameraActivity (which extends ActionBarActivity), but it is not called in the situation where orientation is changed from normal landscape to reversed landscape (or the other way around). I have learned that this is because no config actually changes in this situation. The orientation is still landscape and the screen size doesn't change, thus, onConfigurationChange is not called...

Next, I decided to try and accomplish this using an OrientationEventListener, but I can't seem to access the camera from within this listener:

mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {

        @Override
        public void onOrientationChanged(int orientation) {

            // determine our orientation based on sensor response
            int lastOrientation = mOrientation;

            DisplayMetrics displaymetrics = new DisplayMetrics();
            Display display = getWindowManager().getDefaultDisplay();
            int rotation = display.getRotation();
            getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);

            int height = displaymetrics.heightPixels;
            int width = displaymetrics.widthPixels;

            if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) {
                if (mCamera == null)
                    mCamera = Camera.open(CameraInfo.CAMERA_FACING_BACK);

                int angleToRotate = getRoatationAngle();
                mCamera.setDisplayOrientation(angleToRotate);
                initPreview(width, height);
                startPreview();
            }
        }
    };

I keep getting a NullPointerException because mCamera is null whenever onOrientationChanged gets called. I have checked for this null as seen in my code above, but when the call to Camera.open() is invoked, my app crashes. The NullPointerException occurs when trying to set mCamera inside onOrientationChanged using Camera.open().

I've also tried just calling surfaceChanged from within the OrientationEventListener, and the exact same issue occurs. Only the NullPointerException happens when I try to set the null mCamera variable inside surfaceChanged.

Does anyone have an idea how to resolve this issue?

1

1 Answers

0
votes

I have decided to implement this in a different way. Instead of allowing the activity and preview orientation to change, they are now both locked. The activity is locked in portrait using AndroidManifest.xml and the preview is locked in normal landscape mode and as the phone is rotated, I use the OrientationEventListener to determine the angle of rotation and adjust the output image and the rotation of the shutter button so it feels like there is an orientation change, but in fact the activity and preview stay in the same orientation.

This works flawlessly and stops me from having to implement unnecessary code. This method also removes the delay in orientation change while your activity is reloaded since the activity no longer goes through an orientation change and it does not call onCreate().