Recently, I am working on a mall project. I want to add a watermark for the images in the project. ① add an image watermark. ②: Add a text watermark. I hope you can use the next method.Copy codeThe Code is as follows:
Package com. blogs. 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;
Import javax. swing. ImageIcon;
/**
* Image Watermark
*/
Public class ImageUtil {
/**
* @ Param args
*/
Public static void main (String [] args ){
String srcImgPath = "e:/2.png ";
String iconPath = "e: logo.jpg ";
String targerPath = "e:/3.jpg ";
// Add a watermark to the image
ImageUtil. waterMarkImageByIcon (iconPath, srcImgPath, targerPath, 0, 0,
, 0.1f );
// Add a watermark to the image and rotate the watermark-45
// ImageMarkLogoByIcon. markImageByIcon (iconPath, srcImgPath,
// TargerPath2,-45 );
}
/**
* 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
* @ Param width
* Width (compared to left)
* @ Param height
* Height (compared to the top)
* @ Param clarity
* The more transparency (less than 1) the closer it is to 0, the more transparent it is.
*/
Public static void waterMarkImageByIcon (String iconPath, String srcImgPath,
String targerPath, Integer degree, Integer width, Integer height,
Float clarity ){
OutputStream OS = null;
Try {
Image srcImg = ImageIO. read (new File (srcImgPath ));
System. out. println ("width:" + srcImg. getWidth (null ));
System. out. println ("height:" + srcImg. getHeight (null ));
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 = clarity; // transparency
G. setComposite (AlphaComposite. getInstance (AlphaComposite. SRC_ATOP,
Alpha ));
// Specifies the position of the watermark image.
G. drawImage (img, width, height, 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 ("watermark image added! ");
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
Try {
If (null! = OS)
OS. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
/**
* Add a watermark to an image and set the Rotation Angle of the watermark image.
*
* @ Param logoText
* Watermark text
* @ Param srcImgPath
* Source image path
* @ Param targerPath
* Target Image path
* @ Param degree
* Watermark image rotation angle
* @ Param width
* Width (compared to left)
* @ Param height
* Height (compared to the top)
* @ Param clarity
* The more transparency (less than 1) the closer it is to 0, the more transparent it is.
*/
Public static void waterMarkByText (String logoText, String srcImgPath,
String targerPath, Integer degree, Integer width, Integer height,
Float clarity ){
// 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 = clarity;
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, width, height );
G. dispose ();
OS = new FileOutputStream (targerPath );
// Generate an image
ImageIO. write (buffImg, "JPG", OS );
System. out. println ("watermark text added! ");
} 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 ();
}
}
}
}
There is also an image scaling code:
Copy codeThe Code is as follows:
/**
* Image Scaling (the image scales proportionally to the specified size, and the blank part is white)
*
* @ Param srcPath
* Source image path
* @ Param destPath
* Scaled image path
*/
Public static void zoomImage (String srcPath, String destPath, int destHeight, int destWidth ){
Try {
BufferedImage srcBufferedImage = ImageIO. read (new File (srcPath ));
Int imgWidth = destWidth;
Int imgHeight = destHeight;
Int srcWidth = srcBufferedImage. getWidth ();
Int srcHeight = srcBufferedImage. getHeight ();
If (srcHeight> = srcWidth ){
ImgWidth = (int) Math. round (destHeight * 1.0/srcHeight) * srcWidth ));
} Else {
ImgHeight = (int) Math. round (destWidth * 1.0/srcWidth) * srcHeight ));
}
BufferedImage destBufferedImage = new BufferedImage (destWidth, destHeight, BufferedImage. TYPE_INT_RGB );
Graphics2D graphics2D = destBufferedImage. createGraphics ();
Graphics2D. setBackground (Color. WHITE );
Graphics2D. clearRect (0, 0, destWidth, destHeight );
Graphics2D. drawImage (srcBufferedImage. getScaledInstance (imgWidth, imgHeight, Image. SCALE_SMOOTH), (destWidth/2)-(imgWidth/2), (destHeight/2)-(imgHeight/2), null );
Graphics2D. dispose ();
ImageIO. write (destBufferedImage, "JPEG", new File (destPath ));
} Catch (IOException e ){
E. printStackTrace ();
}
}