The basic idea of adding a watermark to a picture is to load the original image, add text or load a watermark image, save the three parts of the picture
To add a watermark image:
Private Bitmap Createwatermaskimage (Context gcontext, Bitmap src, Bitmap watermark) {String tag = "CreateBitmap"; LOG.D (Tag, "Create a new bitmap"); if (src = = null) {return null;} int w = src.getwidth (), int h = src.getheight (), int ww = watermark.getwidth (); int wh = Watermark.getheight ();//Create the New blank Bitmapbitmap newb = Bitmap.createbitmap (W, H, config.argb_8888);//Create a new bitmap with the same src length width as canvas CV = new Canvas (new b);//Draw src Intocv.drawbitmap (src, 0, 0, NULL);//at 0, 0 coordinates begin drawing into src//draw watermark Intocv.drawbitmap (watermark, 20, 20, NULL)//watermark in the lower right corner of SRC//Save All Clipcv.save (Canvas.all_save_flag);//saving//Storecv.restore ();//Store return NEWB;}
Add text
public static Bitmap SCALEWITHWH (Bitmap src, double W, double h) {if (w = = 0 | | h = = 0 | | src = = NULL) { return SRC; } else {//record SRC's width height int width = src.getwidth (); int height = src.getheight (); Create a matrix container matrix matrix = new Matrix (); Calculates the scale of float ScaleWidth = (float) (w/width); float ScaleHeight = (float) (h/height); Start Scaling Matrix.postscale (ScaleWidth, ScaleHeight); Create scaled picture return Bitmap.createbitmap (src, 0, 0, width, height, matrix, true); }}public Bitmap Drawtexttobitmap (Context gcontext, int gresid, String gtext) {Resources resources = Gconte Xt.getresources (); Float scale = resources.getdisplaymetrics (). density; Bitmap Bitmap = bitmapfactory.decoderesource (resources, GRESID); Bitmap = SCALEWITHWH (Bitmap, 300*scale, 300*scale); Android.graphics.Bitmap.Config bitMapconfig = Bitmap.getconfig (); Set default Bitmap config if none if (Bitmapconfig = = null) {Bitmapconfig = Android.graphics.Bitmap.Config.ARGB _8888; }//Resource bitmaps is imutable,//So we need to convert it to mutable one bitmap = Bitmap.copy (bitmapconfig , true); Canvas canvas = new canvas (bitmap); New antialised Paint paint paint = new paint (Paint.anti_alias_flag); Text color-#3D3D3D paint.setcolor (color.red); Paint.settextsize ((int) (* scale)); Paint.setdither (TRUE); Gets the image sampled with clear Paint.setfilterbitmap (true);//filter some rect bounds = new rect (); Paint.gettextbounds (gtext, 0, Gtext.length (), bounds); int x = 30; int y = 30; Canvas.drawtext (gtext, x * scale, Y * scale, paint); return bitmap; }
Add a watermark image or text to an Android image