From f7498f0c3690164a7d1359e60c286304aacb8326 Mon Sep 17 00:00:00 2001 From: Dylan McIntyre Date: Tue, 24 Jan 2017 18:26:49 -0500 Subject: [PATCH] Add fast camera mode --- camerakit/build.gradle | 2 +- .../java/com/flurgle/camerakit/Camera1.java | 41 ++++++++++++++++++- .../com/flurgle/camerakit/utils/YuvUtils.java | 37 +++++++++++++++++ demo/src/main/res/layout/activity_main.xml | 2 +- 4 files changed, 79 insertions(+), 3 deletions(-) diff --git a/camerakit/build.gradle b/camerakit/build.gradle index db38fd47..ef6273ca 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.5' + PUBLISH_VERSION = '0.9.6' } android { diff --git a/camerakit/src/main/java/com/flurgle/camerakit/Camera1.java b/camerakit/src/main/java/com/flurgle/camerakit/Camera1.java index 2ccaabd3..0d5cc0df 100644 --- a/camerakit/src/main/java/com/flurgle/camerakit/Camera1.java +++ b/camerakit/src/main/java/com/flurgle/camerakit/Camera1.java @@ -1,5 +1,7 @@ package com.flurgle.camerakit; +import android.graphics.Rect; +import android.graphics.YuvImage; import android.hardware.Camera; import android.media.CamcorderProfile; import android.media.MediaRecorder; @@ -8,7 +10,9 @@ import android.support.v4.util.SparseArrayCompat; import android.view.SurfaceHolder; import com.flurgle.camerakit.utils.Size; +import com.flurgle.camerakit.utils.YuvUtils; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.util.List; @@ -162,7 +166,42 @@ public class Camera1 extends CameraViewImpl { @Override void captureStill() { + 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; + } + + YuvImage yuv = new YuvImage(rotatedData, parameters.getPreviewFormat(), postWidth, postHeight, null); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + yuv.compressToJpeg(new Rect(0, 0, postWidth, postHeight), 50, out); + byte[] bytes = out.toByteArray(); + getCameraListener().onPictureTaken(bytes); + } + }); } @Override @@ -273,7 +312,7 @@ public class Camera1 extends CameraViewImpl { // TODO: fix this mCameraParameters.setRotation(calcCameraRotation(mDisplayOrientation) + (mFacing == CameraKit.Constants.FACING_FRONT ? 180 : 0)); - + setAutoFocusInternal(mAutoFocus); setFlashInternal(mFlash); mCamera.setParameters(mCameraParameters); diff --git a/camerakit/src/main/java/com/flurgle/camerakit/utils/YuvUtils.java b/camerakit/src/main/java/com/flurgle/camerakit/utils/YuvUtils.java index d09ea710..a3af5f59 100644 --- a/camerakit/src/main/java/com/flurgle/camerakit/utils/YuvUtils.java +++ b/camerakit/src/main/java/com/flurgle/camerakit/utils/YuvUtils.java @@ -51,5 +51,42 @@ public class YuvUtils { return outputStream.toByteArray(); } + public static byte[] rotateNV21(final byte[] yuv, final int width, final int height, final int rotation) { + if (rotation == 0) return yuv; + if (rotation % 90 != 0 || rotation < 0 || rotation > 270) { + throw new IllegalArgumentException("0 <= rotation < 360, rotation % 90 == 0"); + } + + final byte[] output = new byte[yuv.length]; + final int frameSize = width * height; + final boolean swap = rotation % 180 != 0; + final boolean xflip = rotation % 270 != 0; + final boolean yflip = rotation >= 180; + + for (int j = 0; j < height; j++) { + for (int i = 0; i < width; i++) { + final int yIn = j * width + i; + final int uIn = frameSize + (j >> 1) * width + (i & ~1); + final int vIn = uIn + 1; + + final int wOut = swap ? height : width; + final int hOut = swap ? width : height; + final int iSwapped = swap ? j : i; + final int jSwapped = swap ? i : j; + final int iOut = xflip ? wOut - iSwapped - 1 : iSwapped; + final int jOut = yflip ? hOut - jSwapped - 1 : jSwapped; + + final int yOut = jOut * wOut + iOut; + final int uOut = frameSize + (jOut >> 1) * wOut + (iOut & ~1); + final int vOut = uOut + 1; + + output[yOut] = (byte) (0xff & yuv[yIn]); + output[uOut] = (byte) (0xff & yuv[uIn]); + output[vOut] = (byte) (0xff & yuv[vIn]); + } + } + return output; + } + } diff --git a/demo/src/main/res/layout/activity_main.xml b/demo/src/main/res/layout/activity_main.xml index 369bb7bf..68e22a77 100644 --- a/demo/src/main/res/layout/activity_main.xml +++ b/demo/src/main/res/layout/activity_main.xml @@ -21,7 +21,7 @@ app:ckCropOutput="true" app:ckFacing="back" app:ckFlash="off" - app:ckPictureMode="quality" /> + app:ckPictureMode="speed" />