【问题标题】:Combine two images in android java在android java中组合两个图像
【发布时间】:2011-07-26 13:05:52
【问题描述】:

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

【问题讨论】:

标签: java android image image-processing


【解决方案1】:

类似于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;

}

【讨论】:

    【解决方案2】:

    我一般使用下面的函数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; 
    }  
    

    【讨论】:

      【解决方案3】:

      解决这个问题的最简单方法可能是在一个 RelativeLayout 中使用两个 ImageView。您可以在布局中将 ImageViews 放置在彼此的顶部。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-01-19
          • 2013-06-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多