From c9a3c321296a0d594034044518448e3a933af5b4 Mon Sep 17 00:00:00 2001 From: pqpo Date: Tue, 18 Sep 2018 20:48:28 +0800 Subject: [PATCH] change callback --- .../me/pqpo/smartcamera/MainActivity.java | 13 ++- .../google/android/cameraview/Camera1.java | 20 ++--- .../google/android/cameraview/CameraView.java | 82 +++++-------------- .../pqpo/smartcameralib/SmartCameraView.java | 7 +- 4 files changed, 45 insertions(+), 77 deletions(-) diff --git a/app/src/main/java/me/pqpo/smartcamera/MainActivity.java b/app/src/main/java/me/pqpo/smartcamera/MainActivity.java index b25fc93..7102a42 100644 --- a/app/src/main/java/me/pqpo/smartcamera/MainActivity.java +++ b/app/src/main/java/me/pqpo/smartcamera/MainActivity.java @@ -14,7 +14,7 @@ import android.widget.ImageView; import android.widget.Toast; -import com.google.android.cameraview.CameraView; +import com.google.android.cameraview.Camera1; import com.tbruyelle.rxpermissions2.RxPermissions; import io.reactivex.Observer; @@ -141,11 +141,16 @@ public boolean onScanResult(SmartCameraView smartCameraView, int result, byte[] } }); - mCameraView.addCallback(new CameraView.Callback() { + mCameraView.addCallback(new Camera1.Callback() { @Override - public void onPictureTaken(CameraView cameraView, byte[] data) { - super.onPictureTaken(cameraView, data); + public void onCameraOpened(Camera1 camera) { + super.onCameraOpened(camera); + } + + @Override + public void onPictureTaken(Camera1 camera, byte[] data) { + super.onPictureTaken(camera, data); mCameraView.cropImage(data, new SmartCameraView.CropCallback() { @Override public void onCropped(Bitmap cropBitmap) { diff --git a/smartcameralib/src/cameraview/java/com/google/android/cameraview/Camera1.java b/smartcameralib/src/cameraview/java/com/google/android/cameraview/Camera1.java index bbfdd5a..b9ec55a 100644 --- a/smartcameralib/src/cameraview/java/com/google/android/cameraview/Camera1.java +++ b/smartcameralib/src/cameraview/java/com/google/android/cameraview/Camera1.java @@ -39,7 +39,7 @@ import java.util.SortedSet; import java.util.concurrent.atomic.AtomicBoolean; -class Camera1 { +public class Camera1 { private static final int INVALID_CAMERA_ID = -1; @@ -120,7 +120,7 @@ void startPreview() { public void handleMessage(Message msg) { super.handleMessage(msg); if (previewBuffer != null && mCallback != null) { - mCallback.onPicturePreview(previewBuffer); + mCallback.onPicturePreview(Camera1.this, previewBuffer); if (mCamera != null) { mCamera.addCallbackBuffer(previewBuffer); } @@ -315,7 +315,7 @@ void takePictureInternal() { public void onPictureTaken(byte[] data, Camera camera) { isPictureCaptureInProgress.set(false); - mCallback.onPictureTaken(data); + mCallback.onPictureTaken(Camera1.this, data); camera.cancelAutoFocus(); startPreview(); } @@ -378,7 +378,7 @@ private void openCamera() { } adjustCameraParameters(); mCamera.setDisplayOrientation(calcDisplayOrientation(mDisplayOrientation)); - mCallback.onCameraOpened(); + mCallback.onCameraOpened(this); } private AspectRatio chooseAspectRatio() { @@ -448,7 +448,7 @@ private void releaseCamera() { if (mCamera != null) { mCamera.release(); mCamera = null; - mCallback.onCameraClosed(); + mCallback.onCameraClosed(this); } } @@ -549,15 +549,15 @@ private boolean setFlashInternal(int flash) { } } - interface Callback { + public abstract static class Callback { - void onCameraOpened(); + public void onCameraOpened(Camera1 camera){} - void onCameraClosed(); + public void onCameraClosed(Camera1 camera){} - void onPictureTaken(byte[] data); + public void onPictureTaken(Camera1 camera, byte[] data){} - void onPicturePreview(byte[] data); + public void onPicturePreview(Camera1 camera, byte[] data){} } diff --git a/smartcameralib/src/cameraview/java/com/google/android/cameraview/CameraView.java b/smartcameralib/src/cameraview/java/com/google/android/cameraview/CameraView.java index be6ba34..0d563d0 100644 --- a/smartcameralib/src/cameraview/java/com/google/android/cameraview/CameraView.java +++ b/smartcameralib/src/cameraview/java/com/google/android/cameraview/CameraView.java @@ -127,6 +127,10 @@ public void onDisplayOrientationChanged(int displayOrientation) { }; } + public Camera1 getCamera() { + return mImpl; + } + @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); @@ -267,21 +271,15 @@ public boolean isCameraOpened() { /** * Add a new callback. - * - * @param callback The {@link Callback} to add. - * @see #removeCallback(Callback) */ - public void addCallback(@NonNull Callback callback) { + public void addCallback(@NonNull Camera1.Callback callback) { mCallbacks.add(callback); } /** * Remove a callback. - * - * @param callback The {@link Callback} to remove. - * @see #addCallback(Callback) */ - public void removeCallback(@NonNull Callback callback) { + public void removeCallback(@NonNull Camera1.Callback callback) { mCallbacks.remove(callback); } @@ -398,58 +396,57 @@ public int getFlash() { /** * Take a picture. The result will be returned to - * {@link Callback#onPictureTaken(CameraView, byte[])}. */ public void takePicture() { mImpl.takePicture(); } - private class CallbackBridge implements Camera1.Callback { + private class CallbackBridge extends Camera1.Callback { - private final ArrayList mCallbacks = new ArrayList<>(); + private final ArrayList mCallbacks = new ArrayList<>(); private boolean mRequestLayoutOnOpen; CallbackBridge() { } - public void add(Callback callback) { + public void add(Camera1.Callback callback) { mCallbacks.add(callback); } - public void remove(Callback callback) { + public void remove(Camera1.Callback callback) { mCallbacks.remove(callback); } @Override - public void onCameraOpened() { + public void onCameraOpened(Camera1 camera) { if (mRequestLayoutOnOpen) { mRequestLayoutOnOpen = false; requestLayout(); } - for (Callback callback : mCallbacks) { - callback.onCameraOpened(CameraView.this); + for (Camera1.Callback callback : mCallbacks) { + callback.onCameraOpened(camera); } } @Override - public void onCameraClosed() { - for (Callback callback : mCallbacks) { - callback.onCameraClosed(CameraView.this); + public void onCameraClosed(Camera1 camera) { + for (Camera1.Callback callback : mCallbacks) { + callback.onCameraClosed(camera); } } @Override - public void onPictureTaken(byte[] data) { - for (Callback callback : mCallbacks) { - callback.onPictureTaken(CameraView.this, data); + public void onPictureTaken(Camera1 camera, byte[] data) { + for (Camera1.Callback callback : mCallbacks) { + callback.onPictureTaken(camera, data); } } @Override - public void onPicturePreview(byte[] data) { - for (Callback callback : mCallbacks) { - callback.onPicturePreview(CameraView.this, data); + public void onPicturePreview(Camera1 camera, byte[] data) { + for (Camera1.Callback callback : mCallbacks) { + callback.onPicturePreview(camera, data); } } @@ -509,39 +506,4 @@ public SavedState[] newArray(int size) { } - /** - * Callback for monitoring events about {@link CameraView}. - */ - @SuppressWarnings("UnusedParameters") - public abstract static class Callback { - - /** - * Called when camera is opened. - * - * @param cameraView The associated {@link CameraView}. - */ - public void onCameraOpened(CameraView cameraView) { - } - - /** - * Called when camera is closed. - * - * @param cameraView The associated {@link CameraView}. - */ - public void onCameraClosed(CameraView cameraView) { - } - - /** - * Called when a picture is taken. - * - * @param cameraView The associated {@link CameraView}. - * @param data JPEG data. - */ - public void onPictureTaken(CameraView cameraView, byte[] data) { - } - - public void onPicturePreview(CameraView cameraView, byte[] data) { - } - } - } diff --git a/smartcameralib/src/main/java/me/pqpo/smartcameralib/SmartCameraView.java b/smartcameralib/src/main/java/me/pqpo/smartcameralib/SmartCameraView.java index fbd5b79..e25650d 100644 --- a/smartcameralib/src/main/java/me/pqpo/smartcameralib/SmartCameraView.java +++ b/smartcameralib/src/main/java/me/pqpo/smartcameralib/SmartCameraView.java @@ -11,6 +11,7 @@ import android.support.annotation.Nullable; import android.util.AttributeSet; +import com.google.android.cameraview.Camera1; import com.google.android.cameraview.CameraView; import com.google.android.cameraview.base.Size; @@ -46,10 +47,10 @@ private void init() { smartScanner = new SmartScanner(); uiHandler = new ScanResultHandler(this); - addCallback(new Callback() { + addCallback(new Camera1.Callback() { @Override - public void onPicturePreview(CameraView cameraView, byte[] data) { - super.onPicturePreview(cameraView, data); + public void onPicturePreview(Camera1 camera, byte[] data) { + super.onPicturePreview(camera, data); if (data == null || !scanning) { return; }