3

我已经被这个问题困了好几天了。

我在 Kotlin 中关注了这个 Android 的官方相机示例: android's camera-sample

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

我的问题是:

我按原样使用示例,仅更改val cameraId = manager.cameraIdList[0]val cameraId = manager.cameraIdList[1]前置摄像头。注意:后置摄像头不会发生这种情况。

前置摄像头不工作,并在测试设备上显示黑条:

  • 模拟器:像素 C API 29
  • 设备:Galaxy Tab S2
  • 模式:人像

在此处输入图像描述

我想要一个全屏视图,所以当我没有AutoTextureView在下面的注释行中设置纵横比时,视频会全屏显示,但现在被拉伸了。

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

有没有办法在没有任何拉伸或正确纵横比的情况下设置全屏模式?

我在松弛中经历了以下解决方案,但没有一个对我有用:

相机 2:无法全屏录制视频?

Camera2 API 使预览填充整个视图

Android Camera2 API 拉伸预览

4

1 回答 1

1

经过几天的工作。Camera2 全屏预览和图像捕获帮助我解决了这个问题。

设置onMeasureAutoFitTextureView

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)
于 2020-04-07T06:14:44.807 回答