Image on canvas to JPEG file

d-man picture d-man · Feb 1, 2010 · Viewed 48.4k times · Source

I'm drawing 2D images on the canvas.

I want to save image shown on canvas to JPEG file, how can I do it?

Answer

Samuh picture Samuh · Feb 1, 2010
  1. create an empty bitmap
  2. create a new Canvas object and pass this bitmap to it
  3. call view.draw(Canvas) passing it the canvas object you just created. Refer Documentation of method for details.
  4. Use Bitmap.compress() to write the contents of the bitmap to an OutputStream, file maybe.

Pseudo code:

Bitmap  bitmap = Bitmap.createBitmap( view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);