diff options
Diffstat (limited to 'system/RuntimePermissionsBasic/Application/src/main/java/com/example/android/basicpermissions/camera/CameraPreview.java')
-rw-r--r-- | system/RuntimePermissionsBasic/Application/src/main/java/com/example/android/basicpermissions/camera/CameraPreview.java | 84 |
1 files changed, 45 insertions, 39 deletions
diff --git a/system/RuntimePermissionsBasic/Application/src/main/java/com/example/android/basicpermissions/camera/CameraPreview.java b/system/RuntimePermissionsBasic/Application/src/main/java/com/example/android/basicpermissions/camera/CameraPreview.java index 7abf2d8d..db8eacf6 100644 --- a/system/RuntimePermissionsBasic/Application/src/main/java/com/example/android/basicpermissions/camera/CameraPreview.java +++ b/system/RuntimePermissionsBasic/Application/src/main/java/com/example/android/basicpermissions/camera/CameraPreview.java @@ -18,6 +18,7 @@ package com.example.android.basicpermissions.camera; import android.content.Context; import android.hardware.Camera; +import android.util.AttributeSet; import android.util.Log; import android.view.Surface; import android.view.SurfaceHolder; @@ -27,7 +28,7 @@ import java.io.IOException; /** * Camera preview that displays a {@link Camera}. - * + * <p> * Handles basic lifecycle methods to display and stop the preview. * <p> * Implementation is based directly on the documentation at @@ -36,14 +37,19 @@ import java.io.IOException; public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { private static final String TAG = "CameraPreview"; + private SurfaceHolder mHolder; private Camera mCamera; private Camera.CameraInfo mCameraInfo; private int mDisplayOrientation; - public CameraPreview(Context context, Camera camera, Camera.CameraInfo cameraInfo, - int displayOrientation) { - super(context); + public CameraPreview(Context context, AttributeSet attrs, int defStyleAttr) { + this(context, attrs, defStyleAttr, null, null, 0); + } + + public CameraPreview(Context context, AttributeSet attrs, int defStyleAttr, + Camera camera, Camera.CameraInfo cameraInfo, int displayOrientation) { + super(context, attrs, defStyleAttr); // Do not initialise if no camera has been set if (camera == null || cameraInfo == null) { @@ -59,6 +65,41 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback mHolder.addCallback(this); } + /** + * Calculate the correct orientation for a {@link Camera} preview that is displayed on screen. + * <p> + * Implementation is based on the sample code provided in + * {@link Camera#setDisplayOrientation(int)}. + */ + public static int calculatePreviewOrientation(Camera.CameraInfo info, int rotation) { + 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; + } + + int result; + if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { + result = (info.orientation + degrees) % 360; + result = (360 - result) % 360; // compensate the mirror + } else { // back-facing + result = (info.orientation - degrees + 360) % 360; + } + + return result; + } + public void surfaceCreated(SurfaceHolder holder) { // The Surface has been created, now tell the camera where to draw the preview. try { @@ -104,39 +145,4 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback Log.d(TAG, "Error starting camera preview: " + e.getMessage()); } } - - /** - * Calculate the correct orientation for a {@link Camera} preview that is displayed on screen. - * - * Implementation is based on the sample code provided in - * {@link Camera#setDisplayOrientation(int)}. - */ - public static int calculatePreviewOrientation(Camera.CameraInfo info, int rotation) { - 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; - } - - int result; - if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { - result = (info.orientation + degrees) % 360; - result = (360 - result) % 360; // compensate the mirror - } else { // back-facing - result = (info.orientation - degrees + 360) % 360; - } - - return result; - } } |