combining two png files in android

后端 未结 6 1641
星月不相逢
星月不相逢 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:53

    I use this code

    private class PhotoComposition extends AsyncTask {
        private String pathSave;//path save combined images
    
        @Override
        protected Boolean doInBackground(Object... objects) {
    
          List images = (List) objects[0]; //lsit of path iamges
          pathSave = (String) objects[1];//path save combined images
          if (images.size() == 0) {
            return false;
          }
          List 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 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 bitmaps) {
          int width = 0;
          for (int i = 0; i < bitmaps.size(); i++) {
            width += bitmaps.get(i).getWidth();
          }
          return width;
        }
    

    USAGE

    List 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

提交回复
热议问题