android video app still image setup
The android video app still image setup tutorial describes the steps involved for setting up the still image part of android video app.
Get Code
The code to start this tutorial is on github here
https://github.com/mobapptuts/android_camera2_api_video_app.git Tag camera2-video-still-setup
or you can run this command
git clone https://github.com/mobapptuts/android_camera2_api_video_app.git –branch camera2-video-still-setup
Steps
Setup members for the image size and image reader
private Size mImageSize;
private ImageReader mImageReader; private final ImageReader.OnImageAvailableListener mOnImageAvialableListener = new ImageReader.OnImageAvailableListener() { @Override public void onImageAvailable(ImageReader reader) { } };
Initialise those members in the setup camera method
mImageSize = chooseOptimalSize(map.getOutputSizes(MediaRecorder.class), rotatedWidth, rotatedHeight); mImageReader = ImageReader.newInstance(mImageSize.getWidth(), mImageSize.getHeight(), ImageFormat.JPEG, 1); mImageReader.setOnImageAvailableListener(mOnImageAvialableListener, null);
Add camera capture session member along with it’s capture callback for the preview
private CameraCaptureSession mPreviewCaptureSession; private CameraCaptureSession.CaptureCallback mPreviewCaptureCallback = new CameraCaptureSession.CaptureCallback() { @Override public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) { super.onCaptureCompleted(session, request, result); } };
Create some member states to be used for the preview capture session
private static final int STATE_PREVIEW = 0; private static final int STATE_WAIT_LOCK = 1; private int mCaptureState = STATE_PREVIEW;
Setup the preview capture callback to support the focus wait lock state
private final CameraCaptureSession.CaptureCallback mPreviewCaptureCallback = new CameraCaptureSession.CaptureCallback() { private void process(CaptureResult captureResult) { switch (mState) { case STATE_PREVIEW: break; case STATE__WAIT_LOCK: break; } } @Override public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) { super.onCaptureCompleted(session, request, result); process(result); } };
Setup the mPreviewCaptureSession member in the setupPreview method and add the ImageReader surface to the session
private void startPreview() { SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture(); surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); Surface previewSurface = new Surface(surfaceTexture); try { mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); mCaptureRequestBuilder.addTarget(previewSurface); mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, mImageReader.getSurface()), new CameraCaptureSession.StateCallback() { @Override public void onConfigured(CameraCaptureSession session) { mPreviewCaptureSession = session; try { mPreviewCaptureSession.setRepeatingRequest(mCaptureRequestBuilder.build(), mPreviewCaptureCallback, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override public void onConfigureFailed(CameraCaptureSession session) { Toast.makeText(getApplicationContext(), "Unable to setup camera preview", Toast.LENGTH_SHORT).show(); } }, null); } catch (CameraAccessException e) { e.printStackTrace(); } }
Create the lockFocus method
This will setup the request for auto focus trigger. And then setup the capture request for a single image.
private void lockFocus() { mCaptureState = STATE_WAIT_LOCK; mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_START); try { mPreviewCaptureSession.capture(mCaptureRequestBuilder.build(), mPreviewCaptureCallback, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } }
Inside the preview capture callback check when the autofocus has locked
private CameraCaptureSession.CaptureCallback mPreviewCaptureCallback = new CameraCaptureSession.CaptureCallback() { private void process(CaptureResult captureResult) { switch (mCaptureState) { case STATE_PREVIEW: // Do nothing break; case STATE_WAIT_LOCK: mCaptureState = STATE_PREVIEW; Integer afState = captureResult.get(CaptureResult.CONTROL_AF_STATE); if(afState == CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED || afState == CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED) { Toast.makeText(getApplicationContext(), "AF Locked!", Toast.LENGTH_SHORT).show(); } break; } } @Override public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) { super.onCaptureCompleted(session, request, result); process(result); } };
Add the lockFocus method to the camera button on click listener
mCaptureImageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { lockFocus(); } });
This will conclude the setup for the sill image capture portion of the android camera2 video app. With the following episode implementing the still capture session.
Android video app still image setup summary
In the android video app still image setup tutorial we implemented the ImageReader which will be used to capture the still image. We also created a camera capture session member to represent the preview session. The preview capture session will be used to for the still image capture request. Where its capture callback will be called and checked for auto focus lock. And the tutorial completes once auto focus lock has been completed.