我在这个问题上困扰了好几天。
[我在Kotlin遵循了Android的官方相机示例:android's camera-sample
[我于2020年2月11日在github issue上提出了一个问题,但未收到任何反馈。
我的问题是:
我按原样使用了样本,仅将前置摄像机的val cameraId = manager.cameraIdList[0]
更改为val cameraId = manager.cameraIdList[1]
。注意:在后置摄像头中不会发生。
前置摄像头无法正常工作,并在其上显示黑条测试过的设备:
我想要全屏显示,因此当我在下面的注释行中未设置AutoTextureView
的宽高比时,视频将全屏显示,但现在被拉伸了。
if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//I only have portrait mode
} else {
//textureView.setAspectRatio(previewSize.height, previewSize.width)
}
有没有一种方法可以设置全屏模式而不进行任何拉伸或以正确的宽高比进行?
我一直在通过以下松弛解决方案,但没有一个对我有用:
Camera 2 : Unable to record video in full screen?
[工作几天后。 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)