|
@@ -1,7 +1,6 @@
|
|
|
package cn.com.qmth.stmms.ms.admin.utils;
|
|
|
|
|
|
import cn.com.qmth.stmms.ms.commons.constant.SystemConstant;
|
|
|
-import cn.com.qmth.stmms.ms.commons.utils.MD5Util;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import javax.imageio.IIOImage;
|
|
@@ -10,8 +9,6 @@ import javax.imageio.ImageWriteParam;
|
|
|
import javax.imageio.ImageWriter;
|
|
|
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
|
|
|
import javax.imageio.stream.FileImageOutputStream;
|
|
|
-import javax.imageio.stream.ImageInputStream;
|
|
|
-import javax.imageio.stream.ImageOutputStream;
|
|
|
import java.awt.*;
|
|
|
import java.awt.geom.Rectangle2D;
|
|
|
import java.awt.image.BufferedImage;
|
|
@@ -131,6 +128,69 @@ public class WaterMarkUtils {
|
|
|
return g.getFontMetrics(g.getFont()).getHeight();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 生成水印图(新)
|
|
|
+ *
|
|
|
+ * @param md5
|
|
|
+ * @param text
|
|
|
+ * @param type
|
|
|
+ * @param sourcePath
|
|
|
+ * @param destinationPath
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static void addTextWatermarkNew(String md5, String text, String type, String sourcePath, String destinationPath) throws IOException {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ LOGGER.info("准备生成水印图:{}", start);
|
|
|
+ String sourceTempPath = sourcePath + "temp.jpg";
|
|
|
+ //读取指定路径下面的文件
|
|
|
+ InputStream inputStream = new FileInputStream(sourcePath + ".jpg");
|
|
|
+ OutputStream outputStream = new FileOutputStream(sourceTempPath);
|
|
|
+ SystemConstant.writeStream(inputStream, outputStream);
|
|
|
+ File source = new File(sourceTempPath);
|
|
|
+ File destinationTemp = new File(destinationPath + "temp.jpg");
|
|
|
+ BufferedImage image = ImageIO.read(source);
|
|
|
+ int width = image.getWidth();
|
|
|
+ int height = image.getHeight();
|
|
|
+
|
|
|
+ BufferedImage watermarked = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
|
|
|
+
|
|
|
+ Graphics2D w = (Graphics2D) watermarked.getGraphics();
|
|
|
+ w.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
+ w.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
|
|
+
|
|
|
+ w.drawImage(image.getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_SMOOTH), 0, 0, null);
|
|
|
+ AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f);
|
|
|
+ w.setComposite(alphaChannel);
|
|
|
+ w.setColor(Color.red);
|
|
|
+ w.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 60));
|
|
|
+ FontMetrics fontMetrics = w.getFontMetrics();
|
|
|
+ Rectangle2D rect = fontMetrics.getStringBounds(text, w);
|
|
|
+
|
|
|
+ int x = 0;
|
|
|
+ int y = image.getHeight() - (int) rect.getHeight() + 50;
|
|
|
+
|
|
|
+ w.drawString(text, x, y);
|
|
|
+
|
|
|
+ JPEGImageWriteParam jepgParams = new JPEGImageWriteParam(null);
|
|
|
+ jepgParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
|
|
|
+ jepgParams.setCompressionQuality(1f);
|
|
|
+ final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
|
|
|
+ writer.setOutput(new FileImageOutputStream(destinationTemp));
|
|
|
+ writer.write(null, new IIOImage(watermarked, null, null), jepgParams);
|
|
|
+ writer.dispose();
|
|
|
+ w.dispose();
|
|
|
+ source.delete();
|
|
|
+
|
|
|
+ inputStream = new FileInputStream(destinationTemp);
|
|
|
+ destinationTemp.delete();
|
|
|
+ outputStream.flush();
|
|
|
+ File watermarkFile = new File(destinationPath + ".jpg");
|
|
|
+ outputStream = new FileOutputStream(watermarkFile);
|
|
|
+ SystemConstant.writeStream(inputStream, outputStream);
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ LOGGER.info("生成水印图耗时:{}", (end - start) / 1000 + "s");
|
|
|
+ }
|
|
|
+
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
// Font font = new Font("Symbol", Font.PLAIN, 60); //水印字体
|
|
|
long start = System.currentTimeMillis();
|