如何在Android中为图像添加水印效果?

时间:2012-05-21 04:15:47

标签: android image watermark

我有一个带帧的图像,我需要添加水印效果。我怎么能这样做?

6 个答案:

答案 0 :(得分:35)

我在Android图像处理here上找到了很棒的教程。

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);
    paint.setAlpha(alpha);
    paint.setTextSize(size);
    paint.setAntiAlias(true);
    paint.setUnderlineText(underline);
    canvas.drawText(watermark, location.x, location.y, paint);

    return result;
}

感谢Pete Houston在基本图像处理方面分享了这么有用的教程。

答案 1 :(得分:18)

对于其他参考,如果您想在图像顶部添加应用程序的徽标(位于可绘制文件夹中),请使用以下方法:

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.logo);
        canvas.drawBitmap(waterMark, 0, 0, null);

        return result;
    }

答案 2 :(得分:5)

如果有人还在搜索,我找到了一个很好的解决方案 here

根据源图像添加水印到右下角部分,按比例缩放,这正是我想要的。

/**
 * Embeds an image watermark over a source image to produce
 * a watermarked one.
 * @param source The source image where watermark should be placed
 * @param watermark Watermark image to place
 * @param ratio A float value < 1 to give the ratio of watermark's height to image's height,
 *             try changing this from 0.20 to 0.60 to obtain right results
 */
public static Bitmap addWatermark(Bitmap source, Bitmap watermark, float ratio) {
    Canvas canvas;
    Paint paint;
    Bitmap bmp;
    Matrix matrix;
    RectF r;

    int width, height;
    float scale;

    width = source.getWidth();
    height = source.getHeight();

    // Create the new bitmap
    bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG);

    // Copy the original bitmap into the new one
    canvas = new Canvas(bmp);
    canvas.drawBitmap(source, 0, 0, paint);

    // Scale the watermark to be approximately to the ratio given of the source image height
    scale = (float) (((float) height * ratio) / (float) watermark.getHeight());

    // Create the matrix
    matrix = new Matrix();
    matrix.postScale(scale, scale);

    // Determine the post-scaled size of the watermark
    r = new RectF(0, 0, watermark.getWidth(), watermark.getHeight());
    matrix.mapRect(r);

    // Move the watermark to the bottom right corner
    matrix.postTranslate(width - r.width(), height - r.height());

    // Draw the watermark
    canvas.drawBitmap(watermark, matrix, paint);

    return bmp;
}

评论很好,这是一个巨大的优势!

答案 3 :(得分:4)

看来你正在寻找一个waterrippleeffect。查看完整的源代码。另请查看屏幕截图效果如何。

答案 4 :(得分:0)

即使没有可见的水印,您也可以使用androidWM向图像中添加水印:

添加依赖性:

dependencies {
  ...
  implementation 'com.huangyz0918:androidwm:0.2.3'
  ...
}

和Java代码:

 WatermarkText watermarkText = new WatermarkText(“Hello World”)
                    .setPositionX(0.5) 
                    .setPositionY(0.5) 
                    .setTextAlpha(100) 
                    .setTextColor(Color.WHITE) 
                    .setTextFont(R.font.champagne) 
                    .setTextShadow(0.1f, 5, 5, Color.BLUE); 

 WatermarkBuilder.create(this, backgroundBitmap) 
                    .loadWatermarkText(watermarkText) 
                    .getWatermark() 
                    .setToImageView(backgroundView); 

您可以像这样轻松地添加图像类型水印或文本水印,并且库大小小于30Kb。

答案 5 :(得分:-3)

使用framelayout。将两个图像视图放在framelayout中并指定水印图像视图的位置。