VideoCapturer.startCamera(width, height, fps) 裁剪不同宽度、高度的相机 android

问题描述

我想在 Android 应用程序中打开我的相机并捕获视频以进行视频通话。 首先,我用这个创建了一个 videoCapturer

private fun createCameraCapturer(): VideoCapturer? {
        val enumerator: CameraEnumerator = if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            Camera2Enumerator(context)
        } else {
            Camera1Enumerator(false)
        }

        // Try to find front facing camera
        enumerator.deviceNames.forEach {
            if (enumerator.isFrontFacing(it)) {
                videoCapturer = enumerator.createCapturer(it,null)
                if (videoCapturer != null) return videoCapturer
            }
        }


        // Front facing camera not found,try something else
        enumerator.deviceNames.forEach {
            if (!enumerator.isFrontFacing(it)) {
                videoCapturer = enumerator.createCapturer(it,null)
                if (videoCapturer != null) return videoCapturer
            }
        }

        return null
    }

在我得到一个非空的 videoCapturer 后,我开始使用给定的宽度、高度和 fps 的 cameraCapture。

videoCapturer?.initialize(surfaceTextureHelper,context,videoSource?.capturerObserver)
videoCapturer?.startCapture(width,height,fps)

宽度和高度的可能组合如下

0 -> VideoHeader(fps: 5,height: 128,width: 96
1 -> VideoHeader(fps: 5,height: 320,width: 240
2 -> VideoHeader(fps: 10,height: 480,width: 360
3 -> VideoHeader(fps: 10,height: 640,width: 480
4 -> VideoHeader(fps: 15,height: 960,width: 720

我注意到根据 VideoHeader 在 UI 视图中获得的视频中有一个裁剪。例如,如果我有这样的情况:0 那么参与者被裁剪在他脸部的右上方。就像有一个变焦什么的。 有什么办法可以解决这个问题吗?

解决方法

在 SurfaceViewRenderer 实例上使用以下代码:

surfaceviewrenderer.setScaleType(Scaletofit)

这不会对流产生缩放效果,也不会裁剪流。 您可以根据需要玩转setScaleType