From b448e09944a28696257c06a0d1db9f968f2d4908 Mon Sep 17 00:00:00 2001 From: Dylan McIntyre Date: Mon, 23 Jan 2017 18:41:53 -0500 Subject: [PATCH] YUV Handling!!!! --- .../java/com/flurgle/camerakit/Camera2.java | 46 +++++++++++++------ .../java/com/flurgle/camerakit/YuvUtils.java | 45 ++++++++++++++++++ 2 files changed, 76 insertions(+), 15 deletions(-) create mode 100644 camerakit/src/main/java/com/flurgle/camerakit/YuvUtils.java diff --git a/camerakit/src/main/java/com/flurgle/camerakit/Camera2.java b/camerakit/src/main/java/com/flurgle/camerakit/Camera2.java index 11d74271..0f01b6c4 100644 --- a/camerakit/src/main/java/com/flurgle/camerakit/Camera2.java +++ b/camerakit/src/main/java/com/flurgle/camerakit/Camera2.java @@ -174,7 +174,9 @@ public class Camera2 extends CameraViewImpl { @Override void captureStill() { - + if (mOnImageAvailableListener != null) { + mOnImageAvailableListener.allowCallback(); + } } @Override @@ -520,26 +522,40 @@ public class Camera2 extends CameraViewImpl { }; - private final ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() { + private abstract static class VariableCallbackOnImageAvailableListener implements ImageReader.OnImageAvailableListener { + + protected boolean mAllowOneCallback = false; + + @Override + public abstract void onImageAvailable(ImageReader reader); + + public void allowCallback() { + mAllowOneCallback = true; + } + + } + + private VariableCallbackOnImageAvailableListener mOnImageAvailableListener = new VariableCallbackOnImageAvailableListener() { @Override public void onImageAvailable(ImageReader reader) { Image image = reader.acquireLatestImage(); - // Process the image - image.close(); -// try (Image image = reader.acquireNextImage()) { -// image.close(); -// Image.Plane[] planes = image.getPlanes(); -// if (planes.length > 0) { -// ByteBuffer buffer = planes[0].getBuffer(); -// byte[] data = new byte[buffer.remaining()]; -// buffer.get(data); -// //getCameraListener().onPictureTaken(data); -// } -// } - } + if (image == null) { + return; + } + + if (!mAllowOneCallback) { + image.close(); + return; + } + + mAllowOneCallback = false; + byte[] out = YuvUtils.createRGB(image); + getCameraListener().onPictureTaken(out); + image.close(); + } }; } diff --git a/camerakit/src/main/java/com/flurgle/camerakit/YuvUtils.java b/camerakit/src/main/java/com/flurgle/camerakit/YuvUtils.java new file mode 100644 index 00000000..0aa54f6f --- /dev/null +++ b/camerakit/src/main/java/com/flurgle/camerakit/YuvUtils.java @@ -0,0 +1,45 @@ +package com.flurgle.camerakit; + +import android.graphics.ImageFormat; +import android.graphics.Rect; +import android.graphics.YuvImage; +import android.media.Image; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; + +public class YuvUtils { + + public static byte[] createRGB(Image image) { + ByteBuffer bufferY = image.getPlanes()[0].getBuffer(); + byte[] y = new byte[bufferY.remaining()]; + bufferY.get(y); + + ByteBuffer bufferU = image.getPlanes()[1].getBuffer(); + byte[] u = new byte[bufferU.remaining()]; + bufferU.get(u); + + ByteBuffer bufferV = image.getPlanes()[2].getBuffer(); + byte[] v = new byte[bufferV.remaining()]; + bufferV.get(v); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + try { + outputStream.write(y); + outputStream.write(v); + outputStream.write(u); + } catch (IOException e) { + + } + byte data[] = outputStream.toByteArray(); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, image.getWidth(), image.getHeight(), null); + yuvImage.compressToJpeg(new Rect(0, 0, image.getWidth(), image.getHeight()), 50, out); + + return out.toByteArray(); + } + +} +