使用camera2 api在前置摄像头下进行全屏视频录制

3

我陷入了这个问题很多天了。

我在Kotlin中遵循了Android官方相机示例: android's camera-sample

我在Github上提出了一个问题 issue,但在2020年2月11日之后没有收到任何反馈。

我的问题是:

我像样例代码一样使用了前置摄像头,并将 val cameraId = manager.cameraIdList[0] 更改为 val cameraId = manager.cameraIdList[1]。注意:在后置摄像头中不会发生此问题。

前置摄像头无法工作且在以下设备中显示黑色的条纹:

  • 模拟器: Pixel C API 29
  • 设备: Galaxy Tab S2
  • 模式: 竖屏

enter image description here

我想要全屏查看,所以当我不在下面的注释行中设置AutoTextureView的宽高比时,视频将占据整个屏幕,但现在被拉伸了。

if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
  //I only have portrait mode
} else {
  //textureView.setAspectRatio(previewSize.height, previewSize.width)
} 

有没有一种方式可以设置全屏模式,而不会有任何拉伸或正确的纵横比例?
我已经尝试了以下slack上的解决方案,但都无法解决我的问题: Camera 2 : Unable to record video in full screen? Camera2 API Make Preview Fill Entire View Android Camera2 API stretching the preview
1个回答

1

工作了几天后,Camera2全屏预览和图像捕获 帮助我解决了问题。

AutoFitTextureView中的onMeasure设置为:

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    val width = View.MeasureSpec.getSize(widthMeasureSpec)
    val height = View.MeasureSpec.getSize(heightMeasureSpec)
    if (ratioWidth == 0 || ratioHeight == 0) {
        setMeasuredDimension(width, height)
    } else {
        if (width > ((height * ratioWidth) / ratioHeight)) {
            setMeasuredDimension(width, (width * ratioHeight) / ratioWidth)
        } else {
            setMeasuredDimension((height * ratioWidth) / ratioHeight, height)
        }
    }
}

上述代码使屏幕全屏,但存在预览不在中心的问题。
因此,在configureTransform(viewWidth: Int, viewHeight: Int)中进行了以下翻译。
   // adjust the x and y to centre the preview
   val screenWidth = resources.displayMetrics.widthPixels
   val xShift = (viewWidth - screenWidth)/2

   val screenHeight = resources.displayMetrics.heightPixels
   val yShift = (viewHeight - screenHeight)/2
   matrix.setTranslate(-xShift.toFloat(), -yShift.toFloat())

   textureView.setTransform(matrix)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接