Skip to content

Instantly share code, notes, and snippets.

@tcw165
Last active September 23, 2017 01:43
Show Gist options
  • Save tcw165/fac091365c1c45311fd487db0635a725 to your computer and use it in GitHub Desktop.
Save tcw165/fac091365c1c45311fd487db0635a725 to your computer and use it in GitHub Desktop.
// Activity or Fragment ::onResume
// 1. Wait for the surface ready.
mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture texture,
int width, int height) {
// 2. Open the camera.
manager.openCamera(
cameraId,
new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice device) {
// 3. Create a capture session.
device.createCaptureSession(
previewSurfaces,
new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
// 4. Create a capture request.
mPreviewSession.setRepeatingRequest(
mPreviewBuilder.build(),
null,
mBackgroundHandler);
}
}, mBackgroundHandler);
}
},
null);
}
});
// Activity or Fragment ::onPause
if (captureSession != null) {
captureSession.close();
captureSession = null;
}
if (cameraDevice != null) {
cameraDevice.close();
cameraDevice = null;
}
if (previewReader != null) {
previewReader.close();
previewReader = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment