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