如何使用 Android CameraX 库缩放预览?
How to zoom the preview using Android CameraX library?
希望添加放大预览图片的功能(请看图片A),示例代码在https://github.com/android/camera/tree/master/CameraXBasic
我已阅读 但以下代码不起作用。如何使用 CameraX API 1.0.0-alpha05 缩放预览?
/** 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 你做对了,我可以使用这段代码in/out缩放
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)
}
}
这是输出
更新:
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)
}
希望添加放大预览图片的功能(请看图片A),示例代码在https://github.com/android/camera/tree/master/CameraXBasic
我已阅读
/** 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 你做对了,我可以使用这段代码in/out缩放
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)
}
}
这是输出
更新:
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)
}