6

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

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);

但什么也没发生,你能帮帮我吗?谢谢

4

3 回答 3

10

它解决了,我只是为该代码更改了一点,并感谢您的建议 Doomsknight :)

 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);
于 2012-11-09T03:33:15.563 回答
2

您没有将返回的位图分配result给任何东西。用新的位图替换旧位图。

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

编辑:

根据评论,您仍然遇到问题:尝试对一些参数进行硬编码以进行测试。看看它是否与您的参数有关。

        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
于 2012-11-07T09:44:31.080 回答
1
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;
}
于 2015-11-25T17:27:26.800 回答