1

我有两张图片,一张包含没有脸的身体,一张包含只有脸的图片...

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

那么我怎样才能检测到透明区域并将脸放在透明区域中呢?

我将两个图像与下面的代码组合在一起..但是将脸放在透明区域上不是正确的方法

我的代码如下,

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;
}
4

2 回答 2

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; 
  } 
}
于 2013-04-23T05:36:50.063 回答
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;
    }
于 2016-04-25T04:59:25.617 回答