[Excerpt from-java-Learning] Java Image Overlay Watermark & Word Wrapping __java

Source: Internet
Author: User
Tags stringbuffer

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); } }

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.