Java Implementation Picture watermark

Source: Internet
Author: User

We can often see some pictures on the internet have watermarks, I saw some tutorials, but also their own simple implementation of the next, and then record!

The process of adding a watermark

1. Create a picture Cache object (Bufferimage)---> 2. Create a Java Drawing Tool object (GRAPHICS2D)---> 3. Use the drawing tool object to place the original drawing on the cached picture object

---> 4. Use the drawing tool object to place the watermark text picture on the cached Picture object---> 5. Use the image Encoding tool class to save the cached image output to your computer

 Public classTextwatermark { Public Static voidMain (string[] args) {//TODO auto-generated Method StubFile File =NewFile ("D://big59000.jpg"); Textmark (file,"HELLO World"); }    /**     * @paramfile * Picture Files Object *@paramText * Added watermark text*/     Public Static voidTextmark (file file, String text) {Try{image Image=imageio.read (file); BufferedImage Bufferimage=NewBufferedImage (Image.getwidth (NULL), Image.getheight (NULL), BUFFEREDIMAGE.TYPE_INT_RGB);//1. Create a picture cache objectgraphics2d G= Bufferimage.creategraphics ();//2. Creating a Java Drawing Tool objectg.drawimage (Image,0, 0, Image.getwidth (NULL), Image.getheight (NULL),NULL);//3. Draw the original artwork to the picture cache object using the drawing toolG.setfont (NewFont ("Microsoft Jas Black", font.bold,30)); G.setcolor (color.green);//set the font and color of the watermark textG.setcomposite (Alphacomposite.getinstance (Alphacomposite.src_atop, 0.3f)); g.DrawString (text,200, 50);//4. Add text to the cached picture objectG.dispose (); OutputStream OS=NewFileOutputStream ("D://watertext.jpg"); Imageio.write (Bufferimage,"JPG", OS);//Save cached pictures locally with the image encoding tool                    } Catch(IOException e) {e.printstacktrace (); }    }}

Implementation effect: (the color position of the text can adjust itself)

Java Implementation Picture watermark

Related Article

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.