使用camera2 API在前置摄像头中进行全屏视频录制 [英] Full Screen Video Recording in Front Camera using camera2 api

查看:625
本文介绍了使用camera2 API在前置摄像头中进行全屏视频录制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我在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
  • 模式:人像

我想要全屏视图,因此当我在下面的注释行中未设置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扩展了预览

解决方案

工作几天后. Camera2全屏预览和图像捕获帮助我解决了这个问题. /p>

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)

I have been stuck in this issue for days.

I followed this Android's official camera-sample in Kotlin: android's camera-sample

I raised an issue on github issue on 11 Feb 2020 but haven't received any feedback.

My problem is:

I used the sample as it is and only changed val cameraId = manager.cameraIdList[0] to val cameraId = manager.cameraIdList[1] for front camera. NOTE: It does not happen in rear camera.

The front camera does not work and shows black bar on devices tested:

  • Emulator: Pixel C API 29
  • Device: Galaxy Tab S2
  • Mode: Portrait

I wanted a full screen view, so when I don't set the aspect ratio of AutoTextureView in the commented line below, the video takes full screen but is now stretched.

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

Is there a way to set full screen mode without any stretching or in a correct aspect ratio?

I have been through following solutions in slack and none worked for me:

Camera 2 : Unable to record video in full screen?

Camera2 API Make Preview Fill Entire View

Android Camera2 API stretching the preview

解决方案

After working for days. Camera2 full screen preview and image capture helped me solve the problem.

Setting onMeasure in AutoFitTextureView as:

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

Above code makes the screen full size but had problem of preview not being at the centre

So I translated as follows in 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)

这篇关于使用camera2 API在前置摄像头中进行全屏视频录制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆