combining two png files in android

Solution 1:

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)

Solution 2:

You can do blending. This is not particular to Android. It's just universal image processing.

EDIT:

You may find these articles & samples & code useful:

http://www.jhlabs.com/ip/

http://kfb-android.blogspot.com/2009/04/image-processing-in-android.html

http://code.google.com/p/jjil/

Image Processing on Android

Solution 3:

I use this code

private class PhotoComposition extends AsyncTask<Object, Void, Boolean> {
    private String pathSave;//path save combined images

    @Override
    protected Boolean doInBackground(Object... objects) {

      List<String> images = (List<String>) objects[0]; //lsit of path iamges
      pathSave = (String) objects[1];//path save combined images
      if (images.size() == 0) {
        return false;
      }
      List<Bitmap> bitmaps = new ArrayList<>();
      for (int i = 0; i < images.size(); i++) {
        bitmaps.add(BitmapFactory.decodeFile( images.get(i)));
      }
      int width = findWidth(bitmaps);//Find the width of the composite image
      int height = findMaxHeight(bitmaps);//Find the height of the composite image

      Bitmap combineBitmap  = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);//create bitmap of composite image

      combineBitmap.eraseColor(Color.parseColor("#00000000")); //bcakgraound color of composite image

      Bitmap mutableCombineBitmap = combineBitmap.copy(Bitmap.Config.ARGB_8888, true);//create mutable bitmap to create canvas

      Canvas canvas = new Canvas(mutableCombineBitmap);// create canvas to add bitmaps

      float left = 0f;

      for (int i = 0; i < bitmaps.size(); i++) {
        canvas.drawBitmap(bitmaps.get(i), left, 0f, null);//Taking photos horizontally

        left += bitmaps.get(i).getWidth();//Take right to the size of the previous photo
      }

      OutputStream outputStream = null;
      try {
        outputStream = new FileOutputStream(pathSave);//path of save composite image
        mutableCombineBitmap.compress(Bitmap.CompressFormat.PNG, 80, outputStream);
      } catch (IOException e) {
        e.printStackTrace();
        return false;
      }
      return true;
    }


    @Override
    protected void onPostExecute(Boolean isSave) {
      if (isSave) {
        //iamge save on pathSave
        Log.i("PhotoComposition", "onPostExecute: " + pathSave);
      }
      super.onPostExecute(isSave);
    }

    private int findMaxHeight(List<Bitmap> bitmaps) {
      int maxHeight = Integer.MIN_VALUE;
      for (int i = 0; i < bitmaps.size(); i++) {
        if (bitmaps.get(i).getHeight() > maxHeight) {
          maxHeight = bitmaps.get(i).getHeight();
        }
      }
      return maxHeight;
    }

    private int findWidth(List<Bitmap> bitmaps) {
      int width = 0;
      for (int i = 0; i < bitmaps.size(); i++) {
        width += bitmaps.get(i).getWidth();
      }
      return width;
    }

USAGE

List<String> images = new ArrayList<>();
    images.add("/storage/emulated/0/imageOne.png");//path of image in storage
    images.add("/storage/emulated/0/imageTwo.png");
//   images.add("/storage/emulated/0/imageThree");
//   ... //add more images
    String pathSaveCombinedImage = "/storage/emulated/0/CombinedImage.png";//path save result image
  new PhotoComposition().execute(images, pathSaveCombinedImage);

And the result of using the above code will be as follows

enter image description here