diff --git a/library/src/main/camera2/com/google/android/cameraview/Camera2.java b/library/src/main/camera2/com/google/android/cameraview/Camera2.java index 4edb560b..d6c987d2 100644 --- a/library/src/main/camera2/com/google/android/cameraview/Camera2.java +++ b/library/src/main/camera2/com/google/android/cameraview/Camera2.java @@ -59,59 +59,64 @@ class Camera2 extends CameraViewImpl { } private final CameraManager mCameraManager; - private final ImageReader.OnImageAvailableListener mOnImageAvailableListener - = new ImageReader.OnImageAvailableListener() { + + private final TextureView.SurfaceTextureListener mSurfaceTextureListener + = new TextureView.SurfaceTextureListener() { @Override - public void onImageAvailable(ImageReader reader) { - try (Image image = reader.acquireNextImage()) { - Image.Plane[] planes = image.getPlanes(); - if (planes.length > 0) { - ByteBuffer buffer = planes[0].getBuffer(); - byte[] data = new byte[buffer.remaining()]; - buffer.get(data); - mCallback.onPictureTaken(data); - } - } + public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { + mSurfaceInfo.configure(surface, width, height); + configureTransform(); + startCaptureSession(); + } + + @Override + public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { + mSurfaceInfo.configure(surface, width, height); + configureTransform(); + startCaptureSession(); + } + + @Override + public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { + mSurfaceInfo.configure(null, 0, 0); + return true; + } + + @Override + public void onSurfaceTextureUpdated(SurfaceTexture surface) { } }; - private final SurfaceInfo mSurfaceInfo = new SurfaceInfo(); - private final SizeMap mPreviewSizes = new SizeMap(); - private final SizeMap mPictureSizes = new SizeMap(); - private String mCameraId; - private CameraCharacteristics mCameraCharacteristics; - private CameraDevice mCamera; - private CameraCaptureSession mCaptureSession; - private CaptureRequest.Builder mPreviewRequestBuilder; - private ImageReader mImageReader; - private int mFacing; - private AspectRatio mAspectRatio = Constants.DEFAULT_ASPECT_RATIO; - private boolean mAutoFocus; - private int mFlash; - private int mDisplayOrientation; - private PictureCaptureCallback mCaptureCallback = new PictureCaptureCallback() { + + private final CameraDevice.StateCallback mCameraDeviceCallback + = new CameraDevice.StateCallback() { @Override - public void onPrecaptureRequired() { - mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, - CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START); - setState(STATE_PRECAPTURE); - try { - mCaptureSession.capture(mPreviewRequestBuilder.build(), this, null); - mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, - CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_IDLE); - } catch (CameraAccessException e) { - Log.e(TAG, "Failed to run precapture sequence.", e); - } + public void onOpened(@NonNull CameraDevice camera) { + mCamera = camera; + mCallback.onCameraOpened(); + startCaptureSession(); } @Override - public void onReady() { - captureStillPicture(); + public void onClosed(@NonNull CameraDevice camera) { + mCallback.onCameraClosed(); + } + + @Override + public void onDisconnected(@NonNull CameraDevice camera) { + mCamera = null; + } + + @Override + public void onError(@NonNull CameraDevice camera, int error) { + Log.e(TAG, "onError: " + camera.getId() + " (" + error + ")"); + mCamera = null; } }; + private final CameraCaptureSession.StateCallback mSessionCallback = new CameraCaptureSession.StateCallback() { @@ -142,61 +147,76 @@ public void onClosed(@NonNull CameraCaptureSession session) { } }; - private final TextureView.SurfaceTextureListener mSurfaceTextureListener - = new TextureView.SurfaceTextureListener() { - @Override - public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { - mSurfaceInfo.configure(surface, width, height); - configureTransform(); - startCaptureSession(); - } + private PictureCaptureCallback mCaptureCallback = new PictureCaptureCallback() { @Override - public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { - mSurfaceInfo.configure(surface, width, height); - configureTransform(); - startCaptureSession(); + public void onPrecaptureRequired() { + mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, + CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START); + setState(STATE_PRECAPTURE); + try { + mCaptureSession.capture(mPreviewRequestBuilder.build(), this, null); + mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, + CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_IDLE); + } catch (CameraAccessException e) { + Log.e(TAG, "Failed to run precapture sequence.", e); + } } @Override - public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { - mSurfaceInfo.configure(null, 0, 0); - return true; + public void onReady() { + captureStillPicture(); } + }; + + private final ImageReader.OnImageAvailableListener mOnImageAvailableListener + = new ImageReader.OnImageAvailableListener() { + @Override - public void onSurfaceTextureUpdated(SurfaceTexture surface) { + public void onImageAvailable(ImageReader reader) { + try (Image image = reader.acquireNextImage()) { + Image.Plane[] planes = image.getPlanes(); + if (planes.length > 0) { + ByteBuffer buffer = planes[0].getBuffer(); + byte[] data = new byte[buffer.remaining()]; + buffer.get(data); + mCallback.onPictureTaken(data); + } + } } }; - private final CameraDevice.StateCallback mCameraDeviceCallback - = new CameraDevice.StateCallback() { - @Override - public void onOpened(@NonNull CameraDevice camera) { - mCamera = camera; - mCallback.onCameraOpened(); - startCaptureSession(); - } - @Override - public void onClosed(@NonNull CameraDevice camera) { - mCallback.onCameraClosed(); - } + private String mCameraId; - @Override - public void onDisconnected(@NonNull CameraDevice camera) { - mCamera = null; - } + private CameraCharacteristics mCameraCharacteristics; - @Override - public void onError(@NonNull CameraDevice camera, int error) { - Log.e(TAG, "onError: " + camera.getId() + " (" + error + ")"); - mCamera = null; - } + private CameraDevice mCamera; - }; + private CameraCaptureSession mCaptureSession; + + private CaptureRequest.Builder mPreviewRequestBuilder; + + private ImageReader mImageReader; + + private final SurfaceInfo mSurfaceInfo = new SurfaceInfo(); + + private final SizeMap mPreviewSizes = new SizeMap(); + + private final SizeMap mPictureSizes = new SizeMap(); + + private int mFacing; + + private AspectRatio mAspectRatio = Constants.DEFAULT_ASPECT_RATIO; + + private boolean mAutoFocus; + + private int mFlash; + + private int mDisplayOrientation; public Camera2(Callback callback, Context context) { super(callback); @@ -237,11 +257,6 @@ boolean isCameraOpened() { return mCamera != null; } - @Override - int getFacing() { - return mFacing; - } - @Override void setFacing(int facing) { if (mFacing == facing) { @@ -255,13 +270,13 @@ void setFacing(int facing) { } @Override - Set getSupportedAspectRatios() { - return mPreviewSizes.ratios(); + int getFacing() { + return mFacing; } @Override - AspectRatio getAspectRatio() { - return mAspectRatio; + Set getSupportedAspectRatios() { + return mPreviewSizes.ratios(); } @Override @@ -279,8 +294,8 @@ void setAspectRatio(AspectRatio ratio) { } @Override - boolean getAutoFocus() { - return mAutoFocus; + AspectRatio getAspectRatio() { + return mAspectRatio; } @Override @@ -303,8 +318,8 @@ void setAutoFocus(boolean autoFocus) { } @Override - int getFlash() { - return mFlash; + boolean getAutoFocus() { + return mAutoFocus; } @Override @@ -327,6 +342,11 @@ void setFlash(int flash) { } } + @Override + int getFlash() { + return mFlash; + } + @Override void takePicture() { if (mAutoFocus) { @@ -344,7 +364,7 @@ void setDisplayOrientation(int displayOrientation) { /** * Chooses a camera ID by the specified camera facing ({@link #mFacing}). - *

+ * *

This rewrites {@link #mCameraId}, {@link #mCameraCharacteristics}, and optionally * {@link #mFacing}.

*/ @@ -387,7 +407,7 @@ private void chooseCameraIdByFacing() { /** * Collects some information from {@link #mCameraCharacteristics}. - *

+ * *

This rewrites {@link #mPreviewSizes}, {@link #mPictureSizes}, and optionally, * {@link #mAspectRatio}.

*/ @@ -409,7 +429,7 @@ private void collectCameraInfo() { } } // fallback camera sizes and lower than Marshmellow - if (mPictureSizes.ratios().size() == 0) { + if(mPictureSizes.ratios().size() == 0){ for (android.util.Size size : map.getOutputSizes(ImageFormat.JPEG)) { mPictureSizes.add(new Size(size.getWidth(), size.getHeight())); } @@ -429,7 +449,7 @@ private void prepareImageReader() { /** * Starts opening a camera device. - *

+ * *

The result will be processed in {@link #mCameraDeviceCallback}.

*/ private void startOpeningCamera() { @@ -442,9 +462,9 @@ private void startOpeningCamera() { /** * Starts a capture session for camera preview. - *

+ * *

This rewrites {@link #mPreviewRequestBuilder}.

- *

+ * *

The result will be continuously processed in {@link #mSessionCallback}.

*/ private void startCaptureSession() {