Converting a Canvas into Bitmap Image in Android

converting a canvas into bitmap image in android

Advice depends upon what you are trying to do.

If you are concerned that your controls take a long time to draw, and you want to draw to a bitmap so you can blit the bitmap rather than re-drawing via a canvas, then you don't want to be double-guessing the platform - controls automatically cache their drawing to temporary bitmaps, and these can even be fetched from the control using getDrawingCache()

If you want to draw using a canvas to a bitmap, the usual recipe is:

  1. Create a bitmap of the correct size using Bitmap.createBitmap()
  2. Create a canvas instance pointing that this bitmap using Canvas(Bitmap) constructor
  3. Draw to the canvas
  4. Use the bitmap

How to draw on canvas and convert to bitmap?

Found this article may help: http://www.informit.com/articles/article.aspx?p=2143148&seqNum=2

draw.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Bitmap imageBitmap = Bitmap.createBitmap(imageView.getWidth(), imageView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(imageBitmap);
float scale = getResources().getDisplayMetrics().density;
Paint p = new Paint();
p.setColor(Color.BLUE);
p.setTextSize(24*scale);
canvas.drawText("Hello", imageView.getWidth()/2, imageView.getHeight()/2, p);
imageView.setImageBitmap(imageBitmap);
}
});

Android Canvas to Bitmap

Try it this way...

- Create a bitmap of the correct size using Bitmap.createBitmap()

- Create a canvas instance pointing that this bitmap using Canvas(Bitmap) constructor

- Draw to the canvas

- Use the bitmap

Canvas to Bitmap

As suggested Maulik.J, I looked at converting a canvas into bitmap image in android again.

I could not understand from a bit close expression of this link. But, then I saw below text taken from http://developer.android.com/guide/topics/graphics/2d-graphics.html#draw-with-canvas . So, I solved this problem with the help of Maulik.J and the following text:

The Bitmap is always required for a Canvas. You can set up a new Canvas like this:

Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);

Now your Canvas will draw onto the defined Bitmap. After drawing upon it with the Canvas, you can then carry your Bitmap to another Canvas with one of the Canvas.drawBitmap(Bitmap,...) methods. It's recommended that you ultimately draw your final graphics through a Canvas offered to you by View.onDraw() or SurfaceHolder.lockCanvas()

I created a second Canvas object having a bitmap which has same dimensions with my real canvas, I drew all my illustrations on this second temporary canvas. Then, I drew its bitmap on my real canvas.

Thanks.

How to convert canvas to bitmap in android?

Your bitmap is black because you didn't draw anything on it.

To have some drawing in the bitmap you must indeed create it with this call :

bitmap = Bitmap.createBitmap(getDrawingCache());

Then you have a stackoverflow exception simply because :

  • getDrawingCache() call onDraw(Canvas)
  • onDraw(Canvas) call getDrawnMessage(Canvas canvas)
  • getDrawnMessage(Canvas canvas) call getDrawingCache()
  • getDrawingCache() call onDraw(Canvas)
  • ...

So you real problem is the StackOverflow exception.

Solution : don't call getDrawnMessage(Canvas canvas) in the onDraw(Canvas) method.



Related Topics



Leave a reply



Submit