Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

combining two png files in android

Tags:

android

image

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?

like image 547
John Avatar asked Apr 29 '10 16:04

John


1 Answers

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 = BitmapFactory.decodeFile("myFirstPNG.png");
Bitmap topImage = BitmapFactory.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);

// comboImage 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");
    comboImage.compress(CompressFormat.PNG, 50, os)
} catch(IOException e) {
    e.printStackTrace();
}

EDIT :

there was a typo, So, I've changed

image.compress(CompressFormat.PNG, 50, os)

to

bottomImage.compress(CompressFormat.PNG, 50, os)

like image 94
Ehxor Avatar answered Oct 22 '22 21:10

Ehxor