|
@@ -5,6 +5,10 @@ import com.google.zxing.BarcodeFormat;
|
|
import com.google.zxing.EncodeHintType;
|
|
import com.google.zxing.EncodeHintType;
|
|
import com.google.zxing.MultiFormatWriter;
|
|
import com.google.zxing.MultiFormatWriter;
|
|
import com.google.zxing.common.BitMatrix;
|
|
import com.google.zxing.common.BitMatrix;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
import org.springframework.util.StringUtils;
|
|
import org.springframework.util.StringUtils;
|
|
import sun.misc.BASE64Encoder;
|
|
import sun.misc.BASE64Encoder;
|
|
|
|
|
|
@@ -12,11 +16,13 @@ import javax.imageio.ImageIO;
|
|
import javax.swing.*;
|
|
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
import java.awt.image.BufferedImage;
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
|
-import java.io.File;
|
|
|
|
|
|
+import java.io.*;
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
|
|
+import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
/**
|
|
/**
|
|
* Description
|
|
* Description
|
|
@@ -24,9 +30,18 @@ import java.util.Map;
|
|
* @author : Charles
|
|
* @author : Charles
|
|
* @date : 2020/3/16
|
|
* @date : 2020/3/16
|
|
*/
|
|
*/
|
|
|
|
+@Slf4j
|
|
|
|
+@Component
|
|
public class ImageUtils {
|
|
public class ImageUtils {
|
|
|
|
|
|
- public static String getImageURL(String dirName, String src, String domain) {
|
|
|
|
|
|
+ private static String active;
|
|
|
|
+
|
|
|
|
+ @Value("${spring.profiles.active}")
|
|
|
|
+ public void setActive(String actives) {
|
|
|
|
+ active = actives;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String getImageURL(String dirName, String src, String domain) {
|
|
return getImageURL(dirName, src, 0,domain);
|
|
return getImageURL(dirName, src, 0,domain);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -312,4 +327,334 @@ public class ImageUtils {
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private static String addImage(String srcImgPath, String addImage, Integer x, Integer y) {
|
|
|
|
+ FileOutputStream outImgStream = null;
|
|
|
|
+ String imagePath = "/mnt/newdatadrive/data/runtime/jar-instance/zplma/tempImage";
|
|
|
|
+ if ("dev".equals(active)) {
|
|
|
|
+ imagePath = "D:\\WorkSpace\\file\\tempImage\\";
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ // 读取原图片信息
|
|
|
|
+ File srcImgFile = new File(srcImgPath);
|
|
|
|
+ Image srcImg = ImageIO.read(srcImgFile);
|
|
|
|
+ int srcImgWidth = srcImg.getWidth(null);
|
|
|
|
+ int srcImgHeight = srcImg.getHeight(null);
|
|
|
|
+ BufferedImage buffImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ // 机构名称图片
|
|
|
|
+ URL url = new URL(addImage);
|
|
|
|
+ InputStream inputStream = url.openStream();
|
|
|
|
+ BufferedImage authImage = ImageIO.read(inputStream);
|
|
|
|
+
|
|
|
|
+ // 1、得到画笔对象
|
|
|
|
+ Graphics2D g = buffImg.createGraphics();
|
|
|
|
+
|
|
|
|
+ // 2、设置对线段的锯齿状边缘处理
|
|
|
|
+ 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);
|
|
|
|
+ g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
|
|
|
|
+ 1f));
|
|
|
|
+
|
|
|
|
+ // 6、添加图片的位置
|
|
|
|
+ g.drawImage(authImage, x, y, null);
|
|
|
|
+ g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
|
|
|
|
+ // 7、释放资源
|
|
|
|
+ g.dispose();
|
|
|
|
+
|
|
|
|
+ //将图片写入流中
|
|
|
|
+ String fileType = srcImgPath.substring(srcImgPath.lastIndexOf(".") + 1);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ // 输出图片
|
|
|
|
+ UUID uuid = UUID.randomUUID();
|
|
|
|
+ imagePath += uuid + "." + fileType;
|
|
|
|
+ outImgStream = new FileOutputStream(imagePath);
|
|
|
|
+ ImageIO.write(buffImg, fileType, outImgStream);
|
|
|
|
+ outImgStream.flush();
|
|
|
|
+ outImgStream.close();
|
|
|
|
+
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ try {
|
|
|
|
+ if (null != outImgStream) {
|
|
|
|
+ outImgStream.close();
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return imagePath;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static String addWord(String srcImgPath, String content, String type, String color, int style, int size, int x, int y) {
|
|
|
|
+ FileOutputStream outImgStream = null;
|
|
|
|
+ String imagePath = "/mnt/newdatadrive/data/runtime/jar-instance/zplma/tempImage";
|
|
|
|
+ if ("dev".equals(active)) {
|
|
|
|
+ imagePath = "D:\\WorkSpace\\file\\tempImage\\";
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ // 读取原图片信息
|
|
|
|
+ File srcImgFile = new File(srcImgPath);
|
|
|
|
+ Image srcImg = ImageIO.read(srcImgFile);
|
|
|
|
+ int srcImgWidth = srcImg.getWidth(null);
|
|
|
|
+ int srcImgHeight = srcImg.getHeight(null);
|
|
|
|
+ // 加水印
|
|
|
|
+ BufferedImage buffImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
|
|
|
|
+ Graphics2D g = buffImg.createGraphics();
|
|
|
|
+ g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
|
|
|
|
+ //根据图片的背景设置水印颜色
|
|
|
|
+ String[] split = color.split(",");
|
|
|
|
+ g.setColor(new Color(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2])));
|
|
|
|
+ //设置旋转角度
|
|
|
|
+ g.rotate(0, 0, 0);
|
|
|
|
+ //设置水印透明度
|
|
|
|
+ g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1F));
|
|
|
|
+
|
|
|
|
+ // 画认证编号
|
|
|
|
+ Font font = new Font(type, style, size);
|
|
|
|
+
|
|
|
|
+ g.setFont(font);
|
|
|
|
+ g.drawString(content, x, y);
|
|
|
|
+ g.dispose();
|
|
|
|
+
|
|
|
|
+ String fileType = srcImgPath.substring(srcImgPath.lastIndexOf(".") + 1);
|
|
|
|
+
|
|
|
|
+ // 输出图片
|
|
|
|
+ UUID uuid = UUID.randomUUID();
|
|
|
|
+ imagePath += uuid + "." + fileType;
|
|
|
|
+ outImgStream = new FileOutputStream(imagePath);
|
|
|
|
+ ImageIO.write(buffImg, fileType, outImgStream);
|
|
|
|
+ outImgStream.flush();
|
|
|
|
+ outImgStream.close();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ if (outImgStream != null) {
|
|
|
|
+ try {
|
|
|
|
+ outImgStream.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return imagePath;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static String addQrCode(String srcImgPath, String qrCodeLink, int qrCodeSize, int x, int y){
|
|
|
|
+ FileOutputStream outImgStream = null;
|
|
|
|
+ String imagePath = "/mnt/newdatadrive/data/runtime/jar-instance/zplma/tempImage";
|
|
|
|
+ if ("dev".equals(active)) {
|
|
|
|
+ imagePath = "D:\\WorkSpace\\file\\tempImage\\";
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+
|
|
|
|
+ // 读取原图片信息
|
|
|
|
+ File srcImgFile = new File(srcImgPath);
|
|
|
|
+ Image srcImg = ImageIO.read(srcImgFile);
|
|
|
|
+ int srcImgWidth = srcImg.getWidth(null);
|
|
|
|
+ int srcImgHeight = srcImg.getHeight(null);
|
|
|
|
+ // 加水印
|
|
|
|
+ BufferedImage buffImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //二维码图片
|
|
|
|
+// int qrCodeSize = getQrCodeSize(srcWidth);
|
|
|
|
+ BufferedImage qrCodeImg = createRealQrCode(qrCodeLink, qrCodeSize);
|
|
|
|
+
|
|
|
|
+ // 1、得到画笔对象
|
|
|
|
+ Graphics2D g = buffImg.createGraphics();
|
|
|
|
+
|
|
|
|
+ // 2、设置对线段的锯齿状边缘处理
|
|
|
|
+ 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);
|
|
|
|
+ g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
|
|
|
|
+ 1f));
|
|
|
|
+
|
|
|
|
+ // 6、二维码图片的位置
|
|
|
|
+
|
|
|
|
+ g.drawImage(qrCodeImg, x, y, null);
|
|
|
|
+ g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
|
|
|
|
+ // 7、释放资源
|
|
|
|
+ g.dispose();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ String fileType = srcImgPath.substring(srcImgPath.lastIndexOf(".") + 1);
|
|
|
|
+
|
|
|
|
+ // 输出图片
|
|
|
|
+ UUID uuid = UUID.randomUUID();
|
|
|
|
+ imagePath += uuid + "." + fileType;
|
|
|
|
+ outImgStream = new FileOutputStream(imagePath);
|
|
|
|
+ ImageIO.write(buffImg, fileType, outImgStream);
|
|
|
|
+ outImgStream.flush();
|
|
|
|
+ outImgStream.close();
|
|
|
|
+
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ try {
|
|
|
|
+ if (null != outImgStream) {
|
|
|
|
+ outImgStream.close();
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return imagePath;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static BufferedImage createRealQrCode(String url,int qrCodeSize) {
|
|
|
|
+ int BLACK = 0xFF000000;
|
|
|
|
+ int WHITE = 0xFFFFFFFF;
|
|
|
|
+ try {
|
|
|
|
+ Map<EncodeHintType, String> hints = new HashMap<>();
|
|
|
|
+ hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
|
|
|
|
+ //设置二维码白边的大小
|
|
|
|
+ hints.put(EncodeHintType.MARGIN, "0");
|
|
|
|
+ BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hints);
|
|
|
|
+ //自定义白边边框宽度
|
|
|
|
+ int margin = 1;
|
|
|
|
+ bitMatrix = updateBit(bitMatrix, margin);
|
|
|
|
+ int width = bitMatrix.getWidth();
|
|
|
|
+ int height = bitMatrix.getHeight();
|
|
|
|
+ BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
|
+ for (int x = 0; x < width; x++) {
|
|
|
|
+ for (int y = 0; y < height; y++) {
|
|
|
|
+ image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //根据size放大、缩小生成的二维码
|
|
|
|
+ image = zoomInImage(image,qrCodeSize,qrCodeSize);
|
|
|
|
+ return image;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 图片放大缩小
|
|
|
|
+ */
|
|
|
|
+ public static BufferedImage zoomInImage(BufferedImage originalImage, int width, int height){
|
|
|
|
+ BufferedImage newImage = new BufferedImage(width,height,originalImage.getType());
|
|
|
|
+ Graphics g = newImage.getGraphics();
|
|
|
|
+ g.drawImage(originalImage, 0,0,width,height,null);
|
|
|
|
+ g.dispose();
|
|
|
|
+ return newImage;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 设置白边
|
|
|
|
+ * @param matrix
|
|
|
|
+ * @param margin
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private static BitMatrix updateBit(BitMatrix matrix, int margin){
|
|
|
|
+ int tempM = margin*2;
|
|
|
|
+ //获取二维码图案的属性
|
|
|
|
+ int[] rec = matrix.getEnclosingRectangle();
|
|
|
|
+ int resWidth = rec[2] + tempM;
|
|
|
|
+ int resHeight = rec[3] + tempM;
|
|
|
|
+ // 按照自定义边框生成新的BitMatrix
|
|
|
|
+ BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
|
|
|
|
+ resMatrix.clear();
|
|
|
|
+ //循环,将二维码图案绘制到新的bitMatrix中
|
|
|
|
+ for(int i= margin; i < resWidth- margin; i++){
|
|
|
|
+ for(int j=margin; j < resHeight-margin; j++){
|
|
|
|
+ if(matrix.get(i-margin + rec[0], j-margin + rec[1])){
|
|
|
|
+ resMatrix.set(i,j);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return resMatrix;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String generateAuthImage(Map<String, Object> templateInfo) {
|
|
|
|
+ // 底图
|
|
|
|
+ String templateImage = templateInfo.get("templateImage").toString();
|
|
|
|
+ List<String> tempImageList = new ArrayList<>();
|
|
|
|
+ // 将底图临时存储到本地
|
|
|
|
+ String authImagePath = "";
|
|
|
|
+ String filePath = "/mnt/newdatadrive/data/runtime/jar-instance/zplma/tempImage/";
|
|
|
|
+ if ("dev".equals(active)) {
|
|
|
|
+ filePath = "D:\\WorkSpace\\file\\tempImage\\";
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ URL url = new URL(templateImage);
|
|
|
|
+ InputStream inputStream = url.openStream();
|
|
|
|
+ BufferedImage authImage = ImageIO.read(inputStream);
|
|
|
|
+ String extName = templateImage.substring(templateImage.lastIndexOf(".") + 1);
|
|
|
|
+ UUID uuid = UUID.randomUUID();
|
|
|
|
+ authImagePath = filePath + uuid + "." + extName;
|
|
|
|
+ String tempPath = authImagePath;
|
|
|
|
+ tempImageList.add(tempPath);
|
|
|
|
+ FileOutputStream outImgStream = new FileOutputStream(authImagePath);
|
|
|
|
+ ImageIO.write(authImage, extName, outImgStream);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ // 添加图片
|
|
|
|
+ for (int i = 1; i < 5; i++) {
|
|
|
|
+ Object object = templateInfo.get("addImage" + i + "_image");
|
|
|
|
+ if (null != object) {
|
|
|
|
+ String addImage = object.toString();
|
|
|
|
+ int x = Integer.parseInt(templateInfo.get("addImage" + i + "_x").toString());
|
|
|
|
+ int y = Integer.parseInt(templateInfo.get("addImage" + i + "_y").toString());
|
|
|
|
+ authImagePath = addImage(authImagePath, addImage, x, y);
|
|
|
|
+ tempImageList.add(authImagePath);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 添加文字
|
|
|
|
+ for (int i = 1; i < 5; i++) {
|
|
|
|
+ Object object = templateInfo.get("addWord" + i + "_content");
|
|
|
|
+ if (null != object) {
|
|
|
|
+ String content = object.toString();
|
|
|
|
+ String type = templateInfo.get("addWord" + i + "_type").toString();
|
|
|
|
+ String color = templateInfo.get("addWord" + i + "_color").toString();
|
|
|
|
+ int style = Integer.parseInt(templateInfo.get("addWord" + i + "_style").toString());
|
|
|
|
+ int size = Integer.parseInt(templateInfo.get("addWord" + i + "_size").toString());
|
|
|
|
+ int x = Integer.parseInt(templateInfo.get("addWord" + i + "_x").toString());
|
|
|
|
+ int y = Integer.parseInt(templateInfo.get("addWord" + i + "_y").toString());
|
|
|
|
+ authImagePath = addWord(authImagePath, content, type, color, style, size, x, y);
|
|
|
|
+ tempImageList.add(authImagePath);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 添加二维码
|
|
|
|
+ for (int i = 1; i < 5; i++) {
|
|
|
|
+ Object object = templateInfo.get("addQr" + i + "_link");
|
|
|
|
+ if (null != object) {
|
|
|
|
+ String qrCodeLink = object.toString();
|
|
|
|
+ int qrCodeSize = Integer.parseInt(templateInfo.get("addQr" + i + "_size").toString());
|
|
|
|
+ int x = Integer.parseInt(templateInfo.get("addQr" + i + "_x").toString());
|
|
|
|
+ int y = Integer.parseInt(templateInfo.get("addQr" + i + "_y").toString());
|
|
|
|
+ authImagePath = addQrCode(authImagePath, qrCodeLink, qrCodeSize, x, y);
|
|
|
|
+ tempImageList.add(authImagePath);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ int lastIndex = tempImageList.size() - 1;
|
|
|
|
+ if (lastIndex >= 0) {
|
|
|
|
+ tempImageList.remove(lastIndex);
|
|
|
|
+ }
|
|
|
|
+ tempImageList.forEach(tempImage->{
|
|
|
|
+ // 临时图片
|
|
|
|
+ File tempFile = new File(tempImage);
|
|
|
|
+ // 删除临时图片
|
|
|
|
+ boolean delete = tempFile.delete();
|
|
|
|
+ log.info("【图片上传】>>>>>>>>>>>>>>>>删除临时图片:" + delete);
|
|
|
|
+ });
|
|
|
|
+ return authImagePath;
|
|
|
|
+ }
|
|
}
|
|
}
|