|
|
@@ -0,0 +1,181 @@
|
|
|
+package com.poteviohealth.cgp.statistics.utils;
|
|
|
+
|
|
|
+import com.poteviohealth.cgp.common.utils.DateUtils;
|
|
|
+import com.poteviohealth.cgp.statistics.model.indto.PriceUploadDto;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.commons.lang3.time.DateFormatUtils;
|
|
|
+import org.springframework.mock.web.MockMultipartFile;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.net.URL;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 水印处理
|
|
|
+ * @author Qin
|
|
|
+ */
|
|
|
+public class Watermark {
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读取网络图片
|
|
|
+ *
|
|
|
+ * @param path 网络图片地址
|
|
|
+ */
|
|
|
+ public static Image readNetworkPicture(String path) {
|
|
|
+ if (null == path) {
|
|
|
+ throw new RuntimeException("网络图片路径不能为空");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ // 创建一个URL对象,获取网络图片的地址信息
|
|
|
+ URL url = new URL(path);
|
|
|
+ // 将URL对象输入流转化为图片对象 (url.openStream()方法,获得一个输入流)
|
|
|
+ BufferedImage bugImg = ImageIO.read(url.openStream());
|
|
|
+ if (null == bugImg) {
|
|
|
+ System.out.println(path);
|
|
|
+ throw new RuntimeException("网络图片地址不正确");
|
|
|
+ }
|
|
|
+ return bugImg;
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.out.println(path);
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 水印处理
|
|
|
+ *
|
|
|
+ * @param image 图片对象
|
|
|
+ * @param watermark 水印内容(文字水印内容/图片水印的存放路径)
|
|
|
+ */
|
|
|
+ public static BufferedImage manageWatermark(Image image, String watermark) {
|
|
|
+ int imgWidth = image.getWidth(null);
|
|
|
+
|
|
|
+ int imgHeight = image.getHeight(null);
|
|
|
+ BufferedImage bufImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
|
|
|
+ // 加水印:
|
|
|
+ // 创建画笔
|
|
|
+ Graphics2D graphics = bufImg.createGraphics();
|
|
|
+ // 绘制原始图片
|
|
|
+ graphics.drawImage(image, 0, 0, imgWidth, imgHeight, null);
|
|
|
+
|
|
|
+ // 校验水印的类型
|
|
|
+ if (StringUtils.isEmpty(watermark)) {
|
|
|
+ throw new RuntimeException("文字水印内容不能为空");
|
|
|
+ }
|
|
|
+ // 添加文字水印:
|
|
|
+ // 根据图片的背景设置水印颜色
|
|
|
+ graphics.setColor(new Color(255, 255, 255, 255));
|
|
|
+ // 设置字体 画笔字体样式为微软雅黑,加粗,文字大小为45pt
|
|
|
+ int size = imgWidth/25;
|
|
|
+ graphics.setFont(new Font("微软雅黑", Font.PLAIN, size));
|
|
|
+ // 数组长度
|
|
|
+ String[] waterMarkContents = watermark.split("\n");
|
|
|
+ int contentLength = waterMarkContents.length;
|
|
|
+ // 获取水印文字中最长的
|
|
|
+ int maxLength = 0;
|
|
|
+ for (int i = 0; i < contentLength; i++) {
|
|
|
+ int fontLen = getWatermarkLength(waterMarkContents[i], graphics);
|
|
|
+ if (maxLength < fontLen) {
|
|
|
+ maxLength = fontLen;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int j = 0; j < contentLength; j++) {
|
|
|
+ watermark = waterMarkContents[j];
|
|
|
+ int tempX = 10;
|
|
|
+ int tempY = size+10;
|
|
|
+ // 单字符长度
|
|
|
+ int tempCharLen = 0;
|
|
|
+ // 单行字符总长度临时计算
|
|
|
+ int tempLineLen = 0;
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ for (int i = 0; i < watermark.length(); i++) {
|
|
|
+ char tempChar = watermark.charAt(i);
|
|
|
+ tempCharLen = getCharLen(tempChar, graphics);
|
|
|
+ tempLineLen += tempCharLen;
|
|
|
+ if (tempLineLen >= imgWidth) {
|
|
|
+ // 长度已经满一行,进行文字叠加
|
|
|
+ graphics.drawString(sb.toString(), tempX, tempY);
|
|
|
+ // 清空内容,重新追加
|
|
|
+ sb.delete(0, sb.length());
|
|
|
+ tempLineLen = 0;
|
|
|
+ }
|
|
|
+ // 追加字符
|
|
|
+ sb.append(tempChar);
|
|
|
+ }
|
|
|
+ // 通过设置后两个输入参数给水印定位
|
|
|
+ graphics.drawString(sb.toString(), 20, imgHeight - (contentLength - j) * tempY-10);
|
|
|
+ }
|
|
|
+ graphics.dispose();
|
|
|
+ /* // 输出图片
|
|
|
+ try {
|
|
|
+ File file = new File(url);
|
|
|
+ File parentDir = file.getParentFile();
|
|
|
+
|
|
|
+ // 如果父目录不存在,则创建它
|
|
|
+ if (!parentDir.exists()) {
|
|
|
+ parentDir.mkdirs();
|
|
|
+ }
|
|
|
+ FileOutputStream outImgStream = new FileOutputStream(url);
|
|
|
+ ImageIO.write(bufImg, "jpg", outImgStream);
|
|
|
+ outImgStream.flush();
|
|
|
+ outImgStream.close();
|
|
|
+ return "水印添加成功";
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }*/
|
|
|
+
|
|
|
+ return bufImg;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int getCharLen(char c, Graphics2D g) {
|
|
|
+ return g.getFontMetrics(g.getFont()).charWidth(c);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加水印
|
|
|
+ *
|
|
|
+ * @param path 图片路径
|
|
|
+ * @param watermark 水印内容(文字水印内容/图片水印的存放路径)
|
|
|
+ */
|
|
|
+ public static BufferedImage addWatermark(String path, String watermark) {
|
|
|
+ Image image = readNetworkPicture(path);
|
|
|
+ // 添加文字水印
|
|
|
+ return manageWatermark(image,watermark);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取水印文字的长度
|
|
|
+ *
|
|
|
+ * @param watermarkContent 文字水印内容
|
|
|
+ * @param graphics 图像类
|
|
|
+ */
|
|
|
+ private static int getWatermarkLength(String watermarkContent, Graphics2D graphics) {
|
|
|
+ return graphics.getFontMetrics(graphics.getFont()).charsWidth(watermarkContent.toCharArray(), 0, watermarkContent.length());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static MultipartFile createPrice(PriceUploadDto imageConfig) {
|
|
|
+ String url = imageConfig.getUrl();
|
|
|
+ try {
|
|
|
+ String week = DateUtils.getWeek(org.apache.commons.lang3.time.DateUtils.parseDate(imageConfig.getDate(), "yyyy-MM-dd HH:mm:ss"));
|
|
|
+ Date date =org.apache.commons.lang3.time.DateUtils.parseDate(imageConfig.getDate(), "yyyy-MM-dd HH:mm:ss");
|
|
|
+ String dateVal = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
|
|
|
+ String textWatermark = week + "\n" + dateVal + "\n" + imageConfig.getPunchLatitude() + "," + imageConfig.getPunchLongitude() + "\n" + imageConfig.getPunchAddress();
|
|
|
+ ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
|
|
+ ImageIO.write(addWatermark(url, textWatermark), "jpg", bo );
|
|
|
+ //转换为MultipartFile
|
|
|
+ MultipartFile multipartFile = new MockMultipartFile("test.jpg", bo.toByteArray());
|
|
|
+
|
|
|
+ return multipartFile;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|