Add a watermark image or text to an Android image

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.