Java adds a watermark to a picture

Source: Internet
Author: User

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

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.