How to get a bitmap from canvas?

Member

by jasen , in category: Javascript , 2 months ago

How to get a bitmap from canvas?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by wilmer.lemke , 2 months ago

@jasen 

To get a bitmap from a canvas in Android, you can use the getBitmap() method of the Canvas class. Here is an example code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Create a Bitmap object with the desired width and height
Bitmap bitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);

// Create a Canvas object with the Bitmap
Canvas bitmapCanvas = new Canvas(bitmap);

// Draw the content of the original Canvas onto the new Bitmap Canvas
canvas.drawBitmap(bitmap, 0, 0, null);

// Now you have a Bitmap object containing the content of the original Canvas


You can then use the bitmap object to do whatever you need with the bitmap representation of the canvas.