android camera2 api capture still image
This android tutorial describes the steps involved for the android camera2 api capture still image.
Get Code
You can get it from here https://github.com/mobapptuts/recyclerview_image_gallery.git Tag camera2-still-capture
or else run this command
git clone —branch camera2-still-capture https://github.com/mobapptuts/recyclerview_image_gallery.git
Code Samples
Clean up the ImageReader resource when the application is closed
private void closeCamera() { if(mCameraCaptureSession != null) { mCameraCaptureSession.close(); mCameraCaptureSession = null; } if(mCameraDevice != null) { mCameraDevice.close(); mCameraDevice = null; } if(mImageReader != null) { mImageReader.close(); mImageReader = null; } }
Create a SparseIntArray member to translating the orientations from the display screen to jpeg images.
private static final SparseIntArray ORIENTATIONS = new SparseIntArray(); static { ORIENTATIONS.append(Surface.ROTATION_0, 90); ORIENTATIONS.append(Surface.ROTATION_90, 0); ORIENTATIONS.append(Surface.ROTATION_180, 270); ORIENTATIONS.append(Surface.ROTATION_270, 180); }
Add the ImageReader to the CameraCaptureSession
private void createCameraPreviewSession() { try { SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture(); surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); Surface previewSurface = new Surface(surfaceTexture); mPreviewCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); mPreviewCaptureRequestBuilder.addTarget(previewSurface); mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, mImageReader.getSurface()), new CameraCaptureSession.StateCallback() { @Override public void onConfigured(CameraCaptureSession session) { if(mCameraDevice == null) { return; } try { mPreviewCaptureRequest = mPreviewCaptureRequestBuilder.build(); mCameraCaptureSession = session; mCameraCaptureSession.setRepeatingRequest( mPreviewCaptureRequest, mSessionCaptureCallback, mBackgroundHandler ); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override public void onConfigureFailed(CameraCaptureSession session) { Toast.makeText( getApplicationContext(), "create camera session failed!", Toast.LENGTH_SHORT ).show(); } }, null); } catch (CameraAccessException e) { e.printStackTrace(); } }
Create captureStillImage method
private void captureStillImage() { try { CaptureRequest.Builder captureStillBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); captureStillBuilder.addTarget(mImageReader.getSurface()); int rotation = getWindowManager().getDefaultDisplay().getRotation(); captureStillBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation)); CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() { @Override public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) { super.onCaptureCompleted(session, request, result); Toast.makeText(getApplicationContext(), "Image Captured!", Toast.LENGTH_SHORT).show(); unLockFocus(); } }; mCameraCaptureSession.capture( captureStillBuilder.build(), captureCallback, null ); } catch (CameraAccessException e) { e.printStackTrace(); } }
Call the captureStillImage method from within CameraCaptureSession callback STATE_WAIT_LOCK state.
private CameraCaptureSession.CaptureCallback mSessionCaptureCallback = new CameraCaptureSession.CaptureCallback() { private void process(CaptureResult result) { switch(mState) { case STATE_PREVIEW: // Do nothing break; case STATE__WAIT_LOCK: Integer afState = result.get(CaptureResult.CONTROL_AF_STATE); if(afState == CaptureRequest.CONTROL_AF_STATE_FOCUSED_LOCKED) { /* unLockFocus(); Toast.makeText(getApplicationContext(), "Focus Lock Successful", Toast.LENGTH_SHORT).show(); */ captureStillImage(); } break; } } @Override public void onCaptureStarted(CameraCaptureSession session, CaptureRequest request, long timestamp, long frameNumber) { super.onCaptureStarted(session, request, timestamp, frameNumber); } @Override public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) { super.onCaptureCompleted(session, request, result); process(result); } @Override public void onCaptureFailed(CameraCaptureSession session, CaptureRequest request, CaptureFailure failure) { super.onCaptureFailed(session, request, failure); Toast.makeText(getApplicationContext(), "Focus Lock Unsuccessful", Toast.LENGTH_SHORT).show(); } };