Combining Two Bitmap Image (Side by Side)

How do I merge Bitmap side-by-side

public Bitmap mergeBitmap(Bitmap fr, Bitmap sc) 
{

Bitmap comboBitmap;

int width, height;

width = fr.getWidth() + sc.getWidth();
height = fr.getHeight();

comboBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

Canvas comboImage = new Canvas(comboBitmap);

comboImage.drawBitmap(fr, 0f, 0f, null);
comboImage.drawBitmap(sc, fr.getWidth(), 0f , null);
return comboBitmap;

}

Combining two bitmap image (side by side)

You can use Canvas - check out this article:

http://www.jondev.net/articles/Combining_2_Images_in_Android_using_Canvas

Updated code to do it side by side:

public Bitmap combineImages(Bitmap c, Bitmap s) { // can add a 3rd parameter 'String loc' if you want to save the new image - left some code to do that at the bottom 
Bitmap cs = null;

int width, height = 0;

if(c.getWidth() > s.getWidth()) {
width = c.getWidth() + s.getWidth();
height = c.getHeight();
} else {
width = s.getWidth() + s.getWidth();
height = c.getHeight();
}

cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

Canvas comboImage = new Canvas(cs);

comboImage.drawBitmap(c, 0f, 0f, null);
comboImage.drawBitmap(s, c.getWidth(), 0f, null);

// 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(loc + tmpImg);
cs.compress(CompressFormat.PNG, 100, os);
} catch(IOException e) {
Log.e("combineImages", "problem combining images", e);
}*/

return cs;
}

How to merge to two bitmap one over another programmatically?

try this code sniplet and tweak as per your requirements

 private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, new Matrix(), null);
return bmOverlay;
}

make sure bmp1 is the bigger than bmp2

How can I merge more than two images side by side?

Just write a loop, keeping track of the current width of all the drawn images:

private Bitmap MergeImages(IEnumerable<Image> images)
{
var totalWidth = images.Sum(i => i.Width);
var maxHeight = images.Max(i => i.Height);
Bitmap bitmap = new Bitmap(totalWidth , maxHeight );
var currentXPos = 0;
using (Graphics g = Graphics.FromImage(bitmap))
{
foreach(var image in images)
{
g.DrawImage(image, currentXPos, 0);
currentXPos += image.Width;
}
}

bitmap.MakeTransparent();

return bitmap;
}

How to Place Bitmaps Vertically

I ended up finding a new piece of code here which saves them vertically-

    private Bitmap mergeMultiple(ArrayList<Bitmap> parts) {

int w = 0, h = 0;
for (int i = 0; i < parts.size(); i++) {
if (i < parts.size() - 1) {
w = parts.get(i).getWidth() > parts.get(i + 1).getWidth() ? parts.get(i).getWidth() : parts.get(i + 1).getWidth();
}
h += parts.get(i).getHeight();
}

Bitmap temp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(temp);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
int top = 0;
for (int i = 0; i < parts.size(); i++) {

top = (i == 0 ? 0 : top + parts.get(i).getHeight() + 100);
canvas.drawBitmap(parts.get(i), 0f, top,paint );
}
return temp;

}

Merging Two Images into one Image

If you want to create a side-by-side merged image you will need to create a result bitmap with 2 times the width of first image, or, more scalably, the sum of the widths of the images:

Currently, you are creating a result image with width firstImage.getWidth(). They will clearly overlap or be off the canvas.

Also, you will need to place the second image at x == firstImage.getWidth()

Check out this code (untested):

private Bitmap createSingleImageFromMultipleImages(Bitmap firstImage, Bitmap secondImage) {
Bitmap result = Bitmap.createBitmap(firstImage.getWidth() + secondImage.getWidth(), firstImage.getHeight(), firstImage.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(firstImage, 0f, 0f, null);
canvas.drawBitmap(secondImage, firstImage.getWidth(), 0f, null);
return result;
}

Bitmap mergedImages = createSingleImageFromMultipleImages(firstImage, secondImage);

im.setImageBitmap(mergedImages);


Related Topics



Leave a reply



Submit