0

我知道 CameraX 有点新,但它允许缩放吗?

我不是指在拍照之前直接在屏幕上(TextureView)放大图像吗?

4

3 回答 3

1

他们最近添加了同样的支持。它将包含在下一个版本中。

更改标识。https://android.googlesource.com/platform/frameworks/support/+/f60a1ac99a4144d5bfb0529fa0c769f43eb10c9f

于 2019-10-29T05:13:43.930 回答
1

假设这些依赖项:

dependencies {
    implementation "androidx.camera:camera-core:1.0.0-alpha08"
    implementation "androidx.camera:camera-camera2:1.0.0-alpha08"
    implementation "androidx.camera:camera-lifecycle:1.0.0-alpha02"
    implementation "androidx.camera:camera-view:1.0.0-alpha05"
    implementation "androidx.camera:camera-extensions:1.0.0-alpha05"    
}

这将是CameraFragment.kt图书馆的1.0.0-alpha08

class CameraFragment: Fragment(), View.OnTouchListener, ScaleGestureDetector.OnScaleGestureListener {

    private lateinit var scaleDetector: ScaleGestureDetector
    private lateinit var container: ConstraintLayout
    private lateinit var previewView: PreviewView
    private lateinit var cameraControl: CameraControl
    private lateinit var cameraInfo: CameraInfo
    private var lastScaleFactor = 0f
    ...

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        scaleDetector = ScaleGestureDetector(context, this);
        ...
    }

    @SuppressLint("MissingPermission", "ClickableViewAccessibility")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        container = view as ConstraintLayout
        previewView = container.findViewById(R.id.previewView)
        previewView.setOnTouchListener(this)
        ...
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouch(view: View?, event: MotionEvent?): Boolean {
        scaleDetector.onTouchEvent(event)
        return true
    }

    override fun onScaleBegin(detector: ScaleGestureDetector?): Boolean {
        return true
    }

    override fun onScaleEnd(detector: ScaleGestureDetector?) {
    }

    override fun onScale(detector: ScaleGestureDetector?): Boolean {
        val zoomRatio: Float? = cameraInfo.zoomRatio.value?.toFloat()
        val minZoomRatio: Float? = cameraInfo.minZoomRatio.value?.toFloat()
        val maxZoomRatio: Float? = cameraInfo.maxZoomRatio.value?.toFloat()
        val scaleFactor = scaleDetector.getScaleFactor()
        if (lastScaleFactor == 0f || (Math.signum(scaleFactor) == Math.signum(lastScaleFactor))) {
            cameraControl.setZoomRatio(Math.max(minZoomRatio!!, Math.min(zoomRatio!! * scaleFactor, maxZoomRatio!!)))
            lastScaleFactor = scaleFactor
        } else {
            lastScaleFactor = 0f
        }
        return true
    }
}

CameraXBasic/1.0.0-alpha08已经有PreviewView.

于 2020-01-02T12:26:19.747 回答
1

捏放大:

val camera = cameraProvider.bindToLifecycle(...

val scaleGestureDetector = ScaleGestureDetector(this,
    object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
        override fun onScale(detector: ScaleGestureDetector): Boolean {
            val scale = camera.cameraInfo.zoomState.value!!.zoomRatio * detector.scaleFactor
            camera.cameraControl.setZoomRatio(scale)
            return true
        }
    })

previewView.setOnTouchListener { view, event ->
    view.performClick()
    scaleGestureDetector.onTouchEvent(event)
    return@setOnTouchListener true
}
于 2021-02-01T09:37:09.550 回答