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