From 7816a17d6459dce6b6c436b748e01ba1b530bd99 Mon Sep 17 00:00:00 2001 From: Dylan McIntyre Date: Tue, 24 Jan 2017 19:50:27 -0500 Subject: [PATCH] Faster still capture --- camerakit/build.gradle | 2 +- .../java/com/flurgle/camerakit/Camera1.java | 92 +++++++++++++------ 2 files changed, 64 insertions(+), 30 deletions(-) diff --git a/camerakit/build.gradle b/camerakit/build.gradle index ef6273ca..641ae688 100644 --- a/camerakit/build.gradle +++ b/camerakit/build.gradle @@ -3,7 +3,7 @@ apply plugin: 'com.android.library' ext { PUBLISH_GROUP_ID = 'com.flurgle' PUBLISH_ARTIFACT_ID = 'camerakit' - PUBLISH_VERSION = '0.9.6' + PUBLISH_VERSION = '0.9.7' } android { diff --git a/camerakit/src/main/java/com/flurgle/camerakit/Camera1.java b/camerakit/src/main/java/com/flurgle/camerakit/Camera1.java index 877ee4f5..e8712239 100644 --- a/camerakit/src/main/java/com/flurgle/camerakit/Camera1.java +++ b/camerakit/src/main/java/com/flurgle/camerakit/Camera1.java @@ -226,39 +226,73 @@ public class Camera1 extends CameraViewImpl { mCamera.setOneShotPreviewCallback(new Camera.PreviewCallback() { @Override public void onPreviewFrame(byte[] data, Camera camera) { - Camera.Parameters parameters = camera.getParameters(); - int width = parameters.getPreviewSize().width; - int height = parameters.getPreviewSize().height; - int rotation = mCameraInfo.orientation; - byte[] rotatedData = YuvUtils.rotateNV21(data, width, height, rotation); - - int postWidth; - int postHeight; - - switch (rotation) { - case 90: - case 270: - postWidth = height; - postHeight = width; - break; - - case 0: - case 180: - default: - postWidth = width; - postHeight = height; - break; - } + new Thread(new ProcessStillTask(data, camera, mCameraInfo, new ProcessStillTask.OnStillProcessedListener() { + @Override + public void onStillProcessed(final byte[] data) { + getView().post(new Runnable() { + @Override + public void run() { + getCameraListener().onPictureTaken(data); + } + }); + } + })).start(); + } + }); + } - YuvImage yuv = new YuvImage(rotatedData, parameters.getPreviewFormat(), postWidth, postHeight, null); + static class ProcessStillTask implements Runnable { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - yuv.compressToJpeg(new Rect(0, 0, postWidth, postHeight), 50, out); + byte[] data; + Camera camera; + Camera.CameraInfo cameraInfo; + OnStillProcessedListener onStillProcessedListener; - byte[] bytes = out.toByteArray(); - getCameraListener().onPictureTaken(bytes); + public ProcessStillTask(byte[] data, Camera camera, Camera.CameraInfo cameraInfo, OnStillProcessedListener onStillProcessedListener) { + this.data = data; + this.camera = camera; + this.cameraInfo = cameraInfo; + this.onStillProcessedListener = onStillProcessedListener; + } + + @Override + public void run() { + Camera.Parameters parameters = camera.getParameters(); + int width = parameters.getPreviewSize().width; + int height = parameters.getPreviewSize().height; + int rotation = cameraInfo.orientation; + byte[] rotatedData = YuvUtils.rotateNV21(data, width, height, rotation); + + int postWidth; + int postHeight; + + switch (rotation) { + case 90: + case 270: + postWidth = height; + postHeight = width; + break; + + case 0: + case 180: + default: + postWidth = width; + postHeight = height; + break; } - }); + + YuvImage yuv = new YuvImage(rotatedData, parameters.getPreviewFormat(), postWidth, postHeight, null); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + yuv.compressToJpeg(new Rect(0, 0, postWidth, postHeight), 50, out); + + onStillProcessedListener.onStillProcessed(out.toByteArray()); + } + + interface OnStillProcessedListener { + void onStillProcessed(byte[] data); + } + } @Override