Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 views
in Technique[技术] by (71.8m points)

image - combining two png files in android

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...