How might I add a watermark effect to an image in

2019-01-16 16:54发布

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

5条回答
来,给爷笑一个
2楼-- · 2019-01-16 17:34

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.

查看更多
成全新的幸福
3楼-- · 2019-01-16 17:36

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.

查看更多
不美不萌又怎样
4楼-- · 2019-01-16 17:41

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

查看更多
Ridiculous、
5楼-- · 2019-01-16 17:42

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;
    }
查看更多
别忘想泡老子
6楼-- · 2019-01-16 17:48

If someone is still searching for this, I found the perfect answer 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 watermarkImageFile The image file used as the watermark.  
    * @param sourceImageFile The source image file.  
    * @param destImageFile The output image file.  
    */  
   /**  
    * Adds a watermark on the given image.  
    */  
   public static Bitmap addWatermark(Resources res, Bitmap source) {  
     int w, h;  
     Canvas c;  
     Paint paint;  
     Bitmap bmp, watermark;  
     Matrix matrix;  
     float scale;  
     RectF r;  
     w = source.getWidth();  
     h = source.getHeight();  
     // Create the new bitmap  
     bmp = Bitmap.createBitmap(w, h, 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  
     c = new Canvas(bmp);  
     c.drawBitmap(source, 0, 0, paint);  
     // Load the watermark  
     watermark = BitmapFactory.decodeResource(res, R.drawable.dell_logo);  
     // Scale the watermark to be approximately 40% of the source image height  
     scale = (float) (((float) h * 0.40) / (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(w - r.width(), h - r.height());  
     // Draw the watermark  
     c.drawBitmap(watermark, matrix, paint);  
     // Free up the bitmap memory  
     watermark.recycle();  
     return bmp;  
   } 

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

查看更多
登录 后发表回答