I am trying to use android camera2 for face/eye detection. My problem is that there isn't much documentation on it and some functions do not work as I expect them to. I need to process every frame sent from the camera.
I am trying to use the captureRequestBuilder.addTarget()
to send a frame to the imageReader.OnAvailableImage()
but when I do that, I no longer get frames sent to the preview session.
This piece of code breaks my phone preview of the camera.
SurfaceTexture texture = textureView.getSurfaceTexture();
assert texture != null;
texture.setDefaultBufferSize(imageSize.getWidth(),imageSize.getHeight());
Surface surface = new Surface(texture);
ArrayList<Surface> surfaces = new ArrayList<>();
surfaces.add(surface);
surfaces.add(imageReader.getSurface());
captureRequestBuilder = cameraDevice.createCaptureRequest(cameraDevice.TEMPLATE_PREVIEW);
captureRequestBuilder.addTarget(surface);
captureRequestBuilder.addTarget(imageReader.getSurface());
cameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
But if I do it like this the preview works, but I can't find any way to process the frames
SurfaceTexture texture = textureView.getSurfaceTexture();
assert texture != null;
texture.setDefaultBufferSize(imageSize.getWidth(),imageSize.getHeight());
Surface surface = new Surface(texture);
captureRequestBuilder = cameraDevice.createCaptureRequest(cameraDevice.TEMPLATE_PREVIEW);
captureRequestBuilder.addTarget(surface);
cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
this is the ImageReader
ImageReader reader = ImageReader.newInstance(width,height,ImageFormat.JPEG,1);
List<Surface> outputSurface = new ArrayList<>(2);
outputSurface.add(reader.getSurface());
outputSurface.add(new Surface(textureView.getSurfaceTexture()));
int rotation = getWindowManager().getDefaultDisplay().getRotation();
ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
Image image = null;
try
{
image = reader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes);
}
catch (Exception e)
{
e.printStackTrace();
}
finally {
{
if (image != null)
image.close();
}
}
}
};
reader.setOnImageAvailableListener(readerListener,mBackgroundHandler);
Can anybody help me with real time processing of Camera2 frames?