问题
I hope to add a function to zoom in preview picture (Please see Image A) for the sample code at https://github.com/android/camera/tree/master/CameraXBasic
I have read the article but the following code doesn't work. How can I zoom the preview with CameraX API 1.0.0-alpha05 ?
CameraFragment.kt
/** Declare and bind preview, capture and analysis use cases */
private fun bindCameraUseCases() {
...
// Apply declared configs to CameraX using the same lifecycle owner
CameraX.bindToLifecycle(
viewLifecycleOwner, preview, imageCapture, imageAnalyzer)
//I added code
var my=Rect(0,0,500,500)
preview.zoom(my)
}
Image A
回答1:
@HelloCW you are doing the right things, I am able to do zoom in/out using this code
button_plus.setOnClickListener {
if (right < 100) {
right += 100
bottom += 100
left += 100
top += 100
val my = Rect(left, top, right, bottom)
preview.zoom(my)
}
}
button_minus.setOnClickListener {
if (right > 0) {
right -= 100
bottom -= 100
left -= 100
top -= 100
val my = Rect(left, top, right, bottom)
preview.zoom(my)
}
}
Here is the output
Update :
private fun startCamera() {
val metrics = DisplayMetrics().also { view_finder.display.getRealMetrics(it) }
val screenAspectRatio = Rational(metrics.widthPixels, metrics.heightPixels)
val previewConfig = PreviewConfig.Builder().apply {
setTargetAspectRatio(screenAspectRatio)
setTargetRotation(view_finder.display.rotation)
}.build()
// Build the viewfinder use case
preview = Preview(previewConfig)
preview.setOnPreviewOutputUpdateListener {
// To update the SurfaceTexture, we have to remove it and re-add it
val parent = view_finder.parent as ViewGroup
parent.removeView(view_finder)
parent.addView(view_finder, 0)
view_finder.surfaceTexture = it.surfaceTexture
updateTransform()
}
CameraX.bindToLifecycle(this, preview)
}
来源:https://stackoverflow.com/questions/57881086/how-to-zoom-the-preview-using-android-camerax-library