These two days according to the needs of the picture upload to add watermark, actually say again do not know Java can also do so, since there is a request I went to find information research, now share it, hope to help needy brothers.
To add a watermark to ordinary pictures and add watermark is not the same, to add watermark to the ordinary picture is written in Java, the GIF4J frame, the framework in csdn a lot of can be downloaded, it is recommended to download the cracked version, because the original jar package will have its own A watermark is not going to go .
Import Java.awt.*;import java.awt.image.bufferedimage;import Java.io.*;import Javax.imageio.imageio;import Javax.swing.ImageIcon;//This is the class of the GIF4J frameworkImport Com.gif4j.gifdecoder;import com.gif4j.gifencoder;import com.gif4j.gifimage;import Com.gif4j.GifTransformer; Import Com.gif4j.textpainter;import com.gif4j.watermark;/** * Created by zxd on 2018/1/18. */public class Imageremarkutil {//Watermark TransparencyPrivate float alpha = 0.5f;//Watermark Landscape positionprivate int positionwidth = 150;//Watermark Portrait Positionprivate int positionheight = 300;//Watermark widthprivate int width = 80;//High watermarkprivate int height = 80;//Watermark Text fontPrivate font font = new Font ("Arial", Font.Bold, 72);//Watermark text color Private color color = color.red; /*********** normal picture watermark ***********//** * * @param alpha * Watermark Transparency * @param positionwidth * Watermark Landscape Position * @param positionheight * Watermark Portrait position * @param font * watermark Text font * @param Color * Watermark Text colors */public void setimagemarkoptions (float alpha, int positionwidth, int positionheight,int width,int height, font font, color color) {if (alpha! = 0.0f) This.alpha = Alpha; if (positionwidth! = 0) this.positionwidth = positionwidth; if (positionheight! = 0) this.positionheight = positionheight; if (height! = 0) this.height = height; if (width! = 0) This.width = width; if (font! = null) This.font = font; if (color = null) This.color = color; }/** * Add watermark image to Picture * * @param iconpath * Watermark Picture Path * @param srcimgpath * source Picture Road Diameter * @param targerpath * Target picture path * *public void Markimagebyicon (string iconpath, String Srcimgpath, String targerpath) {Markimagebyicon (IconPath, Srcimgpath, Targerpath, NULL); }/** * Add watermark picture to picture, can set watermark picture rotation angle * * @param iconpath * Watermark Picture Path * @param srcimgpath * SOURCE Picture Path * @param targerpath * Destination picture path * @param degree * watermark picture rotation angle */public void Markimagebyicon (string iconpath, String Srcimgpath, String Targerpath, Integer degree) {outputstream OS = null; try {Image srcimg = imageio.read (new File (Srcimgpath)); BufferedImage buffimg = new BufferedImage (Srcimg.getwidth (null), srcimg.getheight (null), BufferedImage. TYPE_INT_RGB);//1, get the Brush objectGraphics2D g = buffimg.creategraphics ();///2, set the jagged edge processing for the segmentG.setrenderinghint (Renderinghints.key_interpolation, renderinghints.value_interpolation_bilinear); G.drawimage (srcimg.getscaledinstance (Srcimg.getwidth (null), srcimg. GetHeight (NULL), Image.scale_smooth), 0, 0, NULL);//3, set watermark rotationif (null! = degree) {g.rotate (Math.toradians (degree), (double) buffimg.getwidth ()/ 2, (double) buffimg.getheight ()/2); }//4, watermark image of the path watermark image is generally gif or PNG, so you can set the transparencyImageIcon Imgicon = new ImageIcon (IconPath);///5, Get Image object. Image img = imgicon.getimage (); G.setcomposite (Alphacomposite.getinstance (alphacomposite.src_atop, Alpha)); Integer X = srcimg.getwidth (null); Integer Y = srcimg.getheight (null); //6, the location of the watermark pictureG.drawimage (IMG, x (positionwidth+width), Y (positionheight+height), width,height,null); G.setcomposite (Alphacomposite.getinstance (alphacomposite.src_over));//7. Releasing ResourcesG.dispose ();///8, create pictureOS = new FileOutputStream (Targerpath); Imageio.write (buffimg, "JPG", OS); SYSTEM.OUT.PRINTLN ("Image completion add watermark image"); } catch (Exception e) {e.printstacktrace (); } finally {try {if (null! = OS) os.close (); } catch (Exception e) {e.printstacktrace (); } } }/** * Add watermark text to Picture * * @param logotext * Watermark Text * @param srcimgpath * source picture path * @param targerpath * Target image path */public void Markimagebytext (string logotext, String Srcimgpath, String targerpath) { Markimagebytext (Logotext, Srcimgpath, Targerpath, NULL); }/** * Add watermark Text to picture, can set the rotation angle of watermark text * * @param logotext * @param srcimgpath * @param targerpath * @p Aram Degree * /public void Markimagebytext (string logotext, String Srcimgpath, String Targerpath, Integer degree) {InputStream is = null; OutputStream OS = null; try {//1, source pictureImage srcimg = Imageio.read (new File (Srcimgpath)); BufferedImage buffimg = new BufferedImage (Srcimg.getwidth (null), srcimg.getheight (null), BufferedImage. TYPE_INT_RGB);//2, get the Brush objectGraphics2D g = buffimg.creategraphics ();///3, set the jagged edge processing for the segmentG.setrenderinghint (Renderinghints.key_interpolation, renderinghints.value_interpolation_bilinear); G.drawimage (srcimg.getscaledinstance (Srcimg.getwidth (null), srcimg. GetHeight (NULL), Image.scale_smooth), 0, 0, NULL);//4, set watermark rotationif (null! = degree) {g.rotate (Math.toradians (degree), (double) buffimg.getwidth ()/ 2, (double) buffimg.getheight ()/2); }///5, Set watermark text colorG.setcolor (color);//6, Set watermark text fontG.setfont (font);///7, set watermark text transparencyG.setcomposite (Alphacomposite.getinstance (alphacomposite.src_atop, Alpha));//8, the contents of the first parameter, and the coordinates of the text on the picture (x, y), after two parameters .g.DrawString (Logotext, Positionwidth, positionheight);//9. Releasing ResourcesG.dispose ();///10, create pictureOS = new FileOutputStream (Targerpath); Imageio.write (buffimg, "JPG", OS); SYSTEM.OUT.PRINTLN ("Image completion Add watermark text"); } catch (Exception e) {e.printstacktrace (); } finally {try {if (null! = is) is.close (); } catch (Exception e) {e.printstacktrace (); } try {if (null! = OS) os.close (); } catch (Exception e) {e.printstacktrace (); } } }/*********** map and Watermark ************//** * Zoom GIF image, direct file files, can be set and high * /public void makegif (file src, file dest, int width, int height) throws IOException {Gifimage gifimage = Gifdecode R.decode (SRC);//Create a Gifimage object.Gifimage resizeimg = Giftransformer.resize (gifimage, width, height, true); Gifencoder.encode (resizeimg, dest); }//Zoom GIF image, direct file path, can set width and heightpublic void Makegif (string src, string dest, int width, int height) throws IOException {Gifimage gifimage = Gifde Coder.decode (New File (SRC));//Create a Gifimage object.Makegif (new file (src), new file (dest), Gifimage.getscreenwidth ()/2, Gifimage.getscreenheight ()/2); }//Zoom GIF image, transfer files file, not set width and heightpublic void makegif (file src, file dest) throws IOException {Gifimage gifimage = Gifdecoder.decode (src);//Create a Gifimage object.Makegif (SRC, dest, gifimage.getscreenwidth ()/2, Gifimage.getscreenheight ()/2); }//Zoom GIF image, pass file path, not set width and heightpublic void Makegif (string src, String dest) throws IOException {makegif (new file (src), new file (dest)); }/** * Add text watermark in the motion map * *public void addtextwatermarktogif (file src, String watermarktext, File dest) throws IOException {//Watermark initialization, settings (font, style, size, color)Textpainter textpainter = new Textpainter ("bold", Font.Italic, 12)); Textpainter.setoutlinepaint (Color.White); BufferedImage Renderedwatermarktext = textpainter.renderstring (Watermarktext, true);//Picture ObjectGifimage GF = Gifdecoder.decode (src);//Get picture sizeint IW = Gf.getscreenwidth (); int ih = Gf.getscreenheight ();//Get watermark sizeint tw = Renderedwatermarktext.getwidth (); int th = Renderedwatermarktext.getheight ();//Watermark locationPoint P = new Point (); p.x = iw-tw-5; P.Y = ih-th-4;//Ga ShuiyinWatermark Watermark = new Watermark (Renderedwatermarktext, p); GF = watermark.apply (Gifdecoder.decode (SRC), true);//OutputGifencoder.encode (GF, dest); }/** * Add picture watermark in the motion map * *public void addimagewatermarktogif (file src, String watermarkpath, file dest) {try{bufferedimage Rende Redwatermarktext = Imageio.read (new File (Watermarkpath));//Picture ObjectGifimage GF = Gifdecoder.decode (src);//Get picture sizeint IW = Gf.getscreenwidth (); int ih = Gf.getscreenheight ();//Get watermark sizeint tw = Renderedwatermarktext.getwidth (); int th = Renderedwatermarktext.getheight ();//Watermark locationPoint P = new Point (); p.x = iw-tw-20; P.Y = ih-th-20;//Ga ShuiyinWatermark Watermark = new Watermark (Renderedwatermarktext, p);//Watermark TransparencyWatermark.settransparency (1); GF = watermark.apply (Gifdecoder.decode (SRC), false);//OutputGifencoder.encode (GF, dest); } catch (IOException e) {e.printstacktrace (); }} public static void Main (string[] args) {//path to watermark imageString Srcimgpath = "D:/1.jpg"; String Logotext = "copy is invalid";//Image watermark PathString IconPath = "D:/2.jpg";//Add the output path of the watermark fileString Targertextpath = "D:/qie_text.jpg"; String targerTextPath2 = "D:/qie_text_rotate.jpg"; String Targericonpath = "D:/qie_icon.jpg"; String targerIconPath2 = "D:/qie_icon_rotate.jpg"; System.out.println ("Add watermark text to Picture ...");//Add watermark text to the pictureMarkimagebytext (Logotext, Srcimgpath, Targertextpath);//Add watermark text to picture, watermark text rotated -45Markimagebytext (Logotext, Srcimgpath, TargerTextPath2,-45); System.out.println ("Add watermark text to picture end ..."); System.out.println ("Add watermark image to image to start ..."); Setimagemarkoptions (0.3f, 1, 1, NULL, NULL);//Add a watermark image to a pictureMarkimagebyicon (IconPath, Srcimgpath, Targericonpath);//Add watermark image to picture, watermark picture rotated -45Markimagebyicon (IconPath, Srcimgpath, TargerIconPath2,-45); System.out.println ("Add watermark to Picture end ...");//Dynamic graph Add watermark (add watermark file, add watermark, add output file)Addtextwatermarktogif ("D:\\10.gif"), "Copy invalid", New file ("D:\\11.gif")); Addimagewatermarktogif (New file ("D:\\gif\\10.gif"), "D:\\gif\\3.png", New File ("D:\\gif\\4.gif")); }}
There are ordinary pictures to add watermarks and dynamic images to add watermarks, ordinary images to add a watermark method if the dynamic map can be added to the success, but the dynamic map into a static, dynamic map to add a watermark method if the transmission is a normal picture, will be directly error.
I've tried all these things, and now I'm taking notes here, and I want to help the brothers in need.
Java adds watermarks to pictures and motion diagrams