使用TextureView旋转相机流裁剪图像

问题描述 投票:0回答:1

我正在使用的设备中的相机返回旋转的图像(横向16:9)。为了获取流,我使用Camera2 API,对于投影,我使用TextureViewTextureView是固定尺寸。当我尝试使用Matrix对其进行旋转时,会导致裁切图像。

这是轮换前我所拥有的:

enter image description here

然后旋转它并像这样调整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)
}

这将产生以下图像:

enter image description here

我希望将图像拉伸以填充TextureView。需要注意的重要一点是,我不需要考虑设备旋转,因为它始终是纵向的。我也只为一台设备编程,所以我不需要考虑其他相机类型等。

如何在不裁剪的情况下旋转图像?

android kotlin android-camera2 textureview
1个回答
0
投票

此转换解决了我的问题:

    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)
}
© www.soinside.com 2019 - 2024. All rights reserved.