Add watermarks to images using Java

Source: Internet
Author: User

After checking the information, I found that it is very convenient to implement the watermark in java. The watermark can be an image or text, and can be rotated from different angles, you can write a code to process your images in batches.
 
Directory:
Image Watermark
Text watermark
[1]. image watermarks
 
Java code
Package michael. io. image;
 
Import java. awt. AlphaComposite;
Import java. awt. Graphics2D;
Import java. awt. Image;
Import java. awt. RenderingHints;
Import java. awt. image. BufferedImage;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. io. InputStream;
Import java. io. OutputStream;
 
Import javax. imageio. ImageIO;
Import javax. swing. ImageIcon;
 
Import com.sun.image.codec.jpeg. Unzip codec;
Import com.sun.image.codec.jpeg. Unzip imagedecoder;
Import com.sun.image.codec.jpeg. encode imageencoder;
 
/**
* Image Watermark
* @ Blog http://sjsky.iteye.com
* @ Author Michael
*/
Public class ImageMarkLogoByIcon {
 
/**
* @ Param args
*/
Public static void main (String [] args ){
String srcImgPath = "d:/test/michael/myblog_01.png ";
String iconPath = "d:/test/michael/blog_logo.png ";
String targerPath = "d:/test/michael/img_mark_icon.jpg ";
String targerPath2 = "d:/test/michael/img_mark_icon_rotate.jpg ";
// Add a watermark to the image
ImageMarkLogoByIcon. markImageByIcon (iconPath, srcImgPath, targerPath );
// Add a watermark to the image and rotate the watermark-45
ImageMarkLogoByIcon. markImageByIcon (iconPath, srcImgPath, targerPath2,
-45 );
 
}
 
/**
* Add a watermark to an image
* @ Param iconPath: watermark image path
* @ Param srcImgPath: source image path
* @ Param targerPath: Target Image path
*/
Public static void markImageByIcon (String iconPath, String srcImgPath,
String targerPath ){
MarkImageByIcon (iconPath, srcImgPath, targerPath, null );
}
 
/**
* Add a watermark to an image and set the Rotation Angle of the watermark image.
* @ Param iconPath: watermark image path
* @ Param srcImgPath: source image path
* @ Param targerPath: Target Image path
* @ Param degree watermark image rotation angle
*/
Public static void markImageByIcon (String iconPath, String srcImgPath,
String targerPath, Integer degree ){
OutputStream OS = null;
Try {
Image srcImg = ImageIO. read (new File (srcImgPath ));
 
BufferedImage buffImg = new BufferedImage (srcImg. getWidth (null ),
SrcImg. getHeight (null), BufferedImage. TYPE_INT_RGB );
 
// Obtain the paint brush object
// Graphics g = buffImg. getGraphics ();
Graphics2D g = buffImg. createGraphics ();
 
// Set the processing of the jagged edge of a line segment
G. setRenderingHint (RenderingHints. KEY_INTERPOLATION,
RenderingHints. VALUE_INTERPOLATION_BILINEAR );
 
G. drawImage (srcImg. getScaledInstance (srcImg. getWidth (null), srcImg
. GetHeight (null), Image. SCALE_SMOOTH), 0, 0, null );
 
If (null! = Degree ){
// Set watermark Rotation
G. rotate (Math. toRadians (degree ),
(Double) buffImg. getWidth ()/2, (double) buffImg
. GetHeight ()/2 );
}
 
// The path watermark of the watermark image is generally gif or png, so that transparency can be set.
ImageIcon imgIcon = new ImageIcon (iconPath );
 
// Obtain the Image object.
Image img = imgIcon. getImage ();
 
Float alpha = 0.5f; // transparency
G. setComposite (AlphaComposite. getInstance (AlphaComposite. SRC_ATOP,
Alpha ));
 
// Specifies the position of the watermark image.
G. drawImage (img, 150,300, null );
 
G. setComposite (AlphaComposite. getInstance (AlphaComposite. SRC_OVER ));
 
G. dispose ();
 
OS = new FileOutputStream (targerPath );
 
// Generate an image
ImageIO. write (buffImg, "JPG", OS );
 
System. out. println ("image complete adding Icon seal ...... ");
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
Try {
If (null! = OS)
OS. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
}
Original Image myblog_01.jpg:


 
Waterprint image blog_logo.png:

Watermarked:


 
Rotate the watermark icon:


 
 
[2] Text watermarks
Java code
Package michael. io. image;
 
Import java. awt. AlphaComposite;
Import java. awt. Color;
Import java. awt. Font;
Import java. awt. Graphics2D;
Import java. awt. Image;
Import java. awt. RenderingHints;
Import java. awt. image. BufferedImage;
Import java. io. File;
Import java. io. FileOutputStream;
Import java. io. InputStream;
Import java. io. OutputStream;
 
Import javax. imageio. ImageIO;
 
/**
* Text watermark
* @ Blog http://sjsky.iteye.com
* @ Author Michael
*/
Public class ImageMarkLogoByText {
 
/**
* @ Param args
*/
Public static void main (String [] args ){
String srcImgPath = "d:/test/michael/myblog_01.jpg ";
String logoText = "[test the http://sjsky.iteye.com of a text watermark]";
String targerPath = "d:/test/michael/img_mark_text.jpg ";
 
String targerPath2 = "d:/test/michael/img_mark_text_rotate.jpg ";
 
// Add a watermark to the image
ImageMarkLogoByText. markByText (logoText, srcImgPath, targerPath );
 
// Add a watermark to the image and rotate the watermark-45
ImageMarkLogoByText. markByText (logoText, srcImgPath, targerPath2,-45 );
}
 
/**
* Add a watermark to an image
* @ Param logoText
* @ Param srcImgPath
* @ Param targerPath
*/
Public static void markByText (String logoText, String srcImgPath,
String targerPath ){
MarkByText (logoText, srcImgPath, targerPath, null );
}
 
/**
* Add a watermark to an image and set the watermark Rotation Angle
* @ Param logoText
* @ Param srcImgPath
* @ Param targerPath
* @ Param degree
*/
Public static void markByText (String logoText, String srcImgPath,
String targerPath, Integer degree ){
// Path of the master Image
InputStream is = null;
OutputStream OS = null;
Try {
Image srcImg = ImageIO. read (new File (srcImgPath ));
 
BufferedImage buffImg = new BufferedImage (srcImg. getWidth (null ),
SrcImg. getHeight (null), BufferedImage. TYPE_INT_RGB );
 
// Obtain the paint brush object
// Graphics g = buffImg. getGraphics ();
Graphics2D g = buffImg. createGraphics ();
 
// Set the processing of the jagged edge of a line segment
G. setRenderingHint (RenderingHints. KEY_INTERPOLATION,
RenderingHints. VALUE_INTERPOLATION_BILINEAR );
 
G. drawImage (srcImg. getScaledInstance (srcImg. getWidth (null), srcImg
. GetHeight (null), Image. SCALE_SMOOTH), 0, 0, null );
 
If (null! = Degree ){
// Set watermark Rotation
G. rotate (Math. toRadians (degree ),
(Double) buffImg. getWidth ()/2, (double) buffImg
. GetHeight ()/2 );
}
 
// Set the color
G. setColor (Color. red );
 
// Set Font
G. setFont (new Font ("", Font. BOLD, 30 ));
 
Float alpha = 0.5f;
G. setComposite (AlphaComposite. getInstance (AlphaComposite. SRC_ATOP,
Alpha ));
 
// Parameter 1-> set content, followed by two parameters-> Coordinate Position (x, y) of the text on the image ).
G. drawString (logoText 150,300 );
 
G. dispose ();
 
OS = new FileOutputStream (targerPath );
 
// Generate an image
ImageIO. write (buffImg, "JPG", OS );
 
System. out. println ("image after adding a text stamp ...... ");
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
Try {
If (null! = Is)
Is. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
Try {
If (null! = OS)
OS. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
}
The text watermark is added as follows:


 
The text after rotation is as follows:

 

Reposted from: Michael blog

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.