Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: creating a Bitmap with SurfaceView content

I'm afraid to already have the unfortunate answer to this question but just in case... I'm using a SurfaceView to do some image processing with bitmaps (lights and colors modifications) and I would need to import the modified bitmap (i.e. the content of the SurfaceView) in a new bitmap so that I can save it as an image file.

I've been looking around and it seems that it's possible to get a bitmap from View.getDrawingCache() but it doesn't work with SurfaceView. All I get is an empty bitmap.

Is there any solution to this?

Thanks

like image 282
Nicolas P. Avatar asked Jan 20 '11 02:01

Nicolas P.


3 Answers

For API levels >=24, use the PixelCopy API. Here's an example inside an ImageView:

public void DrawBitmap(){
    Bitmap surfaceBitmap = Bitmap.createBitmap(600, 600, Bitmap.Config.ARGB_8888);
    PixelCopy.OnPixelCopyFinishedListener listener = copyResult -> {
        // success/failure callback
    };

    PixelCopy.request(YourSurfaceView, surfaceBitmap,listener,getHandler());
    // visualize the retrieved bitmap on your imageview
    setImageBitmap(plotBitmap);
    }
}
like image 161
pale bone Avatar answered Sep 28 '22 08:09

pale bone


Can you draw your SurfaceView onto a Canvas that's backed by a Bitmap?

    // be sure to call the createBitmap that returns a mutable Bitmap
    Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); 
    Canvas c = new Canvas(b);
    yourSurfaceView.draw(c);
like image 22
idbrii Avatar answered Sep 28 '22 10:09

idbrii


There is no simple way to capture frames of SurfaceView, so you can consider using TextureView instead of SurfaceView: the Bitmap can be retrieved using textureView.getBitmap() method.

like image 24
Andrew Vovk Avatar answered Sep 28 '22 10:09

Andrew Vovk