我正在使用的设备中的相机返回旋转的图像(横向16:9)。为了获取流,我使用Camera2 API,对于投影,我使用TextureView。 TextureView
是固定尺寸。当我尝试使用Matrix
对其进行旋转时,会导致裁切图像。
这是轮换前我所拥有的:
然后旋转它并像这样调整TextureView
大小:
private fun updateTransform() {
val matrix = Matrix()
val centerX = textureView.width / 2f
val centerY = textureView.height / 2f
matrix.postRotate(90.toFloat(), centerX, centerY)
textureView.setTransform(matrix)
textureView.layoutParams = FrameLayout.LayoutParams(textureView.width, textureView.height)
}
这将产生以下图像:
我希望将图像拉伸以填充TextureView
。需要注意的重要一点是,我不需要考虑设备旋转,因为它始终是纵向的。我也只为一台设备编程,所以我不需要考虑其他相机类型等。
如何在不裁剪的情况下旋转图像?
此转换解决了我的问题:
private fun transformTexture() {
val adjustment = Matrix()
val centerX = textureView.width / 2f
val centerY = textureView.height / 2f
val scalex = textureView.width.toFloat() / textureView.height.toFloat()
val scaley = textureView.height.toFloat() / textureView.width.toFloat()
adjustment.postRotate(90F, centerX, centerY)
adjustment.postScale(scalex, scaley, centerX, centerY)
textureView.setTransform(adjustment)
}