在Android中以编程方式添加图像WaterMark

时间:2015-11-08 04:08:30

标签: java android

我正在尝试将图像水印添加到另一个图像。我有以下代码,但我遇到了问题。我不知道'资源资源'是什么。

有人可以帮忙吗?

public static Bitmap addWatermark(Resources res, Bitmap source)
{
     int w, h;
     Canvas c;
     Paint paint;
     Bitmap bmp, watermark;

     Matrix matrix;
     float scale;
     RectF r;

     w = source.getWidth();
     h = source.getHeight();


    bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

    paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG |Paint.FILTER_BITMAP_FLAG);         


    c = new Canvas(bmp);
    c.drawBitmap(source, 0, 0, paint);

    watermark = BitmapFactory.decodeResource(res, R.drawable.android_mo);
    scale = (float) (((float) h * 0.10) / (float) watermark.getHeight());

    matrix = new Matrix();
    matrix.postScale(scale, scale);
    r = new RectF(0, 0, watermark.getWidth(), watermark.getHeight());
    matrix.mapRect(r);
    matrix.postTranslate(w - r.width(), h - r.height());

    c.drawBitmap(watermark, matrix, paint);
    watermark.recycle();

    return bmp;
}

3 个答案:

答案 0 :(得分:1)

您可以通过activity.getResources()fragment.getResources()

获取资源对象

答案 1 :(得分:1)

Resources res 

是您需要从调用的Activity / Fragment中传递的资源对象。

假设,如果该方法在Utility类中,则以

调用该方法
//Add watermark to the selected image
  Bitmap markedBitmap = Utility.addWatermark(context.getResources(), bitmap);

答案 2 :(得分:0)

主位图是bmp1和bmp2 transparante位图:

 public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
            Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
            Canvas canvas = new Canvas(bmOverlay);
            canvas.drawBitmap(bmp1, new Matrix(), null);
            canvas.drawBitmap(bmp2, 0, 0, null);
            return bmOverlay;
        }