From the Internet to find the predecessors to write code, and then optimize the collation, the realization of the image overlay watermark, that is, the text overlay, and then realize the total text according to the length of the automatic line-wrapping overlay Text
ImportJavax.imageio.ImageIO;Importjava.awt.*;ImportJava.awt.image.BufferedImage;ImportJava.io.File;ImportJava.io.FileOutputStream;
/** * Created by Uchoice on 2016/11/25. */Public classMyTest {
/** * Image Add watermark *@param SrcimgpathThe path of the picture to which you want to add a watermark *@param OutimgpathPicture output path after adding watermark *@param MarkcontentcolorColor of Watermark Text *@param fontsizeText Size *@param watermarkcontentThe text of the watermark * *Public voidWaterpress (String Srcimgpath, String Outimgpath, Color Markcontentcolor,intFontSize, String watermarkcontent) {Try{//Read the original picture information File Srcimgfile =NewFile (Srcimgpath); Image srcimg = ImageIO. Read (srcimgfile);intSrcimgwidth = Srcimg.getwidth (NULL);intSrcimgheight = Srcimg.getheight (NULL); Add watermark bufferedimage bufimg =NewBufferedImage (Srcimgwidth, Srcimgheight, BufferedImage.Type_int_rgb); Graphics2D g = bufimg.creategraphics (); G.drawimage (srcimg, 0, 0, srcimgwidth, Srcimgheight,NULL); Font font = new Font ("Courier new", Font.plain, 12); Font font =NewFont ("Song Body", Font.PLAIN, fontsize); G.setcolor (Markcontentcolor); Set the watermark color according to the background of the picture
G.setfont (font);intFontlen = Getwatermarklength (watermarkcontent, G);
intline = Fontlen/srcimgwidth; How many lines should the text length be relative to the width of the picture
inty = srcimgheight-(line + 1) *fontsize; System. out. println ("Total watermark text length:"+ Fontlen +", Picture width:"+ Srcimgwidth +", Number of characters:"+ watermarkcontent.length ());
Text overlay, automatic line wrapping overlayinttempx = 0;intTempy = y;intTempcharlen = 0; Single character lengthintTemplinelen = 0; Temporary calculation of total length of single line characters StringBuffer SB =NewStringBuffer (); for(inti= 0; I<watermarkcontent.length (); i++) {CharTempchar = Watermarkcontent.charat (i); Tempcharlen = Getcharlen (Tempchar, G);
Templinelen + = Tempcharlen;
if(Templinelen >= srcimgwidth) {//length already full row, carries on the text superposition g.drawstring (sb.tostring (), tempx, tempy);
Sb.delete (0, Sb.length ()); Empty content, append again
Tempy + = FontSize;
Templinelen = 0; } sb.append (Tempchar); Append character}
g.DrawString (Sb.tostring (), tempx, tempy); Finally overlay the remaining text g.dispose ();
Output picture FileOutputStream Outimgstream =NewFileOutputStream (Outimgpath); ImageIO. Write (Bufimg,"JPG", Outimgstream); Outimgstream.flush (); Outimgstream.close (); }Catch(Exception e) {E.printstacktrace ();} }
public intGetcharlen (CharC, graphics2d g) { returnG.getfontmetrics (G.getfont ()). Charwidth (c); }
/** * Get watermark Text Total length *@param watermarkcontentThe text of the watermark *@param g *@returnTotal watermark Text Length * *public intGetwatermarklength (String watermarkcontent, graphics2d g) { returnG.getfontmetrics (G.getfont ()). Charswidth (Watermarkcontent.tochararray (), 0, Watermarkcontent.length ()); }
Public static voidMain (string[] args) {//original position, output picture position, watermark text color, watermark text String font ="qqqqqqssss2222 .... ooooddd watermarking effect watermark effect watermarking effect watermark effect watermark effect watermarking effect watermark effect watermarking effect of watermark effect whole watermark effect measurement watermark effect whole";NewMyTest (). Waterpress ("/users/slink/3f608a0601_0.jpg","/users/slink/afterwatermark.jpg", Color.Red, the font); } }