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 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) |
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
// 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) |
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
// 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) |
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
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
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
<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> |
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 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( |
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
/** | |
* 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>?> |
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 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( |