Browse Source

Merge branch 'test-1.4.0-xwj-20230601'

xiewj 1 year ago
parent
commit
c6a47c9fde

+ 59 - 19
720yun_fd_manage/gis_oss/src/main/java/com/gis/oss/util/QrCodeUtils.java

@@ -1,14 +1,22 @@
 package com.gis.oss.util;
 
 import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.img.ImgUtil;
 import cn.hutool.core.io.FileUtil;
 import cn.hutool.extra.qrcode.QrCodeUtil;
 import cn.hutool.extra.qrcode.QrConfig;
+import com.amazonaws.services.dynamodbv2.xspec.S;
+import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
 import lombok.extern.slf4j.Slf4j;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.Date;
 
@@ -111,7 +119,8 @@ public class QrCodeUtils {
         savePath = savePath + "/qrCode.jpg";
 //        log.info("savePath: {}", savePath);
         // 创建二维码tpt
-        QrCodeUtil.generate(url, QrConfig.create().setImg(logoPath).setWidth(300), FileUtil.file(savePath));
+        File file = scaleImage(logoPath);
+        QrCodeUtil.generate(url, QrConfig.create().setImg(file.getAbsoluteFile()).setErrorCorrection(ErrorCorrectionLevel.H).setRatio(12), FileUtil.file(savePath));
 
         // 二维码上传oss
         String ossPath = ossBasePath + id + "/qrCode.jpg";
@@ -126,27 +135,58 @@ public class QrCodeUtils {
     /**
      * 附带logo小图标二维码
      */
-    @Test
-    public  void testGenerateLogoQrCode(){
-        String url = "https://www.baidu.com";
-        String savePath = "E:\\cache\\";
-        // 保存地址
-        String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS") + ".jpg";
-        String dirType = "qrCode/";
-        savePath = savePath + dirType;
+    public static void main(String[] args) {
+        String logoPath="C:\\Users\\4DAGE\\Downloads\\20230728_100814888.jpg";
+        String savePath="C:\\Users\\4DAGE\\Downloads\\123123.jpg";
+        File file = scaleImage(logoPath);
+        QrCodeUtil.generate("asdasdasdasd", QrConfig.create().setImg(file.getAbsoluteFile()).setErrorCorrection(ErrorCorrectionLevel.H).setRatio(12), FileUtil.file(savePath));
 
-        String logoPath = FileUtils.getResource() + "img/kankan_icon.png";
-        log.info("logoPath: {}", logoPath);
+    }
 
-        // 创建目录
-        if (!FileUtil.isDirectory(savePath)) {
-            FileUtil.mkdir(savePath);
+    public static File scaleImage(String imagePath) {
+        try {
+            File imageFile =  new File(imagePath);
+            BufferedImage inputImage = ImageIO.read(imageFile);
+            BufferedImage outputImage = resizeImage(inputImage, 80, 80);
+            fillTransparent(outputImage, 80, 80);
+            String output=imageFile.getParent()+File.separator+imageFile.getName().split("\\.")[0]+".png";
+            File outputFile = new File(output);
+            ImageIO.write(outputImage, "png", outputFile);
+            System.out.println("Image resized and saved successfully.");
+            return outputFile;
+        } catch (IOException e) {
+            e.printStackTrace();
         }
-        savePath = savePath + time;
-        log.info("savePath: {}", savePath);
-        // 创建二维码tpt
-        QrCodeUtil.generate(url, QrConfig.create().setImg(logoPath).setWidth(300), FileUtil.file(savePath));
+        return null;
+
+    }
+
+    public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
+        double aspectRatio = (double) originalImage.getWidth() / (double) originalImage.getHeight();
+        int newWidth = targetWidth;
+        int newHeight = (int) (newWidth / aspectRatio);
+
+        if (newHeight > targetHeight) {
+            newHeight = targetHeight;
+            newWidth = (int) (newHeight * aspectRatio);
+        }
+
+        BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
+        Graphics2D g2d = resizedImage.createGraphics();
+        g2d.drawImage(originalImage, 0, 0, newWidth, newHeight, null);
+        g2d.dispose();
+
+        return resizedImage;
     }
 
+    public static void fillTransparent(BufferedImage image, int targetWidth, int targetHeight) {
+        int offsetX = (targetWidth - image.getWidth()) / 2;
+        int offsetY = (targetHeight - image.getHeight()) / 2;
 
-}
+        Graphics2D g2d = image.createGraphics();
+        g2d.setColor(new Color(0, 0, 0, 0)); // Transparent color
+        g2d.fillRect(0, 0, targetWidth, targetHeight);
+        g2d.drawImage(image, offsetX, offsetY, null);
+        g2d.dispose();
+    }
+ }

+ 1 - 1
720yun_fd_manage/pom.xml

@@ -36,7 +36,7 @@
         <gis.version>1.0.0</gis.version>
         <fastjson.version>1.2.83</fastjson.version>
         <druid.version>1.1.14</druid.version>
-        <hutool.version>5.3.3</hutool.version>
+        <hutool.version>5.8.22</hutool.version>
         <lombok.version>1.18.2</lombok.version>
         <lang3.version>3.7</lang3.version>
         <mysql.version>8.0.15</mysql.version>