Android camera2 speed-up

2019-06-04 11:12发布

问题:

I need to speed-up capturing of camera2 API. I tried to build "android-Camera2Basic" project from google samples. For default capture request from example:

 if (null == activity || null == mCameraDevice) {
            return;
        }
        // This is the CaptureRequest.Builder that we use to take a picture.
        final CaptureRequest.Builder captureBuilder =
                mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(mImageReader.getSurface());

        // Use the same AE and AF modes as the preview.
        captureBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        captureBuilder.set(CaptureRequest.CONTROL_AE_MODE,
                CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);

        // Orientation
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));

        CameraCaptureSession.CaptureCallback CaptureCallback
                = new CameraCaptureSession.CaptureCallback() {

            @Override
            public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
                                           TotalCaptureResult result) {
                showToast("Saved: " + mFile);
                unlockFocus();
            }
        };

        mCaptureSession.stopRepeating();
        mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);

It takes 200-300ms from send request

mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);

And get result in

onImageAvailable(ImageReader reader)

Is it possible to reduce this time? I tried set different parameters for capture request, such as TEMPLATE_ZERO_SHUTTER_LAG, NOISE_REDUCTION_MODE_OFF, EDGE_MODE_OFF, etc. But it has no any effect. If I try to capture burst, then all images, except first are comes very fast, no more then in 30-40ms. How can I reduce capturing time for first image?

回答1:

replying to your comment, but making it into a proper answer:

If you check those slides from the Samsung dev. conference on slide #22 it shows the camera2 model. As you can see, there're several queues:

  • Pending Request queue
  • In flight capture queue
  • output image queue to the Surface showing the camera preview
  • and the callback to onCaptureComplete

that explains why the 1st capture is slow, but in burst mode the next images comes very fast. The requests and processing are queued and the 1st takes 300ms to arrive all the way back on the callback but the next one is already "right behind it".

If you're interested in the new API (and who wouldn't be, camera2 is amazing), you can also check the full video from the Samsung Dev. conference on YouTube. And the official docs. Lot's of good info on those.