YUV Handling!!!!

pull/1/head
Dylan McIntyre 8 years ago
parent 92e0e6ac6e
commit b448e09944
  1. 44
      camerakit/src/main/java/com/flurgle/camerakit/Camera2.java
  2. 45
      camerakit/src/main/java/com/flurgle/camerakit/YuvUtils.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();
}
};
}

@ -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();
}
}
Loading…
Cancel
Save