Java: Adding a watermark to a picture

Source: Internet
Author: User

ImportJava.awt.Color;ImportJava.awt.Graphics2D;ImportJava.awt.Image;ImportJava.awt.image.BufferedImage;ImportJava.io.FileOutputStream;ImportJavax.swing.ImageIcon;ImportCom.sun.image.codec.jpeg.JPEGCodec;ImportCom.sun.image.codec.jpeg.JPEGEncodeParam;ImportCom.sun.image.codec.jpeg.JPEGImageEncoder; Public classWaterMark {/*** Add watermark to image * *@paramFilePath * The path of the picture that needs to be added watermark *@parammarkcontent * Watermark Text *@paramMarkcontentcolor * Color of watermark text *@paramQualnum * Picture quality *@return      */      Public voidCreatemark (String FilePath, String markcontent, Color Markcontentcolor,floatqualnum) {ImageIcon Imgicon=NewImageIcon (FilePath); Image theimg=Imgicon.getimage (); intwidth = theimg.getwidth (NULL); intHeight = theimg.getheight (NULL); BufferedImage Bimage=Newbufferedimage (width, height,bufferedimage.type_int_rgb); Graphics2D g=Bimage.creategraphics ();         G.setcolor (Markcontentcolor);         G.setbackground (Color.White); G.drawimage (theimg,0, 0,NULL); g.DrawString (markcontent, Width/3, HEIGHT/2);//add watermark text and set the contents of the watermark text to appearG.dispose (); Try{FileOutputStream out=NewFileOutputStream (FilePath); JPEGImageEncoder Encoder=Jpegcodec.createjpegencoder (out); JPEGEncodeParam param=Encoder.getdefaultjpegencodeparam (bimage); Param.setquality (Qualnum,true);             Encoder.encode (Bimage, param);                     Out.close (); } Catch(Exception e) {return ; }     }          Public Static voidMain (string[] args) {WaterMark wm=NewWaterMark (); Wm.createmark ("D://map.jpg", "http://www.90che.com", color.white,70f); }}

2007-09-11

Java: Adding a watermark to a picture

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.