Android camera2 api surface preview
August 24, 2015 4:17 am
The android camera2 api surface preview tutorial completes the setup for the preview display which should now be displaying on the device
CODE AVAILABLE ON GITHUB
You can get the code from here and then you will need to Tag createcamerapreviewsession or else you can run this command
git clone --branch createcamerapreviewsession https://github.com/mobapptuts/recyclerview_image_gallery.git
CODE SAMPLES
CamaraIntentActivity
Member Variables
private CaptureRequest mPreviewCaptureRequest; private CaptureRequest.Builder mPreviewCaptureRequestBuilder; private CameraCaptureSession mCameraCaptureSession; private CameraCaptureSession.CaptureCallback mSessionCaptureCallback = new CameraCaptureSession.CaptureCallback() { @Override public void onCaptureStarted(CameraCaptureSession session, CaptureRequest request, long timestamp, long frameNumber) { super.onCaptureStarted(session, request, timestamp, frameNumber); } };
Method createCameraPreviewSession
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), 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(); } }
Category: Camera2 API, tutorials
Tags: android, API, camera2, CameraCaptureSession, CaptureRequest, how-to, preview, surface, tutorial