package org.jeecg.modules.utils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class WaterMarkUtil {
private static final float alpha = 0.8f;
private static int positionWidth = 100;
private static int positionHeight = 300;
private static final Font font = new Font("宋体", Font.BOLD, 300);
private static final Color color = Color.white;
public static void markImage(String text, String srcImgPath, String targetPath) {
markImage(text, srcImgPath, targetPath, null);
}
public static void markImage(String text, String srcImgPath, String targetPath, Integer degree) {
OutputStream os = null;
try {
String type = srcImgPath.substring(srcImgPath.indexOf(".") + 1, srcImgPath.length());
Image srcImg = ImageIO.read(new File(srcImgPath));
int imgWidth = srcImg.getWidth(null);
int imgHeight = srcImg.getHeight(null);
BufferedImage buffImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImg.getScaledInstance(imgWidth, imgHeight, Image.SCALE_SMOOTH), 0, 0, null);
if (null != degree) {
g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
}
g.setColor(color);
g.setFont(font);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
positionWidth = 50;
positionHeight = imgHeight-30;
g.drawString(text,positionWidth, positionHeight);
g.dispose();
os = new FileOutputStream(targetPath);
ImageIO.write(buffImg, type.toUpperCase(), os);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != os){
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void markImageByIO(String text, InputStream inputStream, OutputStream outputStream,
Integer degree, String typeName) {
try {
Image srcImg = ImageIO.read(inputStream);
int imgWidth = srcImg.getWidth(null);
int imgHeight = srcImg.getHeight(null);
BufferedImage buffImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImg.getScaledInstance(imgWidth, imgHeight, Image.SCALE_SMOOTH), 0, 0, null);
if (null != degree) {
g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
}
g.setColor(color);
g.setFont(font);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
int positionWidth = 100;
int positionHeight = 100;
g.drawString(text, positionWidth, positionHeight);
AffineTransform at = new AffineTransform();
at.scale(20, 20);
g.setTransform(at);
g.dispose();
ImageIO.write(buffImg, typeName.toUpperCase(), outputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
}