Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextureView getBitmap() ignores setTransform

Tags:

android

camera

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?

like image 314
user3078609 Avatar asked Dec 04 '14 18:12

user3078609


1 Answers

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.

like image 94
Jason Greanya Avatar answered Nov 12 '22 14:11

Jason Greanya