Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android merge two images

Tags:

android

I have these two images, which I basically merge on canvas. Now i want to save that canvas into an image. How should i do it or if there is any other way to merge two images.

My sample code is -

            Bitmap bmp1 = BitmapFactory.decodeResource(getResources(),
                R.drawable.duckpic);
        Bitmap bmp2 = BitmapFactory.decodeResource(getResources(),
                R.drawable.img);
        // canvas.drawColor(Color.BLACK);
        // canvas.drawBitmap(_scratch, 10, 10, null);
        Bitmap bmOverlay = Bitmap.createBitmap(bmp2.getWidth(), bmp2
                .getHeight(), bmp2.getConfig());
        // Canvas cs = new Canvas(bmp2);
        canvas.scale((float) 0.5, (float) 0.5);
        canvas.drawBitmap(bmp2, new Matrix(), null);
        canvas.drawBitmap(bmp1, new Matrix(), null);
        canvas.save();

I got it working by doing this -

    cs = Bitmap.createBitmap(c.getWidth(), c.getHeight(), Bitmap.Config.ARGB_8888);

    Canvas comboImage = new Canvas(cs);

    comboImage.drawBitmap(s, new Matrix(), null);
    comboImage.drawBitmap(c, new Matrix(), null);
    comboImage.save();
    // this is an extra bit I added, just incase you want to save the new
    // image somewhere and then return the location

    String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png";

    OutputStream os = null;
    try {
        os = new FileOutputStream("/sdcard/" + tmpImg);
        cs.compress(CompressFormat.PNG, 100, os);
    } catch (IOException e) {
        Log.e("combineImages", "problem combining images", e);
    }

Basically it is given here - http://www.jondev.net/articles/Combining_2_Images_in_Android_using_Canvas

like image 682
nasaa Avatar asked May 25 '11 19:05

nasaa


People also ask

How do I paste a picture into another picture on android?

Select what you want to copy. Tap Copy. Touch & hold where you want to paste. Tap Paste.

Is there an app to merge two pictures together?

Union is the latest photography app by Pixite that can merge multiple images into one artistic amalgamation. The app uses masks similar to more robust image editors like Photoshop and GIMP that lets you edit and blend images together.


1 Answers

Use canvas.setBitmap(Bitmap bitmap). This will send the canvas to the specified Bitmap. You'll want to create a new, mutable bitmap for this. After you call setBitmap you can then save that Bitmap to a file.

like image 189
Haphazard Avatar answered Sep 21 '22 22:09

Haphazard