Skip to content

Instantly share code, notes, and snippets.

val characteristics: CameraCharacteristics = ...
val context = this as Context // assuming we are inside of an activity
val surfaceViewSize = getPreviewOutputSize(
context, characteristics, SurfaceHolder::class.java)
val imageReaderSize = getPreviewOutputSize(
context, characteristics, ImageReader::class.java, format = ImageFormat.YUV_420_888)
// RECOMMENDED: Bind use cases to a lifecycle in a single call:
CameraX.bindToLifecycle(
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)
// STEP 1: Define use case configuration
val imageCaptureConfig = ImageCaptureConfig.Builder()
.setTargetResolution(Size(1280, 720))
.build()
// 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)
@owahltinez
owahltinez / select-next-camera.kt
Last active May 22, 2019 05:01
Select the most appropriate next camera from available ones
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,
@owahltinez
owahltinez / select-default-camera.kt
Last active May 22, 2019 04:31
Select the appropriate default camera
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
}
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
<application>
<component name="StudioFlags">
<option name="data">
<map>
<!-- This flag enables experimental automotive templates -->
<entry key="npw.templates.automotive" value="true" />
</map>
</option>
</component>
</application>
val cameraSession: CameraCaptureSession = ...
// Use still capture template to build our capture request
val captureRequest = cameraSession.device.createCaptureRequest(
CameraDevice.TEMPLATE_STILL_CAPTURE)
// Determine if this device supports distortion correction
val characteristics: CameraCharacteristics = ...
val supportsDistortionCorrection = characteristics.get(
CameraCharacteristics.DISTORTION_CORRECTION_AVAILABLE_MODES)?.contains(
/**
* 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>?>
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(