我最近使用Google repo Camera2Basic中的代码和此StackOverflow问题在TextureView上实现了Pinch Zoom:Zoom Camera2 Preview using TextureView
private var finger_spacing = 0f
private var zoom_level = 1f
override fun onTouch(view: View?, motionEvent: MotionEvent?): Boolean {
// Multi-touch -> ZOOM
// https://stackoverflow.com/questions/32711975/zoom-camera2-preview-using-textureview?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
if (motionEvent != null && view != null) {
if (motionEvent.pointerCount > 1) {
val characteristics = cameraManager.getCameraCharacteristics(currentDeviceId)
val maxzoom = characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM) * 10
val m = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE)
val current_finger_spacing = getFingerSpacing(motionEvent)
if (finger_spacing != 0f) {
if (current_finger_spacing > finger_spacing && maxzoom > zoom_level) {
zoom_level += 0.4f
} else if (current_finger_spacing < finger_spacing && zoom_level > 1) {
zoom_level -= 0.4f
}
val minW = (m.width() / maxzoom).toInt()
val minH = (m.height() / maxzoom).toInt()
val difW = m.width() - minW
val difH = m.height() - minH
var cropW = (difW / 100 * zoom_level).toInt()
var cropH = (difH / 100 * zoom_level).toInt()
cropW -= cropW and 3
cropH -= cropH and 3
val zoom = Rect(cropW, cropH,
m.width() - cropW, m.height() - cropH)
captureRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoom)
}
finger_spacing = current_finger_spacing
return true
}
捏缩放可以正常工作,但每次进行缩放时都会出现断断续续的情况,并且它无法像Android上的原生相机一样平滑。我真的很感激如何解决这个问题。感谢