Watermarks and text for Android Images

Source: Internet
Author: User
Two methods: 1. directly write the text string STR = "Text to be written by PICC"; imageview image = (imageview) This. findviewbyid (R. id. imageview); bitmap photo = bitmapfactory. decoderesource (this. getresources (), R. drawable. text); int width = photo. getwidth (), hight = photo. getheight (); system. out. println ("width" + width + "height" + hight); icon = bitmap. createbitmap (width, hight, bitmap. config. argb_8888); // create an empty bitmap canvas = new canvas (ICO N); // initialize the image drawn from the canvas to the icon. Paint photopaint = new paint (); // create a paint brush photopaint. setdither (true); // obtain the image with clear sampling photopaint. setfilterbitmap (true); // filter some rect src = new rect (0, 0, photo. getwidth (), photo. getheight (); // create the coordinates rect DST = new rect (0, 0, width, hight) of a specified new rectangle ); // create a coordinate canvas for the specified new rectangle. drawbitmap (photo, SRC, DST, photopaint); // scale photo or expand to the fill area used by DST. photopaint paint textpaint = new paint (paint. anti _ Alias_flag | paint. dev_kern_text_flag); // sets the paint brush textpaint. settextsize (20366f); // font size textpaint. settypeface (typeface. default_bold); // use the default width textpaint. setcolor (color. red); // used color // textpaint. setshadowlayer (3f, 1, 1, this. getresources (). getcolor (Android. r. color. background_dark); // specifies the canvas for audio and video settings. drawtext (STR, 20, 26, textpaint); // draw up the word, start to unknown x, y uses that pen to draw the canvas. save (canvas. all_save_flag); canvas. restore (); Im Age. setimagebitmap (icon); savemybitmap (icon); 2. combine the two images into the oncreat method {bitmap mark = bitmapfactory. decoderesource (this. getresources (), R. drawable. icon); bitmap photo = bitmapfactory. decoderesource (this. getresources (), R. drawable. text); bitmap A = createbitmap (photo, mark); image. setimagebitmap (a); savemybitmap (a);} private bitmap createbitmap (Bitmap SRC, bitmap watermark) {string tag = "createbit Map "; // 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 Bitmap bitmap newb = bitmap. createbitmap (W, H, config. argb_8888); // create a new bitmap canvas CV = new canvas (NEWB) with the same width as SRC; // draw SRC into cv. drawbitmap (SRC, 0, 0, null); // you can specify SRC // draw watermark into cv. drawbitmap (watermark, W-ww + 5, H-Wh + 5, null); // Add the watermark to the bottom right corner of SRC // save all clip cv. save (canvas. all_save_flag); // save // store cv. restore (); // store return newb;} // Save the image to public void savemybitmap (bitmap BMP) under data {fileoutputstream Fos = NULL; try {Fos = openfileoutput ("image1.jpg", context. mode_private); BMP. compress (bitmap. compressformat. JPEG, 100, FOS);} catch (Fi Lenotfoundexception e) {}finally {If (FOS! = NULL) {try {FOS. Flush (); FOS. Close () ;}catch (ioexception e ){}}}}

 

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.