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

问题描述 投票:1回答:1

我在这个问题上困扰了好几天。

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

[我于2020年2月11日在github issue上提出了一个问题,但未收到任何反馈。

我的问题是:

我按原样使用了样本,仅将前置摄像机的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)
} 

有没有一种方法可以设置全屏模式而不进行任何拉伸或以正确的宽高比进行?

我一直在通过以下松弛解决方案,但没有一个对我有用:

Camera 2 : Unable to record video in full screen?

Camera2 API Make Preview Fill Entire View

Android Camera2 API stretching the preview

android kotlin fullscreen android-camera2 stretching
1个回答
0
投票

[工作几天后。 Camera2 full screen preview and image capture帮助我解决了问题。

onMeasure中的AutoFitTextureView设置为:

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)
© www.soinside.com 2019 - 2024. All rights reserved.