开发者

combining two png files in android

开发者 https://www.devze.com 2022-12-28 10:14 出处:网络
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 overl

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 wit开发者_开发知识库h 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?


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)


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


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

combining two png files in android


You may wish to look into the Canvas object, which would make it easy to do other drawing operations as well. You can just draw your bitmaps onto a canvas where you want them, then save the resulting bitmap.


If they have transparent sections, then if you draw one on top of the other, only the non-transparent portions will overlap. It will be up to you to arrange the bitmaps however you like.

For the separate issue of re-saving your image to a png, use bitmap.compress().


Try this .

public Bitmap mergeBitmap(Bitmap frame, Bitmap img){

    Bitmap bmOverlay = Bitmap.createBitmap(frame.getWidth(), frame.getHeight(), frame.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(img, 0, 0, null);
    canvas.drawBitmap(frame, new Matrix(), null);

    return bmOverlay;

}

Returns a bitmap image

Pass two bitmap images to your function as shown below

Bitmap img= mergeBitmap(imgone, imagetwo);

See the entire post or also see merge multiple images in android programmatically

0

精彩评论

暂无评论...
验证码 换一张
取 消