From b28d53121fc9bb0b20b796d756ea71379b28f176 Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Tue, 29 Aug 2017 12:14:46 +0200 Subject: [PATCH] Start and stop in our thread --- .../com/otaliastudios/cameraview/Camera1.java | 20 ++++++--- .../com/otaliastudios/cameraview/Camera2.java | 7 +++- .../cameraview/CameraController.java | 41 ++++++++++++++++++- 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java index 2018ee35..ce09e397 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java @@ -11,6 +11,7 @@ import android.os.Build; import android.os.Handler; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.annotation.WorkerThread; import android.util.Log; import android.view.SurfaceHolder; @@ -129,9 +130,10 @@ class Camera1 extends CameraController { } + @WorkerThread @Override - void start() { - if (isCameraOpened()) stop(); + void onStart() { + if (isCameraOpened()) onStop(); if (collectCameraId()) { mCamera = Camera.open(mCameraId); @@ -155,9 +157,9 @@ class Camera1 extends CameraController { } } - + @WorkerThread @Override - void stop() { + void onStop() { mFocusHandler.removeCallbacksAndMessages(null); if (isCameraOpened()) { if (mIsCapturingVideo) endVideo(); @@ -390,9 +392,15 @@ class Camera1 extends CameraController { mCamera.takePicture(null, null, null, new Camera.PictureCallback() { @Override - public void onPictureTaken(byte[] data, Camera camera) { + public void onPictureTaken(byte[] data, final Camera camera) { mIsCapturingImage = false; - camera.startPreview(); // This is needed, read somewhere in the docs. + post(new Runnable() { + @Override + public void run() { + // This is needed, read somewhere in the docs. + camera.startPreview(); + } + }); mCameraCallbacks.processImage(data, consistentWithView, exifFlip); } }); diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java index b50c9ce8..0cbad1ca 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java @@ -12,6 +12,7 @@ import android.hardware.camera2.params.StreamConfigurationMap; import android.location.Location; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.annotation.WorkerThread; import android.util.Log; import java.io.File; @@ -79,13 +80,15 @@ class Camera2 extends CameraController { // CameraImpl: + @WorkerThread @Override - void start() { + void onStart() { } + @WorkerThread @Override - void stop() { + void onStop() { } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java index 6cd4c921..28a30be1 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java @@ -4,6 +4,7 @@ import android.graphics.PointF; import android.location.Location; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.annotation.WorkerThread; import java.io.File; @@ -19,14 +20,50 @@ abstract class CameraController implements Preview.SurfaceCallback { protected SessionType mSessionType; protected Hdr mHdr; + private WorkerHandler mHandler; + CameraController(CameraView.CameraCallbacks callback, Preview preview) { mCameraCallbacks = callback; mPreview = preview; mPreview.setSurfaceCallback(this); + mHandler = new WorkerHandler("CameraViewController"); + } + + protected final void post(Runnable runnable) { + mHandler.post(runnable); + } + + //region Start&Stop + + // Starts the preview asynchronously. + final void start() { + mHandler.post(new Runnable() { + @Override + public void run() { + onStart(); + } + }); + } + + // Stops the preview asynchronously. + final void stop() { + mHandler.post(new Runnable() { + @Override + public void run() { + onStop(); + } + }); } - abstract void start(); - abstract void stop(); + // Starts the preview. + @WorkerThread + abstract void onStart(); + + // Stops the preview. + @WorkerThread + abstract void onStop(); + + //endregion abstract void onDisplayOffset(int displayOrientation); abstract void onDeviceOrientation(int deviceOrientation);