From bef1044819fcd2d343864410033daa2e5adaf8e4 Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Tue, 2 Jul 2019 14:57:22 -0500 Subject: [PATCH] Camera2 full support for hq pictures --- .../cameraview/engine/Camera2Engine.java | 21 ++-- .../cameraview/engine/CameraEngine.java | 3 +- .../internal/utils/WorkerHandler.java | 11 +- .../picture/Full2PictureRecorder.java | 115 ++++++++++++------ .../cameraview/demo/CameraActivity.java | 3 +- 5 files changed, 107 insertions(+), 46 deletions(-) diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera2Engine.java b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera2Engine.java index 25ea44d4..ac591199 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera2Engine.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera2Engine.java @@ -63,9 +63,6 @@ import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import androidx.annotation.WorkerThread; -// TODO full2picture fix rotation -// TODO full2picture add shutter -// TODO full2picture see if we don't launch activity // TODO zoom // TODO exposure correction // TODO autofocus @@ -102,6 +99,7 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv // Picture capturing private ImageReader mPictureReader; + private final boolean mPictureCaptureStopsPreview = false; // can make configurable at some point public Camera2Engine(Callback callback) { super(callback); @@ -212,13 +210,16 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv if (firstFrame.compareAndSet(false, true) && onFirstFrame != null) { onFirstFrame.run(); } + if (mPictureRecorder instanceof Full2PictureRecorder) { + ((Full2PictureRecorder) mPictureRecorder).onCaptureStarted(request); + } } @Override public void onCaptureProgressed(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull CaptureResult partialResult) { super.onCaptureProgressed(session, request, partialResult); if (mPictureRecorder instanceof Full2PictureRecorder) { - ((Full2PictureRecorder) mPictureRecorder).process(partialResult); + ((Full2PictureRecorder) mPictureRecorder).onCaptureProgressed(partialResult); } } @@ -226,7 +227,7 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) { super.onCaptureCompleted(session, request, result); if (mPictureRecorder instanceof Full2PictureRecorder) { - ((Full2PictureRecorder) mPictureRecorder).process(result); + ((Full2PictureRecorder) mPictureRecorder).onCaptureCompleted(result); } } @@ -614,7 +615,8 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv mRepeatingRequestBuilder, mRepeatingRequestCallback, builder, - mPictureReader); + mPictureReader, + mPictureCaptureStopsPreview); mPictureRecorder.take(); } catch (CameraAccessException e) { throw createCameraException(e); @@ -623,8 +625,13 @@ public class Camera2Engine extends CameraEngine implements ImageReader.OnImageAv @Override public void onPictureResult(@Nullable PictureResult.Stub result, @Nullable Exception error) { + boolean fullPicture = mPictureRecorder instanceof Full2PictureRecorder; super.onPictureResult(result, error); - applyRepeatingRequestBuilder(); + //noinspection StatementWithEmptyBody + if (fullPicture && mPictureCaptureStopsPreview) { + // See comments in Full2PictureRecorder. + applyRepeatingRequestBuilder(); + } } //endregion diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/CameraEngine.java b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/CameraEngine.java index 52b7ac71..b6277a06 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/CameraEngine.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/CameraEngine.java @@ -1225,7 +1225,8 @@ public abstract class CameraEngine implements // displayOffset - deviceOrientation // Returns the offset between two reference systems. - final int offset(int fromReference, int toReference) { + @SuppressWarnings("WeakerAccess") + public final int offset(int fromReference, int toReference) { if (fromReference == toReference) return 0; // We only know how to compute offsets with respect to REF_SENSOR. // That's why we separate the two cases. diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/internal/utils/WorkerHandler.java b/cameraview/src/main/java/com/otaliastudios/cameraview/internal/utils/WorkerHandler.java index 414b63b7..c2a38843 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/internal/utils/WorkerHandler.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/internal/utils/WorkerHandler.java @@ -52,6 +52,15 @@ public class WorkerHandler { return handler; } + /** + * Returns a fallback WorkerHandler. + * @return a fallback handler + */ + @NonNull + public static WorkerHandler get() { + return get("FallbackCameraThread"); + } + /** * Handy utility to perform an action in a fallback thread. * Not to be used for long-running operations since they will block @@ -60,7 +69,7 @@ public class WorkerHandler { * @param action the action */ public static void execute(@NonNull Runnable action) { - get("FallbackCameraThread").post(action); + get().post(action); } private HandlerThread mThread; diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/picture/Full2PictureRecorder.java b/cameraview/src/main/java/com/otaliastudios/cameraview/picture/Full2PictureRecorder.java index 3ac09316..d7a9ecaf 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/picture/Full2PictureRecorder.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/picture/Full2PictureRecorder.java @@ -16,6 +16,7 @@ import com.otaliastudios.cameraview.CameraException; import com.otaliastudios.cameraview.CameraLogger; import com.otaliastudios.cameraview.PictureResult; import com.otaliastudios.cameraview.internal.utils.ExifHelper; +import com.otaliastudios.cameraview.internal.utils.WorkerHandler; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -30,7 +31,7 @@ import androidx.exifinterface.media.ExifInterface; * A {@link PictureResult} that uses standard APIs. */ @RequiresApi(Build.VERSION_CODES.LOLLIPOP) -public class Full2PictureRecorder extends PictureRecorder { +public class Full2PictureRecorder extends PictureRecorder implements ImageReader.OnImageAvailableListener { private static final String TAG = Full2PictureRecorder.class.getSimpleName(); private static final CameraLogger LOG = CameraLogger.create(TAG); @@ -42,11 +43,14 @@ public class Full2PictureRecorder extends PictureRecorder { private static final int STATE_WAITING_CAPTURE = 4; private static final int STATE_WAITING_IMAGE = 5; + private static final int REQUEST_TAG = CameraDevice.TEMPLATE_STILL_CAPTURE; + private CameraCaptureSession mSession; private CaptureRequest.Builder mBuilder; private CameraCaptureSession.CaptureCallback mCallback; private ImageReader mPictureReader; private CaptureRequest.Builder mPictureBuilder; + private boolean mStopPreviewBeforeCapture; private int mState = STATE_IDLE; public Full2PictureRecorder(@NonNull PictureResult.Stub stub, @@ -55,42 +59,16 @@ public class Full2PictureRecorder extends PictureRecorder { @NonNull CaptureRequest.Builder builder, @NonNull CameraCaptureSession.CaptureCallback callback, @NonNull CaptureRequest.Builder pictureBuilder, - @NonNull ImageReader pictureReader) { + @NonNull ImageReader pictureReader, + boolean stopPreviewBeforeCapture) { super(stub, listener); mSession = session; mBuilder = builder; mCallback = callback; mPictureBuilder = pictureBuilder; + mStopPreviewBeforeCapture = stopPreviewBeforeCapture; mPictureReader = pictureReader; - mPictureReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() { - @Override - public void onImageAvailable(ImageReader reader) { - mState = STATE_IDLE; - - // Read the JPEG. - try { - Image image = reader.acquireLatestImage(); - ByteBuffer buffer = image.getPlanes()[0].getBuffer(); - byte[] bytes = new byte[buffer.remaining()]; - buffer.get(bytes); - mResult.data = bytes; - mResult.format = PictureResult.FORMAT_JPEG; - } catch (Exception e) { - mResult = null; - mError = e; - } - - // Before leaving, unlock focus. - try { - mBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, - CaptureRequest.CONTROL_AF_TRIGGER_CANCEL); - mSession.capture(mBuilder.build(), mCallback, null); - } catch (CameraAccessException ignore) { } - - // Leave. - dispatchResult(); - } - }, null); + mPictureReader.setOnImageAvailableListener(this, WorkerHandler.get().getHandler()); } @Override @@ -126,11 +104,18 @@ public class Full2PictureRecorder extends PictureRecorder { private void runCapture() { try { - mPictureBuilder.setTag(CameraDevice.TEMPLATE_STILL_CAPTURE); + mPictureBuilder.setTag(REQUEST_TAG); mPictureBuilder.addTarget(mPictureReader.getSurface()); mPictureBuilder.set(CaptureRequest.JPEG_ORIENTATION, mResult.rotation); - // mCaptureSession.stopRepeating(); - mSession.abortCaptures(); + if (mStopPreviewBeforeCapture) { + // These two are present in official samples and are probably meant to speed things up? + // But from my tests, they actually make everything slower. So this is disabled by default + // with a boolean coming from the engine. Maybe in the future we can make this configurable + // as some people might want to stop the preview while picture is being taken even if it + // increases the latency. + mSession.stopRepeating(); + mSession.abortCaptures(); + } mSession.capture(mPictureBuilder.build(), mCallback, null); } catch (CameraAccessException e) { mResult = null; @@ -139,7 +124,21 @@ public class Full2PictureRecorder extends PictureRecorder { } } - public void process(@NonNull CaptureResult result) { + public void onCaptureStarted(@NonNull CaptureRequest request) { + if (request.getTag() == (Integer) REQUEST_TAG) { + dispatchOnShutter(false); + } + } + + public void onCaptureProgressed(@NonNull CaptureResult result) { + process(result); + } + + public void onCaptureCompleted(@NonNull CaptureResult result) { + process(result); + } + + private void process(@NonNull CaptureResult result) { switch (mState) { case STATE_IDLE: break; case STATE_WAITING_FOCUS_LOCK: { @@ -179,7 +178,7 @@ public class Full2PictureRecorder extends PictureRecorder { } case STATE_WAITING_CAPTURE: { if (result instanceof TotalCaptureResult - && result.getRequest().getTag() == (Integer) CameraDevice.TEMPLATE_STILL_CAPTURE) { + && result.getRequest().getTag() == (Integer) REQUEST_TAG) { mState = STATE_WAITING_IMAGE; } break; @@ -187,6 +186,50 @@ public class Full2PictureRecorder extends PictureRecorder { } } + @Override + public void onImageAvailable(ImageReader reader) { + mState = STATE_IDLE; + + // Read the JPEG. + Image image = null; + //noinspection TryFinallyCanBeTryWithResources + try { + image = reader.acquireNextImage(); + ByteBuffer buffer = image.getPlanes()[0].getBuffer(); + byte[] bytes = new byte[buffer.remaining()]; + buffer.get(bytes); + mResult.data = bytes; + } catch (Exception e) { + mResult = null; + mError = e; + dispatchResult(); + return; + } finally { + if (image != null) image.close(); + } + + // Just like Camera1, unfortunately, the camera might rotate the image + // and put EXIF=0 instead of respecting our EXIF and leave the image unaltered. + mResult.format = PictureResult.FORMAT_JPEG; + mResult.rotation = 0; + try { + ExifInterface exif = new ExifInterface(new ByteArrayInputStream(mResult.data)); + int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); + mResult.rotation = ExifHelper.readExifOrientation(exifOrientation); + } catch (IOException ignore) { } + + // Before leaving, unlock focus. + try { + mBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, + CaptureRequest.CONTROL_AF_TRIGGER_CANCEL); + mSession.capture(mBuilder.build(), mCallback, null); + } catch (CameraAccessException ignore) { } + + // Leave. + dispatchResult(); + } + + @Override protected void dispatchResult() { mState = STATE_IDLE; diff --git a/demo/src/main/java/com/otaliastudios/cameraview/demo/CameraActivity.java b/demo/src/main/java/com/otaliastudios/cameraview/demo/CameraActivity.java index d1855572..ae676cb3 100644 --- a/demo/src/main/java/com/otaliastudios/cameraview/demo/CameraActivity.java +++ b/demo/src/main/java/com/otaliastudios/cameraview/demo/CameraActivity.java @@ -119,7 +119,8 @@ public class CameraActivity extends AppCompatActivity implements View.OnClickLis PicturePreviewActivity.setPictureResult(result); Intent intent = new Intent(CameraActivity.this, PicturePreviewActivity.class); intent.putExtra("delay", callbackTime - mCaptureTime); - // TODO temp startActivity(intent); + Log.e("CameraActivity", "Picture delay: " + (callbackTime - mCaptureTime)); + startActivity(intent); mCaptureTime = 0; }