1

我想将两个位图并排合并为一个位图。以下代码是合并子底部。如何并排合并到一个位图中?

public Bitmap mergeBitmap(Bitmap fr, Bitmap sc) 
{ 

    Bitmap comboBitmap; 

    int width, height; 

    width = fr.getWidth() + sc.getWidth(); 
    height = fr.getHeight(); 

    comboBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 

    Canvas comboImage = new Canvas(comboBitmap); 


    comboImage.drawBitmap(fr, 0f, 0f, null); 
    comboImage.drawBitmap(sc, 0f , fr.getHeight(), null); 
    return comboBitmap;

}
4

2 回答 2

3
public Bitmap mergeBitmap(Bitmap fr, Bitmap sc) 
    { 

        Bitmap comboBitmap; 

        int width, height; 

        width = fr.getWidth() + sc.getWidth(); 
        height = fr.getHeight(); 

        comboBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 

        Canvas comboImage = new Canvas(comboBitmap); 


        comboImage.drawBitmap(fr, 0f, 0f, null); 
        comboImage.drawBitmap(sc, fr.getWidth(), 0f , null); 
        return comboBitmap;

    }
于 2013-01-10T18:26:55.520 回答
0

本文介绍了将 2 张图像组合在一起的过程(仅适用于 PNG 或 JPG )。它将涉及传递 2 个位图,然后将使用 Canvas 类进行组合。你可以做一些小的改动来让你的两个图像并排:

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.getHeight() > s.getHeight()) { 
      width = c.getWidth() + s.getWidth(; 
      height = c.getHeight()); 
    } else { 
      width = c.getWidth() + s.getWidth(); 
      height = s.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; 
  } 
于 2013-01-10T20:17:36.053 回答