【问题标题】:How might I add a watermark effect to an image in Android?如何在 Android 中为图像添加水印效果?
【发布时间】:2012-05-27 14:39:37
【问题描述】:

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

【问题讨论】:

    标签: android image watermark


    【解决方案1】:

    我找到了关于 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 分享了有关基本图像处理的有用教程。

    【讨论】:

    • 我可以在另一个指定图像的函数中调用这个方法吗?
    • 你可以在另一个函数中调用它。例如-Bitmap result = mark(src, watermark, location, color, alpha, size, underline);
    • 参数颜色颜色应该是int color,方法paint.setColor()正在等待参数int。如果您有更好的想法,请与我们分享。
    • 嗨,它不工作。我在片段内的图像视图上使用它
    • 捕获图像后,我通过将其转换为位图将其保存在存储中。所以,当我在保存之前使用上面的代码在该位图图像上写水印时。它不工作。你能提供更多细节吗
    【解决方案2】:

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

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

    【讨论】:

    • 我的 waterMark 位图为空,为什么会这样?
    • 我在图像上没有得到任何东西。它只是在没有水印的情况下保存。任何帮助!
    【解决方案3】:

    如果有人还在寻找这个,我找到了一个很好的解决方案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;
    }
    

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

    【讨论】:

    • 干得好@Irshad
    • 非常适合我。
    【解决方案4】:

    您似乎正在寻找waterrippleeffect 作为这个。查看完整的源代码。另请查看截图效果如何。

    【讨论】:

      【解决方案5】:

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

      【讨论】:

      • 不要发布工具的链接作为答案,特别是如果你写的,你需要透露隶属关系,否则它是垃圾邮件
      【解决方案6】:

      使用框架布局。在framelayout里面放两个imageview,并指定watermark imageview的位置。

      【讨论】:

      • 水印意味着第二张图片将是第一张图片的一部分,然后可以像分享带有水印的第一张图片一样使用。 Framelayout 只是呈现两张图片,但不会改变它们
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      相关资源
      最近更新 更多