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

后端 未结 6 925
深忆病人
深忆病人 2020-11-30 23:09

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

相关标签:
6条回答
  • 2020-11-30 23:46

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

    0 讨论(0)
  • 2020-11-30 23:50

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

    0 讨论(0)
  • 2020-11-30 23:55

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

    add dependence:

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

    and java code:

     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.

    0 讨论(0)
  • 2020-12-01 00:01

    I found great tutorial on Android Image Processing 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;
    }
    

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

    0 讨论(0)
  • 2020-12-01 00:03

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

    It adds a watermark to the bottom right portion and scales it according 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!

    0 讨论(0)
  • 2020-12-01 00:07

    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;
        }
    
    0 讨论(0)
提交回复
热议问题