Skip to content

Instantly share code, notes, and snippets.

@deepak140596
Created August 28, 2019 13:52
Show Gist options
  • Save deepak140596/4229df904473b639c126cf36e6cd72a0 to your computer and use it in GitHub Desktop.
Save deepak140596/4229df904473b639c126cf36e6cd72a0 to your computer and use it in GitHub Desktop.
private fun startCamera() {
// Create configuration object for the viewfinder use case
val previewConfig = PreviewConfig.Builder().apply {
setTargetAspectRatio(Rational(1, 1))
setTargetResolution(Size(640, 640))
}.build()
// Build the viewfinder use case
val preview = Preview(previewConfig)
// Every time the viewfinder is updated, recompute layout
preview.setOnPreviewOutputUpdateListener {
// To update the SurfaceTexture, we have to remove it and re-add it
val parent = viewFinder.parent as ViewGroup
parent.removeView(viewFinder)
parent.addView(viewFinder, 0)
viewFinder.surfaceTexture = it.surfaceTexture
updateTransform()
}
// Bind use cases to lifecycle
// If Android Studio complains about "this" being not a LifecycleOwner
// try rebuilding the project or updating the appcompat dependency to
// version 1.1.0 or higher.
CameraX.bindToLifecycle(this, preview)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment