How to get a specific camera in android multi camera device

289 views Asked by At

I want to get a specific camera id from multi camera device:

    private fun setupCamera() {
    val cameraIds: Array<String> = cameraManager.cameraIdList

    for (id in cameraIds) {
        val cameraCharacteristics = cameraManager.getCameraCharacteristics(id)
        if (cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT) {
            continue
        }
        val streamConfigurationMap: StreamConfigurationMap? =
            cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
        if (streamConfigurationMap != null) {
            previewSize =
                cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)!!
                    .getOutputSizes(ImageFormat.JPEG).maxByOrNull { it.height * it.width }!!
            imageReader = ImageReader.newInstance(
                previewSize.width, previewSize.height, ImageFormat.JPEG, 1
            )
            initPreview()
            imageReader.setOnImageAvailableListener(onImageAvailableListener, backgroundHandler)
        }
        cameraId = id
    }
}

I used this code, but it returns just a ultra wide rear camera of device. How can I select which camera that I want?

1

There are 1 answers