Copy the code code 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;
/**
* Picture watermark
*/
public class ImageUtil {
/**
* @param args
*/
public static void main(String[] args) {
String srcImgPath = "e:/2.png";
String iconPath = "e://logo.jpg";
String targetPath = "e:/3.jpg";
//Add watermark to image
ImageUtil.waterMarkImageByIcon(iconPath, srcImgPath, targerPath, 0, 0,
, 0.1f);
//Add watermark to the image, watermark rotation -45
// ImageMarkLogoByIcon.markImageByIcon(iconPath, srcImgPath,
// targetPath2, -45);
}
/**
* Add watermark to the picture and set the rotation angle of the watermark picture
*
* @param iconPath
*Watermark image path
* @param srcImgPath
* Source image path
* @param targetPath
* Target image path
* @param degree
* Watermark image rotation angle
* @param width
* Width (compared to left)
* @param height
* Height (compared to top)
* @param clarity
* The closer the transparency (a number less than 1) 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);
//Get the brush object
// Graphics g= buffImg.getGraphics();
Graphics2D g = buffImg.createGraphics();
//Set jagged edge processing for line segments
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 of the watermark image. The watermark is usually gif or png, so the transparency can be set.
ImageIcon imgIcon = new ImageIcon(iconPath);
// Get the Image object.
Image img = imgIcon.getImage();
float alpha = clarity; // transparency
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
//Indicates the location of the watermark image
g.drawImage(img, width, height, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g.dispose();
os = new FileOutputStream(targerPath);
// Generate picture
ImageIO.write(buffImg, "JPG", os);
System.out.println("Adding watermark image completed!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Add watermark to the picture and set the rotation angle of the watermark picture
*
* @param logoText
* Watermark text
* @param srcImgPath
* Source image path
* @param targetPath
* Target image path
* @param degree
* Watermark image rotation angle
* @param width
* Width (compared to left)
* @param height
* Height (compared to top)
* @param clarity
* The closer the transparency (number less than 1) 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) {
//The path of the main 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);
//Get the brush object
// Graphics g= buffImg.getGraphics();
Graphics2D g = buffImg.createGraphics();
//Set jagged edge processing for line segments
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 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));
// The first parameter -> the content of the setting, the next two parameters -> the coordinate position (x, y) of the text on the picture.
g.drawString(logoText, width, height);
g.dispose();
os = new FileOutputStream(targerPath);
// Generate pictures
ImageIO.write(buffImg, "JPG", os);
System.out.println("Add watermark text completed!");
} 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 the code code as follows:
/**
* Image scaling (the image is scaled to the specified size proportionally, and the blank parts are filled with white)
*
* @param srcPath
* Source image path
* @param destPath
* Image path after scaling
*/
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();
}
}