Emkey
Emkey

Reputation: 5356

Make image as canvas background and save it with user's drawing

I'm using the code below. My app was able to draw on the canvas and save it.

But what I want to do is make an image as the background of the canvas so when I save it, it will look like an image with the user's drawing on top of it.

Thank you so much for any help! :)

@Override
public void run() {
    Canvas canvas = null;
    while (_run){
        if(isDrawing == true){
            try{
                canvas = mSurfaceHolder.lockCanvas(null);
                if(mBitmap == null){
                    mBitmap =  Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
                }
                final Canvas c = new Canvas (mBitmap);

                c.drawColor(0, PorterDuff.Mode.CLEAR);
                canvas.drawColor(0, PorterDuff.Mode.CLEAR);
                canvas.drawColor(0xffffffff);

                commandManager.executeAll(c,previewDoneHandler);
                previewPath.draw(c);

                canvas.drawBitmap (mBitmap, 0,  0,null);
            } finally {
                mSurfaceHolder.unlockCanvasAndPost(canvas);
            }


        }

    }

}

Upvotes: 0

Views: 2449

Answers (2)

Lalit Poptani
Lalit Poptani

Reputation: 67286

Try this stuff,

This will return a Bitmap that will be a Merged one of two Bitmap Images, also it will save in the SDCard.

public Bitmap combineImages(Bitmap c, Bitmap s) {

        Bitmap cs = null;
        int width, height = 0;

        if (c.getWidth() > s.getWidth()) {
            width = c.getWidth();
            height = c.getHeight();
        } else {
            width = s.getWidth() + s.getWidth();
            height = c.getHeight();
        }

        cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        Canvas comboImage = new Canvas(cs);

        comboImage.drawBitmap(c, 0, 0, null);
        comboImage.drawBitmap(s, 100, 300, null);

        /******
         * 
         *   Write file to SDCard
         * 
         * ****/

        String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png";
        OutputStream os = null;
        try {
            os = new FileOutputStream(Environment.getExternalStorageDirectory()
                    + "/"+tmpImg);
            cs.compress(CompressFormat.PNG, 100, os);
        } catch (IOException e) {
            Log.e("combineImages", "problem combining images", e);
        }
        return cs;
    }

Upvotes: 2

rDroid
rDroid

Reputation: 4945

For any view that you are creating, you can create a bitmap of what it is currently displaying. use:

view.setDrawingCacheEnabled(true);
Bitmap bitmap=view.getDrawingCache();

Does this help you in achieving what you want ? *be sure to recycle these bitmaps when you are done.

bitmap.recycle();

Upvotes: 0

Related Questions