Implementing OrientationEventListener to troubleshoot camera issues without CameraInfo?

I need to implement OrientationEventListenerfor the camera to work correctly. Google has posted an example implementation onOrientationChangedthat looks like this:

@Override
    public void onOrientationChanged(int orientation) {
        if (orientation == ORIENTATION_UNKNOWN) return;
     android.hardware.Camera.CameraInfo info =
            new android.hardware.Camera.CameraInfo();
     android.hardware.Camera.getCameraInfo(cameraId, info);
     orientation = (orientation + 45) / 90 * 90;
     int rotation = 0;
     if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
         rotation = (info.orientation - orientation + 360) % 360;
     } else {  // back-facing camera
         rotation = (info.orientation + orientation) % 360;
     }
     mParameters.setRotation(rotation);
    }

But I'm building against API level 8, so I don't CameraInfo. How can I accomplish a similar thing as above without CameraInfo?

+4
source share
2 answers

Although the orientation is changing, I believe that I also need to change the camera preview to fit if you want full-screen images when rotating, so I use a similar method, but in the onLayout () method for the camera surface, like this:

/*
 * This class provides the surface for the camera.
 */
public class CameraSurfaceView extends ViewGroup implements SurfaceHolder.Callback
{

@Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom)
    {
        if (changed)
        {               

            final int width = right - left;
            final int height = bottom - top;

            int previewWidth = width;
            int previewHeight = height;
            if (mPreviewSize != null)
            {
                Display display = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

                switch (display.getRotation())
                {
                    case Surface.ROTATION_0:
                        previewWidth = mPreviewSize.height;
                        previewHeight = mPreviewSize.width;
                        mCamera.setDisplayOrientation(90);
                        break;
                    case Surface.ROTATION_90:
                        previewWidth = mPreviewSize.width;
                        previewHeight = mPreviewSize.height;
                        break;
                    case Surface.ROTATION_180:
                        previewWidth = mPreviewSize.height;
                        previewHeight = mPreviewSize.width;
                        break;
                    case Surface.ROTATION_270:
                        previewWidth = mPreviewSize.width;
                        previewHeight = mPreviewSize.height;
                        mCamera.setDisplayOrientation(180);
                        break;
                }                                    
            }

            final int scaledChildHeight = previewHeight * width / previewWidth;

            cameraView.layout(0, height - scaledChildHeight, width, height);

        }
    }

}

HTC Desire Android API 8 (Android 2.2).

+2
public void onOrientationChanged(int orientation) {
   if (orientation == ORIENTATION_UNKNOWN) return;

   int rotation = activity.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;
   }

   parameters.setRotation(degrees );
   mcamera.setParameters(parameters);
}

, .

: , , . . 2.2.

: 2.2. onOrientationChanged : Camera src

+1

All Articles