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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10679445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:34:38  来源:igfitidea点击:

How might I add a watermark effect to an image in Android?

androidimagewatermark

提问by info

I have an image with frames and I need to add a watermark effect. How might I do this?

我有一个带框架的图像,我需要添加水印效果。我该怎么做?

回答by AndroidLearner

I found great tutorial on Android Image Processing here.

我在这里找到了关于 Android 图像处理的很棒的教程。

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

Thanks to Pete Houston who shares such useful tutorial on basic image processing.

感谢 Pete Houston,他分享了如此有用的基本图像处理教程。

回答by Hesam

For others reference, if you want to add the logo of your application (which is in your drawable folder(s)) on top of image use following method:

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

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

回答by Irshad P I

If someone is still searching for this, I found a good solution here

如果有人还在寻找这个,我在这里找到了一个很好的解决方案

It adds a watermark to the bottom rightportion and scales itaccording to the source image which was exactly what I was looking for.

它在右下角添加了一个水印,并根据源图像进行缩放,这正是我正在寻找的。

/**
 * 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;
}

And it is well commented which is what is a huge plus!

它得到了很好的评论,这是一个巨大的优势!

回答by Lalit Poptani

It seems you are looking for a waterrippleeffectas this one. Checkout the complete source code. Also check the screenshot how does the effect look like.

看来你正在寻找一个waterrippleeffect这样的。查看完整的源代码。还要检查截图效果如何。

回答by Yizheng Huang

You can use androidWMto add a watermark into your image, even with invisible watermarks:

您可以使用androidWM将水印添加到您的图像中,即使是不可见的水印:

add dependence:

添加依赖:

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

and java code:

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

You can easily add an image type watermark or a text watermark like this, and the library size is smaller than 30Kb.

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

回答by SathishBabu S

use framelayout. put two imageviews inside the framelayout and specify the position of the watermark imageview.

使用框架布局。将两个 imageview 放入 framelayout 并指定水印 imageview 的位置。