如何使用Android CameraX库缩放预览?
我希望添加一个功能,以放大预览图片(请参阅图片a)的样本代码在 我已经阅读了,但是下面的代码不起作用。如何使用CameraX API 1.0.0-alpha05缩放预览如何使用Android CameraX库缩放预览?,android,kotlin,androidx,android-camera2,Android,Kotlin,Androidx,Android Camera2,我希望添加一个功能,以放大预览图片(请参阅图片a)的样本代码在 我已经阅读了,但是下面的代码不起作用。如何使用CameraX API 1.0.0-alpha05缩放预览 /** Declare and bind preview, capture and analysis use cases */ private fun bindCameraUseCases() { ... // Apply declared configs to CameraX us
/** 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)
}
图像A
@HelloCW您做的事情是正确的,我可以使用此代码进行放大/缩小
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)
}
}
你在真实设备上试过吗?是的,我在真实设备上试过。可能是重复的谢谢,你能发布你的完整代码吗?我在XML中添加了两个按钮并设置了放大/缩小。这是我的startCamera()方法(在更新的答案中)非常感谢,但是我仍然无法运行您的项目,您可以发布您的完整代码吗?您知道Android Studio告诉我,
updateTransform()
没有定义,并且左,右,在你的代码中不能编译。我准备了一个演示帮助这个-只是把额外的两个按钮放大/缩小。
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)
}