Poster Factory (i) How to add watermarks and text to a picture in Android

Source: Internet
Author: User

How to add a watermark to a picture in Android, some core code is intercepted below, for reference only:

/**
* Get pictures with smaller images
* @param src
* @return
*/
public static Bitmap Scalebitmap (String src)
{
Get the height and width of the picture
Bitmapfactory.options Options = new Bitmapfactory.options ();
This setting allows Bitmapfactory.decodefile to get an empty picture, but writes the picture information to the options
Options.injustdecodebounds = true;
Bitmapfactory.decodefile (src, options);
Options.insamplesize = 1;
Set to get Data
Options.injustdecodebounds = false;
Get Pictures
Return Bitmapfactory.decodefile (src, options);
}
/**
* Add watermark can also add text
* @param src
* @param watermark
* @param title
* @return
*/
public static Bitmap Watermarkbitmap (Bitmap src, Bitmap watermark,
String title) {
if (src = = null) {
return null;
}
int w = src.getwidth ();
int h = src.getheight ();
Need to deal with the picture too large caused by the memory of the problem, here my picture is very small, so do not write the corresponding code
Bitmap newb= Bitmap.createbitmap (W, H, config.argb_8888);//Create a new bitmap of the same length as the src width
Canvas CV = new canvas (NEWB);
Cv.drawbitmap (SRC, 0, 0, NULL);//at 0, 0 coordinates begin to draw into SRC
Paint paint=new paint ();
Add a picture
if (watermark! = null) {
int ww = Watermark.getwidth ();
int wh = Watermark.getheight ();
Paint.setalpha (50);
Cv.drawbitmap (watermark, W-WW + 5, H-WH + 5, paint);//Draw a watermark in the lower right corner of SRC
Cv.drawbitmap (watermark, 0, 0, paint);//Draw a watermark in the upper left corner of SRC
}else{
LOG.I ("I", "water mark failed");
}
Add text
if (title!=null)
{
String familyname = "Song Body";
Typeface font = typeface.create (familyname,typeface.normal);
Textpaint textpaint=new textpaint ();
Textpaint.setcolor (color.red);
Textpaint.settypeface (font);
Textpaint.settextsize (40);
This is a line wrap.
Staticlayout layout = new Staticlayout (title,textpaint,w,alignment.align_opposite,1.0f,0.0f,true);
Layout.draw (CV);
Just add the upper left corner of the text.
Cv.drawtext (Title,w-400,h-40,textpaint);
}
Cv.save (Canvas.all_save_flag);//Save
Cv.restore ();//Storage
return newb;
}

Poster Factory (i) How to add watermarks and text to a picture in Android

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.