Browse Source

二维码生成
域名在数据库字典中进行配置
config配置文件接口已放行

lidongxue 3 weeks ago
parent
commit
9ad6887b82

+ 20 - 0
pom.xml

@@ -51,6 +51,7 @@
     </dependencyManagement>
 
     <dependencies>
+
         <!-- ↓ BladeX依赖库 ↓ -->
         <dependency>
             <groupId>org.springblade</groupId>
@@ -75,6 +76,20 @@
             <groupId>org.springblade</groupId>
             <artifactId>blade-starter-sharding</artifactId>
         </dependency>-->
+        <!-- zxing生成二维码 -->
+        <dependency>
+            <groupId>com.google.zxing</groupId>
+            <artifactId>core</artifactId>
+            <version>3.3.3</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.google.zxing</groupId>
+            <artifactId>javase</artifactId>
+            <version>3.3.3</version>
+        </dependency>
+
+
         <dependency>
             <groupId>org.springblade</groupId>
             <artifactId>blade-starter-http</artifactId>
@@ -241,6 +256,11 @@
             <artifactId>alipay-sdk-java</artifactId>
             <version>4.39.282.ALL</version>
         </dependency>
+        <dependency>
+            <groupId>org.mybatis</groupId>
+            <artifactId>mybatis</artifactId>
+            <version>3.5.16</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 1 - 0
src/main/java/org/springblade/common/config/BladeConfiguration.java

@@ -67,6 +67,7 @@ public class BladeConfiguration implements WebMvcConfigurer {
 		secureRegistry.excludePathPatterns("/druid/**");
 		secureRegistry.excludePathPatterns("/payInfo/wxLogin");
 		secureRegistry.excludePathPatterns("/payInfo/wxGetPhoneNumber");
+		secureRegistry.excludePathPatterns("/qr/code/qrCodeUtil");
 		return secureRegistry;
 	}
 

+ 123 - 0
src/main/java/org/springblade/modules/spzf/ewm/controller/QrCodeController.java

@@ -0,0 +1,123 @@
+package org.springblade.modules.spzf.ewm.controller;
+
+
+
+import com.google.zxing.BarcodeFormat;
+import com.google.zxing.EncodeHintType;
+import com.google.zxing.MultiFormatWriter;
+import com.google.zxing.common.BitMatrix;
+import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
+import lombok.AllArgsConstructor;
+import org.apache.commons.lang3.StringUtils;
+import org.springblade.common.cache.DictBizCache;
+import org.springblade.core.tool.api.R;
+import org.springblade.modules.spzf.ewm.pojo.entity.EwmShEntity;
+import org.springblade.modules.spzf.ewm.service.IEwmShService;
+import org.springframework.web.bind.annotation.*;
+
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.net.URL;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+@RestController
+@AllArgsConstructor
+@RequestMapping("qr/code")
+public class QrCodeController{
+
+    private final IEwmShService ewmShService;
+
+    @PostMapping("/qrCodeUtil")
+    public R qrCodeUtil(@RequestParam Integer num,@RequestParam String qhbm){
+        if(num>0 && StringUtils.isNoneBlank(qhbm)){
+            try {
+                String yuming = DictBizCache.getValue("yuming", 2);
+                for (int i = 0; i < num; i++) {
+                    // 准备一个Map集合,用来存放二维码的属性
+                    Map map = new HashMap();
+                    map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
+                    map.put(EncodeHintType.CHARACTER_SET, "UTF-8");
+                    map.put(EncodeHintType.MARGIN, 1);
+                    // 获取文本内容
+                    String url = yuming+"?id=XWKJ-SPARG"+qhbm+"-"+UUID.randomUUID().toString().replace("-","");
+//				String url = "https://www.jlsxwkj.cn/?id=888888";
+                    // 创建zxing核心对象
+                    MultiFormatWriter writer = new MultiFormatWriter();
+                    BitMatrix bitMatrix = writer.encode(url, BarcodeFormat.QR_CODE, 300, 300, map);
+                    int width = bitMatrix.getWidth();
+                    int height = bitMatrix.getHeight();
+
+                    // 生成二维码
+                    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+                    for (int x = 0; x < width; x++) {
+                        for (int y = 0; y < height; y++) {
+                            bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
+                        }
+                    }
+
+//				// 给二维码添加logo
+//				// 第一部分:将logo缩放。
+//				// 通过Part对象获取输入流
+//				FileInputStream inputStream = new FileInputStream(new File("D:\\desktop\\测试用例\\szx.png"));
+//				// 通过ImageIO的read方法,从输入流中读取,从而获得logo图片
+//				Image logoImage = ImageIO.read(inputStream);
+//				// 获取logo图片的宽度
+//				int logoWidth = logoImage.getWidth(null);
+//				// 获取logo图片的高度
+//				int logoHeight = logoImage.getHeight(null);
+//				// 如果logo的宽度或者高度大于100,则重新赋值100
+//				if (logoWidth > 60) {
+//					logoWidth = 60;
+//				}
+//				if (logoHeight > 60) {
+//					logoHeight = 60;
+//				}
+//				// 使用平滑缩放算法对原logo图像进行缩放得到一个全新的图像。
+//				Image scaledLogo = logoImage.getScaledInstance(logoWidth, logoHeight, Image.SCALE_SMOOTH);
+//
+//				// 第二部分:将缩放后的logo画到黑白二维码上
+//				// 获取2D画笔
+//				Graphics2D graphics2D = bufferedImage.createGraphics();
+//				// 开始画的x和y坐标
+//				int x = (300 - logoWidth) / 2;
+//				int y = (300 - logoHeight) / 2;
+//				// 将缩放后的logo画上去
+//				graphics2D.drawImage(scaledLogo, x, y, null);
+//				// 创建一个具有指定位置、宽度、高度和圆角半径的圆角矩形。这个圆角矩形是用来绘制边框的。
+//				Shape shape = new RoundRectangle2D.Float(x, y, logoWidth, logoHeight, 10, 10);
+//				// 使用一个宽度为4像素的基本笔触
+//				graphics2D.setStroke(new BasicStroke(4f));
+//				// 给logo画圆角矩形
+//				graphics2D.draw(shape);
+//				// 释放画笔
+//				graphics2D.dispose();
+
+                    URL qrUrl = new URL(url);
+                    String query = qrUrl.getQuery();
+                    String[] pair = query.split("=");
+                    String qrUrlId = pair[1];
+                    EwmShEntity ewmShEntity = new EwmShEntity();
+                    ewmShEntity.setEwmbs(qrUrlId);
+                    ewmShService.save(ewmShEntity);
+                    // 响应
+                    FileOutputStream out = new FileOutputStream(new File("C:\\shFiles\\"+qrUrlId+".png"));
+                    ImageIO.write(bufferedImage, "png",out);
+                    out.flush();
+                    out.close();
+                }
+                return R.success("生成二维码成功");
+            } catch (Exception e) {
+                e.printStackTrace();
+                return R.fail(e.getMessage());
+            }
+        }
+        return R.fail("请输入正确的参数值");
+    }
+
+
+}