This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val cameraDevice = Camera.open(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val cameraManager = activity.getSystemService(Context.CAMERA_SERVICE) as CameraManager | |
val cameraId = cameraManager.cameraIdList[0] | |
cameraManager.openCamera(cameraId, object : CameraDevice.StateCallback() { | |
override fun onOpened(device: CameraDevice) { | |
// Do something with `device` | |
} | |
override fun onDisconnected(device: CameraDevice) { | |
device.close() | |
} | |
override fun onError(device: CameraDevice, error: Int) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val cameraIdList = cameraManager.cameraIdList // may be empty | |
val characteristics = cameraManager.getCameraCharacteristics(cameraId) | |
val cameraLensFacing = characteristics.get(CameraCharacteristics.LENS_FACING) | |
val cameraCapabilities = characteristics.get(CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES) | |
val cameraCompatible = cameraCapabilities?.contains( | |
CameraMetadata.REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) ?: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun getFirstCameraIdFacing(cameraManager: CameraManager, | |
facing: Int = CameraMetadata.LENS_FACING_BACK): String? { | |
// Get list of all compatible cameras | |
val cameraIds = cameraManager.cameraIdList.filter { | |
val characteristics = cameraManager.getCameraCharacteristics(it) | |
val capabilities = characteristics.get(CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES) | |
capabilities?.contains( | |
CameraMetadata.REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) ?: false | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun filterCompatibleCameras(cameraIds: Array<String>, | |
cameraManager: CameraManager): List<String> { | |
return cameraIds.filter { | |
val characteristics = cameraManager.getCameraCharacteristics(it) | |
characteristics.get(CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES)?.contains( | |
CameraMetadata.REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) ?: false | |
} | |
} | |
fun filterCameraIdsFacing(cameraIds: List<String>, cameraManager: CameraManager, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Retrieve the target surfaces, which could be coming from a number of places: | |
// 1. SurfaceView, if we want to display the image directly to the user | |
// 2. ImageReader, if we want to read each frame or perform frame-by-frame analysis | |
// 3. OpenGL Texture / TextureView, although discouraged for maintainability reasons | |
// 4. RenderScript.Allocation, if we want to do parallel processing | |
val surfaceView = findViewById<SurfaceView>(...) | |
val imageReader = ImageReader.newInstance(...) | |
// Remember to call this only *after* SurfaceHolder.Callback.surfaceCreated() | |
val previewSurface = surfaceView.holder.surface |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val session: CameraCaptureSession = ... // from CameraCaptureSession.StateCallback | |
val captureRequest = session.device.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW) | |
captureRequest.addTarget(previewSurface) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val session: CameraCaptureSession = ... // from CameraCaptureSession.StateCallback | |
val captureRequest: CaptureRequest = ... // from CameraDevice.createCaptureRequest() | |
// The first null argument corresponds to the capture callback, which we should | |
// provide if we wanted to retrieve frame metadata or keep track of failed capture | |
// requests that could indicate dropped frames; the second null argument | |
// corresponds to the Handler used by the asynchronous callback, which will fall | |
// back to the current thread's looper if null | |
session.capture(captureRequest.build(), null, null) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val session: CameraCaptureSession = ... // from CameraCaptureSession.StateCallback | |
val captureRequest: CaptureRequest = ... // from CameraDevice.createCaptureRequest() | |
// This will keep sending the capture request as frequently as possible until the | |
// session is torn down or session.stopRepeating() is called | |
session.setRepeatingRequest(captureRequest.build(), null, null) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val session: CameraCaptureSession = ... // from CameraCaptureSession.StateCallback | |
// Create the repeating request and dispatch it | |
val repeatingRequest = session.device.createCaptureRequest( | |
CameraDevice.TEMPLATE_PREVIEW) | |
repeatingRequest.addTarget(previewSurface) | |
session.setRepeatingRequest(repeatingRequest.build(), null, null) | |
// Some time later... |
OlderNewer