在android java中结合两个图像

时间:2011-03-31 01:19:54

标签: java android image image-processing

所以我在android中的SD卡上本地存储了两个图像,我想将它们组合成一个图像。很难解释所以我将链接到一张图片,以便更好地展示我如何拍摄前两张图片并将它们组合到最后一张图片中。

http://img850.imageshack.us/i/combinedh.jpg/

4 个答案:

答案 0 :(得分:11)

创建目标Bitmap,为其创建Canvas,使用Canvas.drawBitmap将每个源位图blit到目标位图中。

答案 1 :(得分:11)

我通常使用以下函数from Jon Simon来组合两个作为参数传递的Bitmap并将Bitmap作为输出组合,

    public Bitmap combineImages(Bitmap c, Bitmap s) 
{ 
    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); 

    return cs; 
}  

答案 2 :(得分:2)

最简单的方法可能是在RelativeLayout中使用两个ImageView。您可以在布局中将ImageView放在彼此的顶部。

答案 3 :(得分:0)

类似于Hitesh's answer,但具有用于指定前景图像位置的参数:

public static Bitmap mergeBitmaps(Bitmap bitmapBg, Bitmap bitmapFg, float fgLeftPos, float fgTopPos) {

    // Calculate the size of the merged Bitmap
    int mergedImageWidth = Math.max(bitmapBg.getWidth(), bitmapFg.getWidth());
    int mergedImageHeight = Math.max(bitmapBg.getHeight(), bitmapFg.getHeight());

    // Create the return Bitmap (and Canvas to draw on)
    Bitmap mergedBitmap = Bitmap.createBitmap(mergedImageWidth, mergedImageHeight, bitmapBg.getConfig());
    Canvas mergedBitmapCanvas = new Canvas(mergedBitmap);

    // Draw the background image
    mergedBitmapCanvas.drawBitmap(bitmapBg, 0f, 0f, null);

    //Draw the foreground image
    mergedBitmapCanvas.drawBitmap(bitmapFg, fgLeftPos, fgTopPos, null);

    return mergedBitmap;

}