Saving canvas to bitmap on Android
Asked Answered
V

5

7

I'm having some difficulty with regards to placing the contents of a Canvas into a Bitmap. When I attempt to do this, the file gets written with a file size of around 5.80KB but it appears to be completely empty (every pixel is '#000').

The canvas draws a series of interconnected lines that are formed by handwriting. Below is my onDraw for the View. (I'm aware that it's blocking the UI thread / bad practices/ etc.., however I just need to get it working)

Thank you.

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);

        if (IsTouchDown) {

            // Calculate the points
            Path currentPath = new Path();
            boolean IsFirst = true;
            for(Point point : currentPoints){
                if(IsFirst){
                    IsFirst = false;
                        currentPath.moveTo(point.x, point.y);
                    } else {
                        currentPath.lineTo(point.x, point.y);
                    }
                }

            // Draw the path of points
            canvas.drawPath(currentPath, pen);

            // Attempt to make the bitmap and write it to a file.
            Bitmap toDisk = null;
            try {

                // TODO: Get the size of the canvas, replace the 640, 480
                toDisk = Bitmap.createBitmap(640,480,Bitmap.Config.ARGB_8888);
                canvas.setBitmap(toDisk);
                toDisk.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("arun.jpg")));

            } catch (Exception ex) {


            }

        } else {

            // Clear the points
            currentPoints.clear();

        }
    }
Valvular answered 21/2, 2013 at 11:46 Comment(0)
S
13

I had similar problem and i've got solution. Here full code of a task /don't forget about android.permission.WRITE_EXTERNAL_STORAGE permission in manifest/

  public Bitmap saveSignature(){

      Bitmap  bitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.ARGB_8888);
      Canvas canvas = new Canvas(bitmap);
      this.draw(canvas); 

      File file = new File(Environment.getExternalStorageDirectory() + "/sign.png");

      try {
           bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(file));
      } catch (Exception e) {
           e.printStackTrace();
      }

      return bitmap;
  }
Silkaline answered 26/11, 2013 at 21:27 Comment(2)
getting blank pngCalycine
me to get blank image. Any solution for this problem??Piet
S
4

first create a blank bitmap , then create a canvas with that blank bitmap

 Bitmap.Config conf = Bitmap.Config.ARGB_8888; 
 Bitmap bitmap_object = Bitmap.createBitmap(width, height, conf); 
 Canvas canvas = new Canvas(bitmap_object);

now draw your lines on canvas

       Path currentPath = new Path();
        boolean IsFirst = true;
        for(Point point : currentPoints){
            if(IsFirst){
                IsFirst = false;
                    currentPath.moveTo(point.x, point.y);
                } else {
                    currentPath.lineTo(point.x, point.y);
                }
            }

        // Draw the path of points
        canvas.drawPath(currentPath, pen);

Now access your bitmap via bitmap_object

Synchrotron answered 21/2, 2013 at 21:37 Comment(0)
L
3

You'll have to draw after setting the bitmap to the canvas. Also use a new Canvas object like this:

Canvas canvas = new Canvas(toDisk);
canvas.drawPath(currentPath, pen);
toDisk.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File("arun.png")));

I recommend using PNG for saving images of paths.

Licentious answered 21/2, 2013 at 12:1 Comment(4)
Thanks for the feedback, if this operation was to be taken out of the onDraw() method, using an Thread or AsyncTask or something, how would the onDraw(Canvas) canvas object get passed through to them? So that I can apply the canvas.setBitmap() routine? Thanks.Valvular
@DheerajV.S. I have the same thing: created a canvas with a bitmap and drawn a path. It gets displayed properly (using ImageView.setImageBitmap) but Bitmap.compress returns null and a invalid file is created.Mckinnon
do you find out any solution @vedant1811Quake
did youy find any solution @ user1185687Quake
A
3

you must call canvas.setBitmap(bitmap); before drawing anything on Canvas. After calling canvas.setBitmap(bitmap); draw on Canvas and then save the Bitmap you passed to Canvas.

Armstrong answered 21/2, 2013 at 12:3 Comment(1)
Hi there, thanks for the feedback. I've tried this to no success as I keep getting an UnsupportedOperationException when I apply this. Screenshot: i.imgur.com/eHHzY5Z.png. This must be done in the onDraw method right? As how else could you get the 'canvas' handle?Valvular
D
0

May be

canvas.setBitmap(toDisk);

is not in correct place.

Try this :

toDisk = Bitmap.createBitmap(640,480,Bitmap.Config.ARGB_8888);              
toDisk.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("arun.jpg")));

canvas.setBitmap(toDisk);
Delegacy answered 21/2, 2013 at 11:56 Comment(1)
Nope, unfortunately it did not work. Thanks for the tip though.Valvular

© 2022 - 2024 — McMap. All rights reserved.