Skip to content

Instantly share code, notes, and snippets.

@dp-singh
Created March 7, 2022 16:24
Show Gist options
  • Save dp-singh/5b84756912ead7f1dc38870740ddb936 to your computer and use it in GitHub Desktop.
Save dp-singh/5b84756912ead7f1dc38870740ddb936 to your computer and use it in GitHub Desktop.
Change resolution function.
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