浏览代码

批量下载二维码增加边框

zhijiezhao 10 月之前
父节点
当前提交
e5dbd0b3b1

+ 2 - 0
src/main/java/com/caimei/model/en/QrCodeSize.java

@@ -10,6 +10,8 @@ import java.util.Date;
  */
 public class QrCodeSize {
     public static final int XLARGE = 1000;
+
+    public static final int MLARGE = 180;
     public static final int LARGE = 137;
     public static final int MEDIUM = 104;
     public static final int SMALL = 70;

+ 2 - 0
src/main/java/com/caimei/model/po/UploadFilePo.java

@@ -8,6 +8,8 @@ import lombok.Data;
  */
 @Data
 public class UploadFilePo {
+
+    private String authName;
     /**
      * 文件名称
      */

+ 1 - 0
src/main/java/com/caimei/service/auth/impl/DownloadServiceImpl.java

@@ -135,6 +135,7 @@ public class DownloadServiceImpl implements DownloadService {
                 UploadFilePo file = new UploadFilePo();
                 file.setFileUrl(qrCodeLink);
                 file.setFileName(authImage.getAuthParty());
+                file.setAuthName(authImage.getName());
                 fileList.add(file);
 //                String imagePath = createBufferedImageFile(qrCodeImage, fileName, "png");
             });

+ 103 - 11
src/main/java/com/caimei/service/auth/impl/UploadServiceImpl.java

@@ -14,10 +14,12 @@ import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.imageio.ImageIO;
+import java.awt.*;
 import java.awt.image.BufferedImage;
 import java.io.*;
 import java.net.URL;
 import java.util.*;
+import java.util.List;
 import java.util.concurrent.atomic.AtomicReference;
 
 /**
@@ -47,7 +49,7 @@ public class UploadServiceImpl implements UploadService {
         int index = originalFilename.lastIndexOf(".");
         String extName = originalFilename.substring(index);
         String filePath = "/mnt/newdatadrive/data/runtime/jar-instance/zplma/tempFile/";
-        if ("dev".equals(active)){
+        if ("dev".equals(active)) {
             filePath = "D:\\WorkSpace\\file\\tempImport\\";
         }
         filePath += randomStr + extName;
@@ -87,16 +89,17 @@ public class UploadServiceImpl implements UploadService {
 
     /**
      * 创建图片压缩包
+     *
      * @param fileMap
-     * @param type      1机构授权牌,2设备授权牌,3机构二维码,4设备二维码,5资料库图片压缩包
+     * @param type    1机构授权牌,2设备授权牌,3机构二维码,4设备二维码,5资料库图片压缩包
      * @return
      * @throws Exception
      */
     @Override
-    public String createImageZip(Map<String,List<UploadFilePo>> fileMap, Integer type) throws Exception {
+    public String createImageZip(Map<String, List<UploadFilePo>> fileMap, Integer type) throws Exception {
         String randomStr = UUID.randomUUID().toString();
         String filePath = "/mnt/newdatadrive/data/runtime/jar-instance/zplma/tempImage/";
-        if ("dev".equals(active)){
+        if ("dev".equals(active)) {
             filePath = "D:\\uploadZip\\";
         }
         // 压缩文件夹上层临时文件夹路径
@@ -187,9 +190,55 @@ public class UploadServiceImpl implements UploadService {
                 UploadFilePo file = fileList.get(i);
                 String fileUrl = file.getFileUrl();
                 String fileName = file.getFileName();
-                BufferedImage qrCodeImage = ImageUtils.createQrCode(fileUrl, QrCodeSize.LARGE);
+                BufferedImage qrCodeImage = ImageUtils.createQrCode(fileUrl, QrCodeSize.MLARGE);
                 String tempFilePath = filePath + "/" + fileName;
-                ImageUtils.transferBufferdImage(qrCodeImage, tempFilePath, "png");
+                // 加载背景图片
+                BufferedImage backgroundImage = ImageIO.read(new File("/mnt/newdatadrive/data/runtime/jar-instance/zplma/background.png"));
+                // 设置文字
+                String title = file.getAuthName() + "正品授权";
+                String bottom = file.getFileName();
+                // 计算二维码和文字的位置
+                int qrCodeX = (backgroundImage.getWidth() - qrCodeImage.getWidth()) / 2;
+                int qrCodeY = (backgroundImage.getHeight() - qrCodeImage.getHeight()) / 2 + 25;
+                // 文字距离图片顶部的距离,可以根据需要调整
+                int textY = 30;
+                // 创建新图片,使用背景图片的尺寸
+                BufferedImage finalImage = new BufferedImage(backgroundImage.getWidth(), backgroundImage.getHeight(), BufferedImage.TYPE_INT_RGB);
+                // 获取Graphics2D对象进行绘制
+                Graphics2D g2d = finalImage.createGraphics();
+                // 抗锯齿
+                g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+                g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
+                g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
+                // 绘制背景图片
+                g2d.drawImage(backgroundImage, 0, 0, null);
+                // 绘制二维码
+                g2d.drawImage(qrCodeImage, qrCodeX, qrCodeY, null);
+                // 设置字体和颜色 字体大小可根据需要调整
+                g2d.setFont(new Font("微软雅黑", Font.BOLD, 20));
+                g2d.setColor(Color.WHITE);
+                // 绘制文字 如果抬头或者底部字数大于14,需要手动计算换行
+                if (title.length() > 14) {
+                    // 计算文字的X坐标,使其居中显示
+                    FontMetrics metrics = g2d.getFontMetrics();
+                    String first = title.substring(0, 14);
+                    String second = title.substring(14);
+                    int textX = (finalImage.getWidth() - metrics.stringWidth(first)) / 2;
+                    int textS = (finalImage.getWidth() - metrics.stringWidth(second)) / 2;
+                    g2d.drawString(first, textX, textY);
+                    g2d.drawString(second, textS, textY + 20);
+                    int textB = (finalImage.getWidth() - metrics.stringWidth(bottom)) / 2;
+                    g2d.drawString(bottom, textB, 370);
+                } else {
+                    // 计算文字的X坐标,使其居中显示
+                    FontMetrics metrics = g2d.getFontMetrics();
+                    int textX = (finalImage.getWidth() - metrics.stringWidth(title)) / 2;
+                    g2d.drawString(title, textX, textY);
+                    g2d.drawString(bottom, textX, 370);
+                }
+                ImageUtils.transferBufferdImage(finalImage, tempFilePath, "png");
+                // 清理资源
+                g2d.dispose();
             }
         } else if (4 == type) {
             // 设备二维码
@@ -206,9 +255,52 @@ public class UploadServiceImpl implements UploadService {
                     UploadFilePo file = fileList.get(i);
                     String fileUrl = file.getFileUrl();
                     String fileName = file.getFileName();
-                    BufferedImage qrCodeImage = ImageUtils.createQrCode(fileUrl, QrCodeSize.LARGE);
+                    BufferedImage qrCodeImage = ImageUtils.createQrCode(fileUrl, QrCodeSize.MLARGE);
+                    // 0位置仪器名称 1位置sn码
+                    String[] split = fileName.split("-");
+                    // 加载背景图片
+                    BufferedImage backgroundImage = ImageIO.read(new File("/mnt/newdatadrive/data/runtime/jar-instance/zplma/snback.png"));
+                    // 设置文字
+                    String text = split[0];
+                    String bot = "";
+                    if (split[1].length() < 3) {
+                        bot = "仪器SN码:" + split[1] + "******" + split[1];
+                    } else {
+                        bot = "仪器SN码:" + split[1].substring(0, 2) + "******" + split[1].substring(0, 3);
+                    }
+                    // 计算二维码和文字的位置
+                    int qrCodeX = (backgroundImage.getWidth() - qrCodeImage.getWidth()) / 2;
+                    int qrCodeY = (backgroundImage.getHeight() - qrCodeImage.getHeight()) / 2 + 58;
+                    // 文字距离图片顶部的距离,可以根据需要调整
+                    int textY = 30;
+                    // 创建新图片,使用背景图片的尺寸
+                    BufferedImage finalImage = new BufferedImage(backgroundImage.getWidth(), backgroundImage.getHeight(), BufferedImage.TYPE_INT_RGB);
+                    // 获取Graphics2D对象进行绘制
+                    Graphics2D g2d = finalImage.createGraphics();
+                    // 抗锯齿
+                    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+                    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
+                    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
+                    // 绘制背景图片
+                    g2d.drawImage(backgroundImage, 0, 0, null);
+                    // 绘制二维码
+                    g2d.drawImage(qrCodeImage, qrCodeX, qrCodeY, null);
+                    // 设置字体和颜色 字体大小可根据需要调整
+                    g2d.setFont(new Font("微软雅黑", Font.BOLD, 20));
+                    g2d.setColor(Color.WHITE);
+                    // 计算文字的X坐标,使其居中显示
+                    FontMetrics metrics = g2d.getFontMetrics();
+                    int textX = (finalImage.getWidth() - metrics.stringWidth(text)) / 2;
+                    g2d.drawString(text, textX, textY);
+                    // 设置字体和颜色 字体大小可根据需要调整
+                    g2d.setFont(new Font("微软雅黑", Font.BOLD, 14));
+                    Color customColor = new Color(27, 163, 240);
+                    g2d.setColor(customColor);
+                    int textB = (finalImage.getWidth() - metrics.stringWidth(bot)) / 2 + 33;
+                    g2d.drawString(bot, textB, 90);
                     String tempFilePath = filePath + "/" + fileName;
-                    ImageUtils.transferBufferdImage(qrCodeImage, tempFilePath, "png");
+                    ImageUtils.transferBufferdImage(finalImage, tempFilePath, "png");
+                    g2d.dispose();
                 }
             }
         }
@@ -220,7 +312,7 @@ public class UploadServiceImpl implements UploadService {
     }
 
     @Override
-    public String saveFileByUrl(String url,String fileName) throws Exception {
+    public String saveFileByUrl(String url, String fileName) throws Exception {
         InputStream input = new URL(url).openStream();
         File file = inputStreamToFile(input, fileName);
         String filePath = System.getProperty("java.io.tmpdir") + File.separator + fileName;
@@ -234,10 +326,10 @@ public class UploadServiceImpl implements UploadService {
 
 
     /**
-     *   工具类
+     * 工具类
      * inputStream 转 File
      */
-    public static File inputStreamToFile(InputStream ins, String name) throws Exception{
+    public static File inputStreamToFile(InputStream ins, String name) throws Exception {
 //        File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
         File file = new File("D:\\uploadZip\\机构二维码\\" + File.separator + name);
         if (file.exists()) {

+ 1 - 1
src/main/resources/mapper/FileMapper.xml

@@ -221,7 +221,7 @@
         </if>
     </select>
     <select id="getAuthImageList" resultType="com.caimei.model.vo.AuthVo">
-        select a.id as authId, a.authUserId, a.authImage, a.authParty
+        select a.id as authId, a.authUserId, a.authImage, a.authParty,u.name
         from cm_brand_auth a
                  left join cm_brand_auth_user u on a.authUserId = u.authUserId
         where a.delFlag = 0