本文介绍了TextureView getBitmap()忽略setTransform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将TextureView用于CameraPreview。由于显示比例和预览比例之间的差异,我在 onSurfaceTextureAvailable()中使用 textureView.setTransform(matrix)用于缩放预览。当我需要TextureView的屏幕快照时,我使用 textureView.getBitmap(),但是在某些智能手机型号中,使用 getBitmap()忽略预览的缩放。为什么会发生?

解决方案

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

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



我通过以下方法解决了这个问题,该问题仅在拔出位图后才应用相同的变换:

  TextureView textureView =(TextureView)findViewById(R.id.texture); 
Matrix m = new Matrix();
//在此处创建矩阵。
textureView.setTransform(m);

//需要获取位图时
位图bitmap = textureView.getBitmap();
bitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),textureView.getTransform(null),true);

我确实注意到,位图没有像原始视图中那样被裁剪。就我而言,TextureView一直在裁剪图像的顶部和底部,而Bitmap仍然是全尺寸的。基本上,默认情况下,TextureView看起来使用 centerCrop的等效项来显示原始源。我将对应的ImageView设置为使用 android:scaleType = centerCrop ,并且视图具有相同的外观。



当然,这意味着我要处理更多我真正需要的图像,但是我还没有弄清楚如何在处理之前完全裁剪出TextureView正在做什么。


I'm using a TextureView for CameraPreview. Because of a difference between the display ratio and the preview ratio I use textureView.setTransform(matrix) in onSurfaceTextureAvailable() for scaling the preview. When I need a screenshot of the textureView, I use textureView.getBitmap(), but in some models of smartphones, getBitmap() ignores scaling of the preview. Why does it happen?

解决方案

I discovered this same issue when I needed to post-process a camera preview before displaying it.

It looks like the original camera capture is what comes out in getBitmap() rather than the transformed view that ends up being displayed.

I worked around the problem with the following, which just applies the same transform after the Bitmap is pulled out:

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

I did notice that the Bitmap isn't cropped like it may be in the original view. In my case, the TextureView had been cropping the top and bottom of the image, while the Bitmap was still full sized. Basically, it looks like TextureView uses the equivalent of "centerCrop" for display of the original source by default. I set my corresponding ImageView to use android:scaleType="centerCrop" and the views have the same aspect.

Of course, this means I'm processing more of the image that I really need to, but I haven't worked out how to crop out exactly what the TextureView is doing before processing yet.

这篇关于TextureView getBitmap()忽略setTransform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 22:03