Created
March 7, 2022 16:24
-
-
Save dp-singh/5b84756912ead7f1dc38870740ddb936 to your computer and use it in GitHub Desktop.
Change resolution function.
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
protected fun changeResolution( | |
api: UsbServiceApi, | |
resolution: CameraResolution = CameraResolution.SD, | |
afterUpdate: (() -> Unit)? = null | |
) { | |
lifecycle.coroutineScope.launch(UsbScheduler.dispatcher) { | |
if (!api.connectedDevice().isPresent) { | |
Timber.tag(TAG) | |
.e("Change resolution is called without the video device height=${resolution.height} width=${resolution.width}") | |
return@launch | |
} | |
val videoDevice: VideoDevice = api.connectedDevice().get() | |
val formats = videoDevice.availableFormats | |
val frameDescriptor: FrameDesc? = formats.find { format -> | |
// select format | |
format.getColorFormat() == ColorFormat.YUY2 || | |
format.getColorFormat() == ColorFormat.MJPEG | |
}?.frameDescs?.find { | |
it.width == resolution.width && it.height == resolution.height | |
} | |
if (frameDescriptor != null) { | |
// start stream | |
val stream = videoDevice.createStream( | |
frameDescriptor, | |
frameDescriptor.defaultFrameInterval | |
) | |
api.currentStreamReader.bindStream(stream).await() | |
afterUpdate?.invoke() | |
} else { | |
Timber.tag(TAG) | |
.e("Frame resolution not available height ${resolution.height} width${resolution.width}") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment