相机预览和录制的视频不符合纵横比

问题描述

我正在尝试使用camera2 API录制视频,但是Camera的预览大小和视频与预期的不同。该对象在相机内预览中居中显示,但是在录制视频时,该对象会稍微移动。这些是2张图片,可以更好地说明我的意思

enter image description here

enter image description here

我正在使用以下代码 在打开相机的乐趣中

mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder::class.java),mCameraRecordingWidth!!,mCameraRecordingHeight!!)
        mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture::class.java),width,height,mVideoSize)

fun chooseVideoSize(choices: Array<Size>,configCameraWidth: Int,configCameraHeight: Int): Size {
        for (size in choices) {
            if (configCameraHeight == size.width && configCameraWidth == size.height) {
                return size
            }
        }
        for (size in choices) {
            if (size.width == size.height * 4 / 3 && (size.width <= 1080 || size.width <= configCameraHeight)) {
                return size
            }
        }
     
        return choices[choices.size - 1]
    }

 private fun chooseOptimalSize(choices: Array<Size>,width: Int,height: Int,aspectRatio: Size?): Size {
        val aspectRatioWidth = aspectRatio!!.width
        val aspectRatioHeight = aspectRatio.height
        val bigEnough: MutableList<Size> = ArrayList()
        for (option in choices) {
            if (option.height == option.width * aspectRatioHeight / aspectRatioWidth && option.width >= width && option.height >= height) {
                bigEnough.add(option)
            }
        }
        return if (bigEnough.size > 0) {
            Collections.min(bigEnough,CompareSizesByArea())
        } else {
            choices[0]
        }
    }

我正在关注以下链接:https://github.com/googlearchive/android-Camera2Video 并使用AutoFitTextureView

 override fun onMeasure(widthMeasureSpec: Int,heightMeasureSpec: Int) {
    super.onMeasure(widthMeasureSpec,heightMeasureSpec)
    val width = MeasureSpec.getSize(widthMeasureSpec)
    val height = MeasureSpec.getSize(heightMeasureSpec)
    if (0 == mRatioWidth || 0 == mRatioHeight) {
        setMeasuredDimension(width,height)
    } else {
        if (width < height * mRatioWidth / mRatioHeight) {
            setMeasuredDimension(height * mRatioWidth / mRatioHeight,height)
        } else {
            setMeasuredDimension(width,width * mRatioHeight / mRatioWidth)
        }
    }
}

任何想法/解决方案对我来说都更为重要。非常感谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...