【问题标题】:Rotating camera stream with TextureView crops the image使用 TextureView 旋转相机流裁剪图像
【发布时间】:2019-10-09 12:38:39
【问题描述】:

我正在使用的设备中的相机返回旋转图像(横向 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。需要注意的重要一点是,我不需要考虑设备旋转,因为它总是纵向的。我也只为一台设备编程,所以我不需要考虑不同的相机类型等。

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

【问题讨论】:

    标签: android kotlin android-camera2 textureview


    【解决方案1】:

    这个转换解决了我的问题:

        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)
    }
    

    【讨论】:

      猜你喜欢
      • 2013-06-05
      • 1970-01-01
      • 2014-10-21
      • 2015-02-18
      • 2021-10-01
      • 2016-12-27
      • 2011-12-09
      • 2015-08-23
      • 1970-01-01
      相关资源
      最近更新 更多