Hy, I tried to search this but not very much luck the most similar is this one http://ketankantilal.blogspot.com/2011/03/how-to-combine-images-and-store-to.html Anyhow, I'm developing for android. The question is I have images in png format (or jpg as bmp is quite large for my app). How can I combine three images from top to bottom. I do not need to save them on sd just to display them. Thanks, and sorry if a similar question with answer exists.
Asked
Active
Viewed 1,100 times
0
-
1What exactly do you mean with "combine from top to bottom"? – User May 17 '12 at 17:10
-
Yes, this could mean one of any number of things. Image manipulation is a complex subject with a large vocabulary. You should figure out how to express this specific task using that vocabulary so we will know exactly what you are trying to do. – mtmurdock May 17 '12 at 18:04
1 Answers
2
You could use a Canvas and then draw each Bitmap (assuming each image is loaded into a Bitmap object) using appropriate top and left offsets.
You would increase the top offset of the next bitmap by the total size of the previously drawn Bitmaps.
Check out http://developer.android.com/reference/android/graphics/Canvas.html
example:
public void stackImages(Context ctx)
{
// base image, if new images have transparency or don't fill all pixels
// whatever is drawn here will show.
Bitmap result = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
// b1 will be on top
Bitmap b1 = Bitmap.createBitmap(400, 200, Bitmap.Config.ARGB_8888);
// b2 will be below b1
Bitmap b2 = Bitmap.createBitmap(400, 200, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(result);
c.drawBitmap(b1, 0f, 0f, null);
// notice the top offset
c.drawBitmap(b2, 0f, 200f, null);
// result can now be used in any ImageView
ImageView iv = new ImageView(ctx);
iv.setImageBitmap(result);
// or save to file as png
// note: this may not be the best way to accomplish the save
try {
FileOutputStream out = new FileOutputStream(new File("some/file/name.png"));
result.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
}

soost
- 91
- 3
-
Note, that this will be a 'last on top' method, meaning that unless the images have transparency in them, which ever image is drawn last will appear on top. This isn't wrong if its what you want, but if you're looking for a blend of the images, this probably wont work. Image manipulation is a very complex subject, and you need to carefully define what you want your result to be before you start into the code side of things. – mtmurdock May 17 '12 at 17:28
-
this is why you supply the top offset, it will shift the bitmap down that many pixels. For example, if your first image is 50 pixels in height, your next bitmap should be drawn with top = 50. If the second if 25 pixels in height, the 3rd would be given top = 75, etc. etc. you can achieve this with the Canvas.drawBitmap(Bitmap bitmap, float left, float top, Paint paint) method – soost May 17 '12 at 17:34
-
I'm sorry, I read "top to bottom" as stacked on top of each other, not arranged vertically. – mtmurdock May 17 '12 at 17:36
-
the thing I want to do is lets say I have two png 400x200 files, I want to create a whole new 400x400 png by stacking one over the other. Food for thought here, could I use this canvas to save this file as bmp and then to png. And could I first change the png to bmp then to canvas and then from canvas export it to bmp and then to png???? – user1398593 May 17 '12 at 20:55
-
I'm really sorry here, it appears that this really is the answer. Okay the question now would be how do I use the canvas. How can I (let's say) use the picture that is in the canvas as a background on a layer???? Or as an image on top of an Image View???? – user1398593 May 17 '12 at 21:36
-
The initial bitmap passed to the canvas constructor will be your end result. So it would need to be the size of the bitmap you want to end with. When you do your modifications to the canvas, you can use the passed bitmap just like any other bitmap. I would look at this question: http://stackoverflow.com/questions/649154/android-bitmap-save-to-location . There is no need to convert to bmp, you can save it directly as PNG from the Bitmap object. You can initialize your Bitmap with any of the image formats you've discussed though. – soost May 18 '12 at 13:36