Skip to content

Instantly share code, notes, and snippets.

Oscar Wahltinez owahltinez

Block or report user

Report or block owahltinez

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View core-principles-2.kt
// RECOMMENDED: Bind use cases to a lifecycle in a single call:
this as LifecycleOwner, preview, imageCapture, imageAnalyzer)
// Do *NOT* bind your use cases like this
CameraX.bindToLifecycle(this as LifecycleOwner, preview)
CameraX.bindToLifecycle(this as LifecycleOwner, imageCapture)
CameraX.bindToLifecycle(this as LifecycleOwner, imageAnalyzer)
View core-principles-1.kt
// STEP 1: Define use case configuration
val imageCaptureConfig = ImageCaptureConfig.Builder()
.setTargetResolution(Size(1280, 720))
// STEP 2: Create the use case object
val imageCapture = ImageCapture(imageCaptureConfig)
// STEP 3: Bind the use case to our lifecycle
CameraX.bindToLifecycle(this as LifecycleOwner, imageCapture)
View studioFlags.xml
<component name="StudioFlags">
<option name="data">
<!-- This flag enables experimental automotive templates -->
<entry key="npw.templates.automotive" value="true" />
View multi-camera-api-6.kt
val cameraSession: CameraCaptureSession = ...
// Use still capture template to build our capture request
val captureRequest = cameraSession.device.createCaptureRequest(
// Determine if this device supports distortion correction
val characteristics: CameraCharacteristics = ...
val supportsDistortionCorrection = characteristics.get(
View multi-camera-api-5.kt
val cameraManager: CameraManager = ...
// Get the two output targets from the activity / fragment
val surface1 = ... // from SurfaceView
val surface2 = ... // from SurfaceView
val dualCamera = findShortLongCameraPair(manager)!!
val outputTargets = DualCameraOutputs(
null, mutableListOf(surface1), mutableListOf(surface2))
View multi-camera-api-4.kt
fun findShortLongCameraPair(manager: CameraManager, facing: Int? = null): DualCamera? {
return findDualCameras(manager, facing).map {
val characteristics1 = manager.getCameraCharacteristics(it.physicalId1)
val characteristics2 = manager.getCameraCharacteristics(it.physicalId2)
// Query the focal lengths advertised by each physical camera
val focalLengths1 = characteristics1.get(
CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS) ?: floatArrayOf(0F)
val focalLengths2 = characteristics2.get(
View multi-camera-api-3.kt
* Helper type definition that encapsulates 3 sets of output targets:
* 1. Logical camera
* 2. First physical camera
* 3. Second physical camera
typealias DualCameraOutputs =
Triple<MutableList<Surface>?, MutableList<Surface>?, MutableList<Surface>?>
View multi-camera-api-2.kt
fun openDualCamera(cameraManager: CameraManager,
dualCamera: DualCamera,
executor: Executor = AsyncTask.SERIAL_EXECUTOR,
callback: (CameraDevice) -> Unit) {
dualCamera.logicalId, executor, object : CameraDevice.StateCallback() {
override fun onOpened(device: CameraDevice) = callback(device)
// Omitting for brevity...
override fun onError(device: CameraDevice, error: Int) = onDisconnected(device)
View multi-camera-api-1.kt
* Helper class used to encapsulate a logical camera and two underlying
* physical cameras
data class DualCamera(val logicalId: String, val physicalId1: String, val physicalId2: String)
fun findDualCameras(manager: CameraManager, facing: Int? = null): Array<DualCamera> {
val dualCameras = ArrayList<DualCamera>()
// Iterate over all the available camera characteristics
View using-multiple-camera-streams-11.kt
val frame = it.acquireNextImage()
// Do something with `frame` here
}, null)
You can’t perform that action at this time.