Project requirements: Edit the article to edit, add the effect of the watermark Program Code
Package image;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics2D;
Import Java.awt.Image;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Javax.imageio.ImageIO; /** * @author Nameless * @Date 2017/12/29 * @use use Java code to add watermark to the picture/public class Watermarkutils {<span class= "Hljs-javadoc" >/** *<span class= "Hljs-javadoctag" > @param </span> srcimgpath source picture Path *< Span class= "Hljs-javadoctag" > @param </span> tarimgpath saved picture path *<span class= "Hljs-javadoctag" > @param </span> watermarkcontent watermark Content *<span class= "Hljs-javadoctag" > @param </span> markcontentcolor watermark Color *& Lt;span class= "Hljs-javadoctag" > @param </span> font watermark font */</span> <span class= "Hljs-keyword" > public</span> <span class= "Hljs-keyword" >void</span> <span class= "Hljs-title" >addwatermark </span> (String Srcimgpath, String Tarimgpath, String Watermarkcontent,color markcontentcolor,font Font) {<s Pan class= "Hljs-keyword" >try</span> {<span class= "hljs-comment" >//read the original picture information </span> Fi Le srcimgfile = <span class= "Hljs-keyword" >new</span> File (Srcimgpath); <span class= "Hljs-comment" > Get file </span> Image srcimg = Imageio.reaD (srcimgfile); <span class= "hljs-comment" >//file into pictures </span> <span class= "Hljs-keyword" >int</sp an> srcimgwidth = srcimg.getwidth (<span class= "Hljs-keyword" >null</span>); <span class= " Hljs-comment ">//get Picture's width </span> <span class=" Hljs-keyword ">int</span> srcimgheight = Srcimg.get Height (<span class= "Hljs-keyword" >null</span>) <span class= "Hljs-comment" >//get high </span of picture > <span class= "hljs-comment" >//add watermark </span> bufferedimage bufimg = <span class= "Hljs-keyw
Ord ">new</span> bufferedimage (Srcimgwidth, Srcimgheight, Bufferedimage.type_int_rgb);
Graphics2D g = bufimg.creategraphics (); G.drawimage (srcimg, <span class= "Hljs-number" >0</SPAN> <span class= "Hljs-number" >0</span>
, Srcimgwidth, Srcimgheight, <span class= "Hljs-keyword" >null</span>); G.setcolor (Markcontentcolor); <span class= "hljs-comment" >//rootAccording to the background of the image set watermark color </span> g.setfont (font);
<span class= "hljs-comment" >//set font </span> <span class= "Hljs-comment" >//set the coordinates of the watermark </span> <span class= "Hljs-keyword" >int</span> x = srcimgwidth-<span class= "Hljs-number" >2</span>*ge
Twatermarklength (Watermarkcontent, G); <span class= "Hljs-keyword" >int</span> y = srcimgheight-<span class= "Hljs-number" >2</span>*
Getwatermarklength (Watermarkcontent, G); g.DrawString (watermarkcontent, x, y);
<span class= "hljs-comment" >//draw watermark </span> g.dispose (); <span class= "hljs-comment" >//output picture </span> fileoutputstream Outimgstream = <span class= "Hljs-keywo
Rd ">new</span> FileOutputStream (Tarimgpath);
Imageio.write (bufimg, <span class= "hljs-string" > "JPG" </SPAN>, Outimgstream); System.out.println (<span class= "hljs-string" > "Add watermark Complete" </span>);
Outimgstream.flush ();
Outimgstream.close (); } <span class= "Hljs-keyword" >catch</span> (Exception e) {<span class= "hljs-comment" >//todo:ha Ndle exception</span>}} <span class= "Hljs-keyword" >public</span> <span class= "Hljs-keyword" ;int</span> <span class= "Hljs-title" >getWatermarkLength</span> (String watermarkcontent, Graphics2D g) {<span class= "Hljs-keyword" >return</span> g.getfontmetrics (G.getfont ()). Charswidth (water
Markcontent.tochararray (), <span class= "Hljs-number" >0</SPAN>, Watermarkcontent.length ()); } <span class= "Hljs-keyword" >public</span> <span class= "Hljs-keyword" >static</span> <
Span class= "Hljs-keyword" >void</span> <span class= "Hljs-title" >main</span> (string[) args) { Font font = <span class= "Hljs-keyword" >new</span> font (<span class= "hljs-string" > "Microsoft Ya Hei" </span ", Font.plain, <span class= "Hljs-number" >35</span>); <span class= "hljs-comment" >//watermark font </span> String srcimgpath=<span class= "hljs-string" > "h:/quiet Write Write/ Write blog/java to add a watermark to the picture/s.jpg "</span>; <span class= "hljs-comment" >//source picture address </span> String tarimgpath=<span class= "hljs-string" > "h:/quiet Write/ Write blog/java to add a watermark to the picture/t.jpg "</span>; <span class= "Hljs-comment" >//addresses to be stored </span> String watermarkcontent=<span class= "hljs-string" > " Image source: http://blog.csdn.net/zjq_1314520 "</span>; <span class= "hljs-comment" >//watermark content </span> Color color=<span class= "Hljs-keyword" >new</span> Color (<span class= "Hljs-number" >255</span>,<span class= "Hljs-number" >255</span>,<span class= "Hljs-number" >255</span>,<span class= "Hljs-number" >128</span>); <span class= "hljs-comment" >//watermark Image Color and transparency </span> <span class="Hljs-keyword" >new</span> watermarkutils (). Addwatermark (Srcimgpath, Tarimgpath, color, watermarkcontent,
Font);
}
Final Effect
focus on more articles