如何在CameraX中按TextureView的宽度和高度缩放预览视频?

时间:2019-11-19 04:09:20

标签: android-layout video preview textureview android-camerax

我已经为我的TextureView分配了一个固定的高度:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/camera_container"
    android:background="@android:color/black"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextureView
        android:id="@+id/view_finder"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

但是我的视频预览仍会随着设备的缩放而缩放: my video preview

这是我在kotlin类中的设置代码:

    @SuppressLint("RestrictedApi")
    private fun bindCameraUseCases() {

        // Get screen metrics used to setup camera for full screen resolution
        val metrics = DisplayMetrics().also { viewFinder.display.getRealMetrics(it) }
        val screenAspectRatio = Rational(metrics.widthPixels, metrics.heightPixels)
        Log.e(TAG, "Screen metrics: ${metrics.widthPixels} x ${metrics.heightPixels}")

        // Set up the view finder use case to display camera preview
        val viewFinderConfig = PreviewConfig.Builder().apply {
            setLensFacing(lensFacing)
            setTargetAspectRatio(screenAspectRatio)
            setTargetRotation(viewFinder.display.rotation)
        }.build()

        preview = AutoFitPreviewBuilder.build(viewFinderConfig, viewFinder)

        val videoCaptureConfig = VideoCaptureConfig.Builder().apply {
            setTargetRotation(viewFinder.display.rotation)
        }.build()

        videoCapture = VideoCapture(videoCaptureConfig)
        CameraX.bindToLifecycle(viewLifecycleOwner, preview,videoCapture)
    }

当我尝试更改时:

// Get screen metrics used to setup camera for full screen resolution
       val metrics = DisplayMetrics().also { viewFinder.display.getRealMetrics(it) }
       val screenAspectRatio = Rational(metrics.widthPixels, metrics.heightPixels)
       Log.e(TAG, "Screen metrics: ${metrics.widthPixels} x ${metrics.heightPixels}")

       // Set up the view finder use case to display camera preview
       val viewFinderConfig = PreviewConfig.Builder().apply {
           setLensFacing(lensFacing)
           setTargetAspectRatio(screenAspectRatio)
           setTargetRotation(viewFinder.display.rotation)
       }.build()

至:

// Get screen metrics used to setup camera for full screen resolution

       // Set up the view finder use case to display camera preview
       val viewFinderConfig = PreviewConfig.Builder().apply {
           setLensFacing(lensFacing)
           setTargetRotation(viewFinder.display.rotation)
       }.build()

然后我得到结果:second result

在第二个结果中,它没有按TextureView的宽度缩放。

那么,如何通过TextureView的宽度和高度缩放预览? 谢谢所有亲^^

0 个答案:

没有答案