在android中添加水印图像

时间:2012-11-07 09:28:36

标签: android android-layout

我有代码在这样的图像中添加水印

public static Bitmap mark(Bitmap src, String watermark, Point location, Color color, int alpha, int size, boolean underline) {
            int w = src.getWidth();
            int h = src.getHeight();
            Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());

            Canvas canvas = new Canvas(result);
            canvas.drawBitmap(src, 0, 0, null);

            Paint paint = new Paint();
            paint.setColor(color.RED);
            paint.setAlpha(alpha);
            paint.setTextSize(size);
            paint.setAntiAlias(true);
            paint.setUnderlineText(underline);
            canvas.drawText(watermark, location.x, location.y, paint);

            return result;
        }

我用这段代码调用该函数

mark(bitmap, "watermark", b, null, c, 100, false);
            imgshoot.setImageBitmap(bitmap);

但没有任何事情发生,你能帮帮我吗?感谢

3 个答案:

答案 0 :(得分:8)

它解决了,我只是改变了一点代码,感谢你的建议Doomknight:)

 public static Bitmap mark(Bitmap src, String watermark) {
    int w = src.getWidth();
    int h = src.getHeight();
    Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(src, 0, 0, null);
    Paint paint = new Paint();
    paint.setColor(Color.RED);
    paint.setTextSize(18);
    paint.setAntiAlias(true);
    paint.setUnderlineText(true);
    canvas.drawText(watermark, 20, 25, paint);

    return result;
}

我用这个函数调用

bitmap = mark(bitmap, "Hallo");
imgshoot.setImageBitmap(bitmap);

答案 1 :(得分:2)

您没有将返回的位图result分配给任何内容。将旧位图替换为新位图。

 bitmap = mark(bitmap, "watermark", b, null, c, 100, false);
 imgshoot.setImageBitmap(bitmap);

编辑:

根据评论,你仍然遇到问题:尝试硬编码一些参数,进行测试。看看你的params是否存在问题。

        Paint paint = new Paint();
        paint.setColor(color.RED);
        //paint.setAlpha(alpha);
        paint.setTextSize(20); //size
        //paint.setAntiAlias(true);
        paint.setUnderlineText(underline);
        canvas.drawText(watermark, 10, 10, paint); //location.x, location.y

答案 2 :(得分:0)

private Bitmap addWaterMark(Bitmap src) 
{
    int w = src.getWidth();
    int h = src.getHeight();
    Bitmap result = Bitmap.createBitmap(w,h,src.getConfig());
    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(src,0,0, null);
    Bitmap waterMark = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_laucher);
    canvas.drawBitmap(waterMark,0,0,null);

    return result;
}