问题
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