combining two png files in android

后端 未结 6 1625
星月不相逢
星月不相逢 2020-11-30 23:32

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 w

相关标签:
6条回答
  • 2020-11-30 23:37

    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)

    0 讨论(0)
  • 2020-11-30 23:43

    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.

    0 讨论(0)
  • 2020-11-30 23:43

    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 讨论(0)
  • 2020-11-30 23:53

    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

    0 讨论(0)
  • 2020-12-01 00:00

    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

    0 讨论(0)
  • 2020-12-01 00:01

    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().

    0 讨论(0)
提交回复
热议问题