Full Screen Video Recording in Front Camera using camera2 api

倾然丶 夕夏残阳落幕 提交于 2020-05-12 02:52:15

问题


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


回答1:


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)


来源:https://stackoverflow.com/questions/60824076/full-screen-video-recording-in-front-camera-using-camera2-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!