15

I am working on a distributed application of android.I have splitted a single image into lets say 4 parts and then processed it. Now I want to combine 4 bitmap images into a single image. How can i do that?

vinesh
  • 4,425
  • 5
  • 39
  • 45
  • http://stackoverflow.com/questions/6944061/android-merge-two-images Hope this will help you – RAAAAM Mar 01 '13 at 06:52
  • Possible duplicate of [overlay two images in android to set an imageview](http://stackoverflow.com/questions/2739971/overlay-two-images-in-android-to-set-an-imageview) – GSerg Apr 09 '16 at 16:47

3 Answers3

23
Bitmap[] parts = new Bitmap[4];
Bitmap result = Bitmap.createBitmap(parts[0].getWidth() * 2, parts[0].getHeight() * 2, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
for (int i = 0; i < parts.length; i++) {
    canvas.drawBitmap(parts[i], parts[i].getWidth() * (i % 2), parts[i].getHeight() * (i / 2), paint);
}

Something like this =)

Gowtham
  • 11,143
  • 12
  • 43
  • 62
Anton
  • 507
  • 3
  • 11
7

Following piece of code will do the trick for you to combine four bitmaps in one. Call this method 3 times to combine the four images.

Step 1:Combine first two images

Step 2:Combine the renaming two images

Step 3:Combine the the two new created bitmaps

public static 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, 0, 0, null);
        return bmOverlay;
    }
Karan_Rana
  • 2,839
  • 2
  • 24
  • 34
1

You need to create a function of bitmap type. That is, it returns a bitmap data type. The function should have an argument of data type Bitmap which is an array.

Download demo here

You will pass your images to the function as array of bitmap. This is our function to merge not only four images but any size of images.

private Bitmap mergeMultiple(Bitmap[] parts){

    Bitmap result = Bitmap.createBitmap(parts[0].getWidth() * 2, parts[0].getHeight() * 2, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    Paint paint = new Paint();
    for (int i = 0; i < parts.length; i++) {
        canvas.drawBitmap(parts[i], parts[i].getWidth() * (i % 2), parts[i].getHeight() * (i / 2), paint);
    }
        return result;
    }

Finally you are done.. Read more here

Daniel Nyamasyo
  • 2,278
  • 1
  • 22
  • 23