Add a watermark to the demo as follows, after the new project test if you encounter problems, you can perform the last picture to indicate the change.
Import Java.awt.AlphaComposite;
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 java.io.IOException;
Import Java.io.OutputStream;
Import Javax.imageio.ImageIO;
public class Demo {
public static void Main (string[] args) {
TODO auto-generated Method Stub
File File = new file ("D://img/1.jpg");
Textmark (file, "HELLO World");
}
/**
* @param file
* Picture File Object
* @param text
* Added watermark Text
*/
public static void Textmark (file file, String text) {
try {
Image image = Imageio.read (file);
BufferedImage bufferimage = new BufferedImage (Image.getwidth (null), image.getheight (null), Bufferedimage.type_int_ RGB);//1. Create a picture Cache object
Graphics2D g = bufferimage.creategraphics (); 2. To create a Java drawing tool object
G.drawimage (image, 0, 0, image.getwidth (null), image.getheight (null), NULL),//3. Draws the original artwork through the drawing tool into the picture cache object
G.setfont (New Font ("Microsoft Jas Black", font.bold,30));
G.setcolor (Color.green);//Set the font and color of the watermark text
G.setcomposite (Alphacomposite.getinstance (Alphacomposite.src_atop, 0.3f));
g.DrawString (text, 200, 50);//4. Adding text to a cached picture object
G.dispose ();
OutputStream OS = new FileOutputStream ("d://new.jpg");
Imageio.write (Bufferimage, "JPG", OS);//Use the image encoding tool to save cached pictures locally
} catch (IOException e) {
E.printstacktrace ();
}
}
}
The main error is import Javax.imageio.ImageIO;
Java adds a watermark to a picture