Skip to content

Instantly share code, notes, and snippets.

@ashwin-sp
Last active February 18, 2018 19:52
Show Gist options
  • Save ashwin-sp/d1b06be5df363c523ec06cbeae8c64d1 to your computer and use it in GitHub Desktop.
Save ashwin-sp/d1b06be5df363c523ec06cbeae8c64d1 to your computer and use it in GitHub Desktop.
/**
* Begin a still image capture
*/
fun takePicture() {
if (cameraDevice ==
null) {
Log.w(TAG, "Cannot capture image. Camera not initialized.")
return
}
// Here, we create a CameraCaptureSession for capturing still images.
try {
cameraDevice.createCaptureSession(
listOf<Surface>(imageReader.getSurface()),
sessionCallback, null)
} catch (cae: CameraAccessException) {
Log.d(TAG, "access exception while preparing pic", cae)
}
}
/**
* Callback handling session state changes
*/
private val sessionCallback = object : CameraCaptureSession.StateCallback() {
override fun onConfigured(cameraCaptureSession: CameraCaptureSession) {
// The camera is already closed
if (cameraDevice == null) {
return
}
// When the session is ready, we start capture.
captureSession = cameraCaptureSession
triggerImageCapture()
}
override fun onConfigureFailed(cameraCaptureSession: CameraCaptureSession) {
Log.w(TAG, "Failed to configure camera")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment