|
@@ -13,7 +13,6 @@ import java.awt.*;
|
|
import java.awt.geom.Rectangle2D;
|
|
import java.awt.geom.Rectangle2D;
|
|
import java.awt.image.BufferedImage;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.*;
|
|
import java.io.*;
|
|
-import java.nio.charset.Charset;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* Created by yuanpan on 2017/12/13.
|
|
* Created by yuanpan on 2017/12/13.
|
|
@@ -185,108 +184,10 @@ public class WaterMarkUtils {
|
|
destinationTemp.delete();
|
|
destinationTemp.delete();
|
|
outputStream.flush();
|
|
outputStream.flush();
|
|
File watermarkFile = new File(destinationPath + ".jpg");
|
|
File watermarkFile = new File(destinationPath + ".jpg");
|
|
|
|
+ LOGGER.info("watermark存放目录:{}", watermarkFile.getPath());
|
|
outputStream = new FileOutputStream(watermarkFile);
|
|
outputStream = new FileOutputStream(watermarkFile);
|
|
SystemConstant.writeStream(inputStream, outputStream);
|
|
SystemConstant.writeStream(inputStream, outputStream);
|
|
long end = System.currentTimeMillis();
|
|
long end = System.currentTimeMillis();
|
|
LOGGER.info("生成水印图耗时:{}", (end - start) / 1000 + "s");
|
|
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();
|
|
|
|
- LOGGER.info("准备生成水印图:{}", start);
|
|
|
|
- String srcImgPath = "/Users/king/stmms-ms/static/images/6/SX/2/27e80f728ec8171eb56f834b67e919f9.jpg"; //源图片地址
|
|
|
|
- String srcImgTempPath = "/Users/king/stmms-ms/static/images/6/SX/2/27e80f728ec8171eb56f834b67e919f9Temp.jpg";
|
|
|
|
- String tarImgPath = "/Users/king/stmms-ms/static/watermark/6/SX/2"; //待存储的地址
|
|
|
|
-// String waterMarkContent = "90分"; //水印内容
|
|
|
|
-// Color color = new Color(254, 3, 10, 255); //水印图片色彩以及透明度
|
|
|
|
-// WaterMarkUtils.addWaterMark(srcImgPath, tarImgPath, waterMarkContent, color, font);
|
|
|
|
- //读取指定路径下面的文件
|
|
|
|
- InputStream inputStream = new FileInputStream(srcImgPath);
|
|
|
|
- OutputStream outputStream = new FileOutputStream(srcImgTempPath);
|
|
|
|
- int index = 0;
|
|
|
|
- byte[] bytes = new byte[1024];
|
|
|
|
- byte[] bytesEnc = new byte[1024];
|
|
|
|
- while ((index = inputStream.read(bytes)) != -1) {
|
|
|
|
- //将字节数组的数据全部写入到输出流中
|
|
|
|
- for (int i = 0; i < index; i++) {
|
|
|
|
- //通过异或运算加密
|
|
|
|
- bytesEnc[i] = (byte) (bytes[i] ^ SystemConstant.SECRET_KEY);
|
|
|
|
- }
|
|
|
|
- outputStream.write(bytesEnc, 0, index);
|
|
|
|
- }
|
|
|
|
- String txt = "【" + "1842030002" + "】【" + "test1" + "】【" + String.valueOf(100) + "分】";
|
|
|
|
- String text = new String(txt.getBytes(), Charset.forName("UTF-8"));
|
|
|
|
- File source = new File(srcImgTempPath);
|
|
|
|
- File destination = new File("/Users/king/stmms-ms/static/images/6/SX/2/1842030002temp.jpg");
|
|
|
|
- BufferedImage image = ImageIO.read(source);
|
|
|
|
- int width = image.getWidth();
|
|
|
|
- int height = image.getHeight();
|
|
|
|
-
|
|
|
|
- // determine image type and handle correct transparency
|
|
|
|
- //int imageType = "png".equalsIgnoreCase(type) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
|
|
|
|
- BufferedImage watermarked = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
|
|
|
|
-
|
|
|
|
- // initializes necessary graphic properties
|
|
|
|
- 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);
|
|
|
|
-
|
|
|
|
- // calculate center of the image
|
|
|
|
-// int x = (image.getWidth() - (int) rect.getWidth()) / 2;
|
|
|
|
-// int y = image.getHeight() / 2;
|
|
|
|
-
|
|
|
|
- int x = 0;
|
|
|
|
- int y = image.getHeight() - (int) rect.getHeight() + 50;
|
|
|
|
-
|
|
|
|
- // add text overlay to the image
|
|
|
|
- w.drawString(text, x, y);
|
|
|
|
- //ImageIO.write(watermarked, type, destination);
|
|
|
|
- //w.dispose();
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- JPEGImageWriteParam jepgParams = new JPEGImageWriteParam(null);
|
|
|
|
- jepgParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
|
|
|
|
- jepgParams.setCompressionQuality(1f);
|
|
|
|
- final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
|
|
|
|
- writer.setOutput(new FileImageOutputStream(destination));
|
|
|
|
- writer.write(null, new IIOImage(watermarked, null, null), jepgParams);
|
|
|
|
- writer.dispose();
|
|
|
|
- w.dispose();
|
|
|
|
- source.delete();
|
|
|
|
-
|
|
|
|
- inputStream = new FileInputStream(destination);
|
|
|
|
- destination.delete();
|
|
|
|
- outputStream.flush();
|
|
|
|
- File file = new File(tarImgPath);
|
|
|
|
- if (!file.exists()) {
|
|
|
|
- file.mkdirs();
|
|
|
|
- }
|
|
|
|
- String imageMd5 = "27e80f728ec8171eb56f834b67e919f9";
|
|
|
|
- File sheetFile = new File(tarImgPath + File.separator + imageMd5 + ".jpg");
|
|
|
|
- outputStream = new FileOutputStream(sheetFile);
|
|
|
|
- index = 0;
|
|
|
|
- bytes = new byte[1024];
|
|
|
|
- bytesEnc = new byte[1024];
|
|
|
|
- while ((index = inputStream.read(bytes)) != -1) {
|
|
|
|
- //将字节数组的数据全部写入到输出流中
|
|
|
|
- for (int i = 0; i < index; i++) {
|
|
|
|
- //通过异或运算加密
|
|
|
|
- bytesEnc[i] = (byte) (bytes[i] ^ SystemConstant.SECRET_KEY);
|
|
|
|
- }
|
|
|
|
- outputStream.write(bytesEnc, 0, index);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- long end = System.currentTimeMillis();
|
|
|
|
- LOGGER.info("生成水印图耗时:{}", (end - start) / 1000 + "s");
|
|
|
|
-// WaterMarkUtils.addTextWatermark(text, "jpg", srcImgPath, tarImgPath);
|
|
|
|
- }
|
|
|
|
}
|
|
}
|