views:

629

answers:

4

I have two png image files that I would like my android app to combine programmatically into one png image file and am wondering if it is possible to do so? if so, what I would like to do is just overlay them on each other to create one file.

the idea behind this is that I have a handful of png files, some with a portion of the image on the left with the rest transparent and the others with an image on the right and the rest transparent. and based on user input it will combine the two to make one file to display. (and i cant just display the two images side by side, they need to be one file)

is this possible to do programmatically in android and how so?

+1  A: 

You can do blending. This is not particular to Android. It's just universal image processing.

EDIT:

You may find these articles & samples & code useful:

http://www.jhlabs.com/ip/

http://kfb-android.blogspot.com/2009/04/image-processing-in-android.html

http://code.google.com/p/jjil/

http://stackoverflow.com/questions/2043019/image-processing-on-android

Viet
but does android support blending and how so?
John
Thanks for the links :)
John
A: 

You may wish to look into the Canvas object, which would make it easy to do other drawing operations as well. You can just draw your bitmaps onto a canvas where you want them, then save the resulting bitmap.

Steve Pomeroy
A: 

If they have transparent sections, then if you draw one on top of the other, only the non-transparent portions will overlap. It will be up to you to arrange the bitmaps however you like.

For the separate issue of re-saving your image to a png, use bitmap.compress().

Brad Hein
+1  A: 

I've been trying to figure this out for a little while now.

Here's (essentially) the code I used to make it work.

// Get your images from their files
Bitmap bottomImage = new BitmapFactory.decodeFile("myFirstPNG.png");
Bitmap topImage = new BitmapFactor.decodeFile("myOtherPNG.png");

// As described by Steve Pomeroy in a previous comment, 
// use the canvas to combine them.
// Start with the first in the constructor..
Canvas comboImage = new Canvas(bottomImage);
// Then draw the second on top of that
comboImage.drawBitmap(topImage, 0f, 0f, null);

// bottomImage is now a composite of the two. 

// To write the file out to the SDCard:
OutputStream os = null;
try {
    os = new FileOutputStream("/sdcard/DCIM/Camera/" + "myNewFileName.png");
    image.compress(CompressFormat.PNG, 50, os)
} catch(IOException e) {
    e.printStackTrace();
}
Second