【问题标题】:Full Screen Video Recording in Front Camera using camera2 api使用camera2 api在前置摄像头中进行全屏视频录制
【发布时间】:2020-07-04 12:53:15
【问题描述】:

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

我在 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)
} 

有没有办法设置全屏模式而不进行任何拉伸或以正确的纵横比?

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

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】:

    工作了几天。 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-03
      相关资源
      最近更新 更多