In the original (now obsolete) camera API, we were able to receive preview frames in Camera.PreviewCallback and be able to process it (possibly for a very long time) and release the buffer in order to be able to get another frame without delaying the screen preview, with some code as shown below:
public void onPreviewFrame(final byte[] data, Camera camera) { new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { (... do some slow processing ...) } @Override protected void onPostExecute(Void aVoid) { mCamera.addCallbackBuffer(data);
The API will only be a callback with a new frame, if there can be another buffer to receive it without delaying the screen preview.
I am trying to reproduce the same behavior in the new Camera2 API, but I cannot find a way to do this without delaying the screen preview. If I add a second target (same resolution as the screen image, YUV_420_888) in the preview request:
mPreviewRequestBuilder.addTarget(surface); mPreviewRequestBuilder.addTarget(previewImageReader.getSurface()); mCameraDevice.createCaptureSession( Arrays.asList(surface, previewImageReader.getSurface()), ...
the screen preview will lag even if I just close the image as soon as I get it:
@Override public void onImageAvailable(ImageReader reader) { reader.acquireNextImage().close(); }
What is the right way to use Camera2 to emulate the behavior of the API of the source camera (i.e. if you have a new buffer when it is free and does not slow down the screen)?
Update. If anyone is interested in what the rest of the code looks like, this is just a modified version of the standard sample android-camera2Basic, here is what I changed .