From d4cde237c5bb9681260a2ebf9fc894da7c671f2d Mon Sep 17 00:00:00 2001 From: hu177768073 <177768073@qq.com> Date: Thu, 19 Sep 2024 11:38:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=94=A8=E6=88=B7=E9=82=80?= =?UTF-8?q?=E8=AF=B7=E7=A0=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/hfkj/config/ConfigListener.java | 3 ++ .../java/com/hfkj/common/QRCodeGenerator.java | 50 ++++++++++++++++++- .../impl/BsUserInviteCodeServiceImpl.java | 49 +++++++++++++++--- .../resources/dev/commonConfig.properties | 4 +- 4 files changed, 96 insertions(+), 10 deletions(-) diff --git a/cweb/src/main/java/com/hfkj/config/ConfigListener.java b/cweb/src/main/java/com/hfkj/config/ConfigListener.java index 35eaeda..bfe5155 100644 --- a/cweb/src/main/java/com/hfkj/config/ConfigListener.java +++ b/cweb/src/main/java/com/hfkj/config/ConfigListener.java @@ -10,10 +10,13 @@ public class ConfigListener implements ServletContextListener { @Resource private SysConfig sysConfig; + @Resource + private CommonSysConfig commonSysConfig; @Override public void contextInitialized(ServletContextEvent sce) { SysConst.setSysConfig(sysConfig); + CommonSysConst.setSysConfig(commonSysConfig); } @Override diff --git a/service/src/main/java/com/hfkj/common/QRCodeGenerator.java b/service/src/main/java/com/hfkj/common/QRCodeGenerator.java index 1b0acef..59a55b8 100644 --- a/service/src/main/java/com/hfkj/common/QRCodeGenerator.java +++ b/service/src/main/java/com/hfkj/common/QRCodeGenerator.java @@ -1,6 +1,7 @@ package com.hfkj.common; import com.google.zxing.BarcodeFormat; +import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; @@ -12,6 +13,8 @@ import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; /** * @Auther: 胡锐 @@ -29,8 +32,9 @@ public class QRCodeGenerator { */ public static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException { QRCodeWriter qrCodeWriter = new QRCodeWriter(); - - BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); + Map hints = new HashMap<>(); + hints.put(EncodeHintType.MARGIN, 0); + BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,hints); File file = new File(filePath); if(!file.exists()){ @@ -56,6 +60,48 @@ public class QRCodeGenerator { ImageIO.write(read , "png" , new File(CommonSysConst.getSysConfig().getFilesystem() + "/" + path)); } + public static String overlapImage(String path ,String number) { + try { + // 创建BufferedImage对象 + int width = 100; + int height = 45; + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + + // 获取Graphics2D对象 + Graphics2D g2d = image.createGraphics(); + + // 设置背景颜色并填充 + g2d.setComposite(AlphaComposite.Clear); + g2d.fillRect(0, 0, width, height); + g2d.setComposite(AlphaComposite.SrcOver); + // 设置背景颜色并填充 + //g2d.setColor(Color.BLACK); + //g2d.fillRect(0, 0, width, height); + + // 设置字体和颜色 + Font font = new Font("Arial", Font.BOLD, 30); + g2d.setFont(font); + g2d.setColor(Color.BLACK); + + // 绘制数字到图片上 + int x = (width - g2d.getFontMetrics().stringWidth(number)) / 2; + int y = (height + g2d.getFontMetrics().getHeight()) / 2; + g2d.drawString(number, x, y); + + // 释放Graphics2D对象的资源 + g2d.dispose(); + + // 将BufferedImage保存为图片文件 + File outputFile = new File(path + number + ".png"); + ImageIO.write(image, "png", outputFile); + return outputFile.getName(); + + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + public static void main(String[] args) { try { generateQRCodeImage("This is my first QR Code", 350, 350, "D:\\/ss/qr1.png"); diff --git a/service/src/main/java/com/hfkj/service/user/impl/BsUserInviteCodeServiceImpl.java b/service/src/main/java/com/hfkj/service/user/impl/BsUserInviteCodeServiceImpl.java index 9c15c0e..2eb08a6 100644 --- a/service/src/main/java/com/hfkj/service/user/impl/BsUserInviteCodeServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/user/impl/BsUserInviteCodeServiceImpl.java @@ -13,6 +13,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; import java.util.Date; import java.util.List; @@ -37,24 +42,56 @@ public class BsUserInviteCodeServiceImpl implements BsUserInviteCodeService { data.setUserId(userId); data.setCreateTime(new Date()); data.setStatus(1); - System.out.println(commonSysConfig.getDomain()); // 生成二维码 - String fileUrl = "/userInviteCode/"+data.getUserId()+".png"; + String fileUrl = "/userInviteCode/"+data.getUserId()+"_"+System.currentTimeMillis()+".png"; QRCodeGenerator.generateQRCodeImage( ""+data.getUserId(), - 260, - 260, + 180, + 180, commonSysConfig.getFilesystem() + fileUrl ); // 背景图 - // QRCodeGenerator.overlapImage(fileUrl, "/template/qrCodeBackdrop1_1.png",740,110); - data.setQrCodeImg(fileUrl); + QRCodeGenerator.overlapImage(fileUrl, "/template/invite_background.png",660,1470); + + // 邀请码存放目录 + String invitationCodeUrl = "userInviteCode/"; + // 生成邀请码图片 + String overlapImage = QRCodeGenerator.overlapImage(commonSysConfig.getFilesystem() + "/" + invitationCodeUrl, "" + userId); + + QRCodeGenerator.overlapImage( + "/"+invitationCodeUrl+overlapImage, + fileUrl,285,1566); + + data.setQrCodeImg(CommonSysConst.getSysConfig().getDomain() + "/filesystem/" + invitationCodeUrl+overlapImage); userInviteCodeMapper.insert(data); // 缓存 redisUtil.hset(CACHE_KEY, data.getUserId()+"", data); + + // 删除临时文件 + new File(CommonSysConst.getSysConfig().getFilesystem() + fileUrl).delete(); + return data; } +/* public static void main(String[] args) throws Exception { + String userId = "123456"; + // 生成二维码 + String fileUrl = "/userInviteCode/"+userId+"_"+System.currentTimeMillis()+".png"; + QRCodeGenerator.generateQRCodeImage( + userId, + 155, + 155, + "D:/home/project/youtao/filesystem/" + fileUrl + ); + // 背景图 + QRCodeGenerator.overlapImage2(fileUrl, "D:/home/project/youtao/filesystem/template/invite_background.png",660,1470); + + // 邀请码 + String invitationCodeUrl = "D:/home/project/youtao/filesystem/userInviteCode/"; + QRCodeGenerator.overlapImage2("userInviteCode/"+QRCodeGenerator.overlapImage(invitationCodeUrl, "100001"), + "D:/home/project/youtao/filesystem/"+fileUrl,285,1566); + }*/ + @Override public BsUserInviteCode getDetail(Long userId) { diff --git a/service/src/main/resources/dev/commonConfig.properties b/service/src/main/resources/dev/commonConfig.properties index 4aae95d..0fef8ca 100644 --- a/service/src/main/resources/dev/commonConfig.properties +++ b/service/src/main/resources/dev/commonConfig.properties @@ -1,6 +1,6 @@ filesystem=/home/project/youtao/filesystem -domain=47.92.163.212 +domain=http://47.92.163.212 taoBaoPostUrl = https://eco.taobao.com/router/rest taoBaoAppKey = 34807616 -taoBaoAppSecret = f04baedca9cd794665dfa04a2fcbfd86 \ No newline at end of file +taoBaoAppSecret = f04baedca9cd794665dfa04a2fcbfd86