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

dced5bon  于 2022-12-28  发布在  Android
关注(0)|答案(8)|浏览(136)

我有一张有边框的图片,我需要添加水印效果。我该怎么做?

ocebsuys

ocebsuys1#

我发现了伟大的教程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,他分享了关于基本图像处理的有用教程。

klr1opcd

klr1opcd2#

供其他人参考,如果你想添加你的应用程序的标志(这是在您的可绘制文件夹(s))的图像顶部使用以下方法:

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

zyfwsgd63#

如果有人仍在搜索此问题,我找到了一个很好的解决方案**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;
}

而且评论很好,这是一个巨大的加分!

tp5buhyn

tp5buhyn4#

看来你正在寻找一个waterrippleeffect作为这一个。检查完整的源代码。也检查截图效果看起来如何。

fjnneemd

fjnneemd5#

在Kotlin:
注:以上答案只是修改代码

private fun mark(src: Bitmap, watermark: String): Bitmap {
        val w = src.width
        val h = src.height
        val result = Bitmap.createBitmap(w, h, src.config)
        val canvas = Canvas(result)
        canvas.drawBitmap(src, 0f, 0f, null)
        val paint = Paint()
        paint.color = Color.RED
        paint.textSize = 10f
        paint.isAntiAlias = true
        paint.isUnderlineText = true
        canvas.drawText(watermark, 20f, 25f, paint)
        return result
    }

val imageBitmap = mark(yourBitmap, "Your Text")
binding.meetProofImageView.setImageBitmap(imageBitmap)
nzkunb0c

nzkunb0c6#

您可以使用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。

cygmwpex

cygmwpex7#

我尝试了其他帖子中提到的一些库,比如this,但不幸的是它丢失了,现在还不能下载。所以我遵循了上面的AndroidLearner 's answer,但在稍微调整了代码之后,对于那些旋转水印有困难的人,以及什么值对Paint类的各种方法有效,为了使文本显示旋转一个Angular (像大多数公司水印一样),您可以使用以下代码。
注意,wh分别是屏幕的宽度和高度,你可以很容易地计算出来,你可以在stackoverflow上找到很多方法。

public static Bitmap waterMarkBitmap(Bitmap src, String watermark) {
    int w = src.getWidth();
    int h = src.getHeight();
    Bitmap mutableBitmap = Utils.getMutableBitmap(src);
    Bitmap result = Bitmap.createBitmap(w, h, mutableBitmap.getConfig());

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

    Paint paint = new Paint();
    paint.setColor(Color.RED);
    paint.setTextSize(92f);
    paint.setAntiAlias(true);
    paint.setAlpha(70);  // accepts value between 0 to 255, 0 means 100% transparent, 255 means 100% opaque.
    paint.setUnderlineText(false);
    canvas.rotate(45, w / 10f, h / 4f);
    canvas.drawText(watermark, w / 10f, h / 4f, paint);
    canvas.rotate(-45, w / 10f, h / 4f);

    return result;
}

它将文本水印旋转45度,并将其放置在位图的中心。
还请注意,如果你不能得到水印,它可能是你作为源位图是不可变的情况。对于这种最坏的情况,你可以使用下面的方法从一个不可变的位图创建一个可变的位图。

public static Bitmap getMutableBitmap(Bitmap immutableBitmap) {
    if (immutableBitmap.isMutable()) {
        return immutableBitmap;
    }

    Bitmap workingBitmap = Bitmap.createBitmap(immutableBitmap);
    return workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
}

我在here里面找到了上面的方法。我已经在我的应用程序中使用了这两个方法进行了测试,在我添加了上面的调整后,它工作得很完美。尝试一下,让我知道它是否工作。

yrdbyhpb

yrdbyhpb8#

使用framelayout.把两个imageview放进framelayout并指定水印imageview的位置.

相关问题