Combining two bitmap image (side by side)

前端 未结 3 1036
醉梦人生
醉梦人生 2020-12-04 22:24

Can any one help to combine two bitmap images into single bitmap

in android (Side by side).

Thanks, Yuvaraj

3条回答
  •  攒了一身酷
    2020-12-04 23:09

    You can use Canvas - check out this article:

    http://www.jondev.net/articles/Combining_2_Images_in_Android_using_Canvas

    Updated code to do it side by side:

    public Bitmap combineImages(Bitmap c, Bitmap s) { // can add a 3rd parameter 'String loc' if you want to save the new image - left some code to do that at the bottom 
        Bitmap cs = null; 
    
        int width, height = 0; 
    
        if(c.getWidth() > s.getWidth()) { 
          width = c.getWidth() + s.getWidth(); 
          height = c.getHeight(); 
        } else { 
          width = s.getWidth() + s.getWidth(); 
          height = c.getHeight(); 
        } 
    
        cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
    
        Canvas comboImage = new Canvas(cs); 
    
        comboImage.drawBitmap(c, 0f, 0f, null); 
        comboImage.drawBitmap(s, c.getWidth(), 0f, null); 
    
        // this is an extra bit I added, just incase you want to save the new image somewhere and then return the location 
        /*String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png"; 
    
        OutputStream os = null; 
        try { 
          os = new FileOutputStream(loc + tmpImg); 
          cs.compress(CompressFormat.PNG, 100, os); 
        } catch(IOException e) { 
          Log.e("combineImages", "problem combining images", e); 
        }*/ 
    
        return cs; 
      } 
    

提交回复
热议问题