Search code examples
previewface-detectionsnapshotcamera2

snapshotting a camera2 preview screen


I want to snapshot a preview screen using the camera2 API. I guess there are two spots where I can do this in my code at the surfacetexturelistner where it updates the textureview (this is where the preview renders to) or at the CameraCaptureSession.CaptureCallback where it updates the session for the preview. So I want the snapshot in bitmap so I can feed it to a FaceDetector(http://developer.android.com/reference/android/media/FaceDetector.html) class of course I will use a different thread to not hinder performance. The snapshot will not get noticed by the user. So here are the two areas to do it and want to know if anybody has done this or has any suggestions:

          private TextureView.SurfaceTextureListener SurListener =
            new TextureView.SurfaceTextureListener() {

                    @Override
                    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
                        drawView();
                        //I can maybe snapshot here?
                    }

                    @Override
                    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width,
                            int height) {
                    }

                    @Override
                    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                        return false;
                    }

                    @Override
                    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width,
                            int height) {
                        setupCamera();
                        StartUpCam();
                    }
                };

OR I can do it here:

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

            @Override
            public void onCaptureStarted(CameraCaptureSession session,
                    CaptureRequest request, long timestamp, long frameNumber) {
                super.onCaptureStarted(session, request, timestamp, frameNumber);
                //take a snapshot here???
            }
        };

Just to let you know, I am aware of the face detector that comes with camera2 API but that is only accessed after you take a picture so it's of no use to me because I want to do it at preview time.


Solution

  • It was really easy all I had to do was: gameSurface.getBitmap() Where gameSurface is the TextureView.