TextureView getBitmap()忽略setTransform

时间:2014-12-04 18:08:40

标签: android camera

我正在使用TextureView进行CameraPreview。由于显示比率和预览比率之间的差异,我使用textureView.setTransform(matrix)中的onSurfaceTextureAvailable()来缩放预览。当我需要textureView的屏幕截图时,我使用textureView.getBitmap(),但在某些型号的智能手机中,getBitmap()会忽略预览的缩放。为什么会这样?

1 个答案:

答案 0 :(得分:2)

当我需要在显示之前对相机预览进行后期处理时,我发现了同样的问题。

看起来原始相机捕获是getBitmap()中出现的内容,而不是最终显示的转换后的视图。

我解决了以下问题,它只是在拔出Bitmap后应用相同的转换:

TextureView textureView = (TextureView) findViewById( R.id.texture );
Matrix m = new Matrix();
// Do matrix creation here.
textureView.setTransform( m );

// When you need to get the Bitmap
Bitmap bitmap = textureView.getBitmap();
bitmap = Bitmap.createBitmap( bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), textureView.getTransform( null ), true );

我注意到Bitmap没有像在原始视图中那样被裁剪。在我的例子中,TextureView已经裁剪了图像的顶部和底部,而Bitmap仍然是全尺寸的。基本上,它看起来像TextureView使用相当于" centerCrop"用于默认显示原始源。我将相应的ImageView设置为使用android:scaleType="centerCrop",并且视图具有相同的方面。

当然,这意味着我正在处理更多我真正需要的图像,但我还没有弄清楚如何在处理之前精确地裁剪出TextureView正在做的事情。

相关问题