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: 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)) |
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 openDualCamera(cameraManager: CameraManager, | |
dualCamera: DualCamera, | |
executor: Executor = AsyncTask.SERIAL_EXECUTOR, | |
callback: (CameraDevice) -> Unit) { | |
cameraManager.openCamera( | |
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) |
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 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 |
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
imageReader.setOnImageAvailableListener({ | |
val frame = it.acquireNextImage() | |
// Do something with `frame` here | |
it.close() | |
}, 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 frameBufferCount = 3 // just an example, depends on your usage of ImageReader | |
val imageReader = ImageReader.newInstance( | |
imageReaderSize.width, imageReaderSize.height, ImageFormat.YUV_420_888 | |
frameBufferCount) |
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 surfaceView = findViewById<SurfaceView>(...) | |
surfaceView.holder.addCallback(object : SurfaceHolder.Callback { | |
override fun surfaceCreated(holder: SurfaceHolder) { | |
// We do not need to specify image format, and it will be considered of type PRIV | |
// Surface is now ready and we could use it as an output target for CameraSession | |
} | |
... | |
}) |
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 = ... | |
// Hardware level will be one of: | |
// - CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY, | |
// - CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL, | |
// - CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED, | |
// - CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL, | |
// - CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_3 | |
val hardwareLevel = characteristics.get( | |
CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL) |
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 <T>getMaximumOutputSize( | |
characteristics: CameraCharacteristics, targetClass: Class<T>, format: Int? = null): | |
Size { | |
val config = characteristics.get( | |
CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP) | |
// If image format is provided, use it to determine supported sizes; else use target class | |
val allSizes = if (format == null) | |
config.getOutputSizes(targetClass) else config.getOutputSizes(format) | |
return allSizes.sortedWith(compareBy { it.height * it.width }).reversed()[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 characteristics: CameraCharacteristics = ... | |
val targetClass: Class<T> = ... // e.g. SurfaceView::class.java | |
val sizes = characteristics.get( | |
CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP) | |
.getOutputSizes(targetClass) |
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 outputFormat: Int = ... // e.g. ImageFormat.JPEG | |
val sizes = characteristics.get( | |
CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP) | |
.getOutputSizes(outputFormat) |