合并两个图像(一个图像是透明的没有脸,第二个图像只是来自SD卡的脸)

时间:2013-04-23 05:30:48

标签: android

我有两个图像,一个图像包含没有脸的身体,一个图像只包含脸部......

现在我想合并这两个图像....第一个只包含没有脸的身体的图像就是脸是透明的......

那么如何检测透明区域并将面部放在透明区域?

我将两张图片与下面的代码组合在一起..但是将面部放在透明区域

是不正确的方法

我的代码如下,

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, 0f, 0f, null);

    return cs;
}

2 个答案:

答案 0 :(得分:1)

使用Canvas将两个或多个图像合并到android中,使用下面的代码简化合并图像, 首先为要合并它的特定图像创建位图。

获取要合并图像的区域的X和Y轴位置。

    mComboImage = new Canvas(mBackground);

   mComboImage.drawBitmap(c, x-axis position in f, y-axis position in f, null);

    mComboImage.drawBitmap(c, 0f, 0f, null);
    mComboImage.drawBitmap(s, 200f, 200f, null);


    mBitmapDrawable = new BitmapDrawable(mBackground);
    Bitmap mNewSaving = ((BitmapDrawable)mBitmapDrawable).getBitmap();

在imageview中设置此新位图。 imageView.setImageBitmap(mNewSaving);

此方法在此方法中,两个图像位图组合在一个位图中,该位图返回新合并图像的位图。还将此图像保存在sdcard上。如下代码

public Bitmap combineImages(Bitmap c, Bitmap s) {
    Bitmap cs = null; 

    int width, height = 0; 

    if(c.getWidth() > s.getWidth()) { 
      width = c.getWidth(); 
      height = c.getHeight() + s.getHeight(); 
    } else { 
      width = s.getWidth(); 
      height = c.getHeight() + s.getHeight(); 
    } 

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

    Canvas comboImage = new Canvas(cs); 
    comboImage.drawBitmap(c, new Matrix(), null);
    comboImage.drawBitmap(s, new Matrix(), null);

    // this is an extra bit I added, just incase you want to save the new image somewhere and then return the location.

    return cs; 
  } 
}

答案 1 :(得分:0)

以下是合并两个位图的正确方法:

    public Bitmap combineImages(Bitmap topImage, Bitmap bottomImage) {
        Bitmap overlay = Bitmap.createBitmap(bottomImage.getWidth(), bottomImage.getHeight(), bottomImage.getConfig());
        Canvas canvas = new Canvas(overlay);
        canvas.drawBitmap(bottomImage, new Matrix(), null);
        canvas.drawBitmap(topImage, 0, 0, null);
        return overlay;
    }