Commit 0864165b by 宋祥

1.生成二维码

parent 2ec071a0
...@@ -167,6 +167,18 @@ ...@@ -167,6 +167,18 @@
<artifactId>oss-plugin</artifactId> <artifactId>oss-plugin</artifactId>
<version>7.0.0</version> <version>7.0.0</version>
</dependency> </dependency>
<!-- 二维码 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.27</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.5.3</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.baosight.hpjx.controller; package com.baosight.hpjx.controller;
import com.baosight.hpjx.hp.mt.untils.Base64ToMultipartFile; import com.baosight.hpjx.core.utils.QrCodeUtils;
import com.baosight.hpjx.hp.mt.untils.UploadUtils2; import com.baosight.hpjx.hp.mt.untils.UploadUtils2;
import com.baosight.hpjx.util.FileUtils;
import com.baosight.hpjx.util.LogUtils; import com.baosight.hpjx.util.LogUtils;
import com.baosight.hpjx.util.MapUtils;
import com.baosight.hpjx.util.StringUtils;
import com.baosight.iplat4j.core.ei.EiConstant; import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo; import org.springframework.data.repository.query.Param;
import com.baosight.iplat4j.core.exception.PlatException; import org.springframework.web.bind.annotation.CrossOrigin;
import com.baosight.iplat4j.core.service.soa.XServiceManager; import org.springframework.web.bind.annotation.RequestMapping;
import com.baosight.iplat4j.eu.dm.document.bos.s3.api.signed.SignedBosHelp; import org.springframework.web.bind.annotation.RequestMethod;
import com.baosight.iplat4j.eu.dm.document.bos.s3.param.BosParamHelp; import org.springframework.web.bind.annotation.RestController;
import com.google.common.base.Joiner;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Set;
import net.sf.json.JSONObject;
/** /**
* @author:songx * @author:songx
...@@ -51,7 +38,7 @@ public class FileUploadController { ...@@ -51,7 +38,7 @@ public class FileUploadController {
Map resultMap = new HashMap(); Map resultMap = new HashMap();
try { try {
JSONObject upload = UploadUtils2.upload((CommonsMultipartFile)file); JSONObject upload = UploadUtils2.upload((CommonsMultipartFile)file);
resultMap.put("id",upload.getString("docId")); resultMap.put("id", upload.getString("docId"));
resultMap.put("status", EiConstant.STATUS_SUCCESS); resultMap.put("status", EiConstant.STATUS_SUCCESS);
resultMap.put("message", "SUCCESS"); resultMap.put("message", "SUCCESS");
return resultMap; return resultMap;
...@@ -61,4 +48,22 @@ public class FileUploadController { ...@@ -61,4 +48,22 @@ public class FileUploadController {
return resultMap; return resultMap;
} }
} }
/**
* @param content
* @return
* @throws IOException
*/
@RequestMapping(value = "/createQrCode", method = RequestMethod.GET)
public Map<String, Object> createQrCode(@Param("content") String content) {
try {
return QrCodeUtils.create(content);
} catch (Exception e) {
Map resultMap = new HashMap();
resultMap.put("status", 500);
resultMap.put("message", LogUtils.getMsg(e));
return resultMap;
}
}
} }
...@@ -36,4 +36,9 @@ public class OSConstant { ...@@ -36,4 +36,9 @@ public class OSConstant {
*/ */
public final static String DOWN_DIR = USER_DIR + SEPARATOR + "download"; public final static String DOWN_DIR = USER_DIR + SEPARATOR + "download";
/**
* 二维码目录
*/
public final static String QRCODE_DIR = USER_DIR + SEPARATOR + "qrcode";
} }
package com.baosight.hpjx.core.tools;
/**
* @author:songx
* @date:2024/4/16,16:29
*/
public class EuTools {
/**
* @author:songx
* @date:2024/4/16,16:29
*/
public static class EuDm01 {
/**
* @param realPath
* @param docId
* @return
*/
public static void intoDb(String realPath, String docId) {
}
}
}
package com.baosight.hpjx.core.utils;
import com.baosight.hpjx.core.constant.OSConstant;
import com.baosight.hpjx.util.FileUtils;
import com.baosight.hpjx.util.MapUtils;
import com.baosight.hpjx.util.StringUtils;
import com.baosight.iplat4j.core.ioc.spring.PlatApplicationContext;
import com.baosight.iplat4j.core.util.DateUtils;
import com.baosight.iplat4j.core.web.threadlocal.UserSession;
import com.baosight.iplat4j.eu.dm.PlatFileUploadUtils;
import com.baosight.iplat4j.eu.dm.util.IFileUploader;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
/**
* 二维码工具类
*
* @author:songx
* @date:2024/4/16,14:47
*/
@Slf4j
public class QrCodeUtils {
// CODE_WIDTH:二维码宽度,单位像素
private static final int CODE_WIDTH = 400;
// CODE_HEIGHT:二维码高度,单位像素
private static final int CODE_HEIGHT = 400;
// FRONT_COLOR:二维码前景色,0x000000 表示黑色
private static final int FRONT_COLOR = 0x000000;
// BACKGROUND_COLOR:二维码背景色,0xFFFFFF 表示白色
// 演示用 16 进制表示,和前端页面 CSS 的取色是一样的,注意前后景颜色应该对比明显,如常见的黑白
private static final int BACKGROUND_COLOR = 0xFFFFFF;
/**
* 生成本地文件
*
* @param content
* @throws WriterException
* @throws IOException
*/
public static Map<String, Object> create(String content) throws Exception {
String fileLocation = PlatApplicationContext.getProperty("iplat4j.admin.upload.fileLocation");
if ("S3".equals(fileLocation)) {
return createByS3(content);
} else {
return createByLocal(content);
}
}
/**
* 生成二维码到S3
*
* @param content
* @return
*/
private static Map<String, Object> createByS3(String content) throws Exception {
// 生成二维码
File file = createFile(null, content);
Map inMap = new HashMap();
inMap.put("groupId", "hk_file");
inMap.put("configPathDefine", "qrCode");
Map<String, String> uploadMap = PlatFileUploadUtils.uploadFile(file, inMap);
log.warn("uploadMap", uploadMap);
String docId = MapUtils.getString(uploadMap, "docId");
// 上传成功以后需要删除本地文件
file.delete();
Map resultMap = new HashMap();
resultMap.put("fileLocation", "S3");
resultMap.put("docId", docId);
resultMap.put("filePath", MapUtils.getString(uploadMap, "docUrl"));
return resultMap;
}
/**
* 生成本地文件
*
* @param content
* @return
*/
private static Map<String, Object> createByLocal(String content) throws Exception {
IFileUploader fileUploader = PlatApplicationContext.getApplicationContext()
.getBean("platFileUploader", IFileUploader.class);
Map<String, String> pathMap = fileUploader.getRealPath("qrCode");
String category = pathMap.get("path");
File dir = new File(category);
if (!dir.exists()) {
dir.mkdirs();
}
String fileName = StringUtils.uuid();
String fileNameAll = fileName + ".png";
String chgName = fileUploader.transformFileName(fileNameAll);
// 生成本地文件
File file = createFile(category + "/" + chgName, content);
// 写入记录
Map map = new HashMap();
map.put("docId", fileName);
map.put("dirId", pathMap.get("dirId"));
map.put("docTag", "hk_file");
map.put("docName", fileNameAll);
map.put("chgName", chgName);
map.put("docSize", file.length());
map.put("recCreator", UserSession.getLoginName());
map.put("recCreateTime", DateUtils.curDateTimeStr14());
fileUploader.uploadDoc(map);
Map resultMap = new HashMap();
resultMap.put("fileLocation", "local");
resultMap.put("docId", fileName);
resultMap.put("filePath", file.getAbsolutePath());
resultMap.put("file", file);
return resultMap;
}
/**
* 生成二维码文件
*
* @param filePath
* @param content
* @return
* @throws Exception
*/
private static File createFile(String filePath, String content) throws IOException, WriterException {
// 文件如果不存在就创建
if (StringUtils.isBlank(filePath)) {
filePath = OSConstant.QRCODE_DIR + OSConstant.SEPARATOR + StringUtils.uuid() + ".png";
}
FileUtils.creatFiles(filePath);
File file = new File(filePath);
//核心代码-生成二维码
BufferedImage bufferedImage = getBufferedImage(content.trim());
ImageIO.write(bufferedImage, "png", file);
return file;
}
/**
* 生成二维码并输出到输出流, 通常用于输出到网页上进行显示,输出到网页与输出到磁盘上的文件中,区别在于最后一句 ImageIO.write
* write(RenderedImage im,String formatName,File output):写到文件中
* write(RenderedImage im,String formatName,OutputStream output):输出到输出流中
*
* @param content :二维码内容
* @param outputStream :输出流,比如 HttpServletResponse 的 getOutputStream
*/
public static void create(String content, OutputStream outputStream) throws WriterException, IOException {
if (StringUtils.isBlank(content)) {
return;
}
content = content.trim();
//核心代码-生成二维码
BufferedImage bufferedImage = getBufferedImage(content);
//区别就是这一句,输出到输出流中,如果第三个参数是 File,则输出到文件中
ImageIO.write(bufferedImage, "png", outputStream);
}
/**
* 核心代码-生成二维码
*
* @param content
* @return
* @throws WriterException
*/
private static BufferedImage getBufferedImage(String content) throws WriterException {
//com.google.zxing.EncodeHintType:编码提示类型,枚举类型
Map<EncodeHintType, Object> hints = new HashMap();
//EncodeHintType.CHARACTER_SET:设置字符编码类型
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//EncodeHintType.ERROR_CORRECTION:设置误差校正
//ErrorCorrectionLevel:误差校正等级,L = ~7% correction、M = ~15% correction、Q = ~25% correction、H = ~30% correction
//不设置时,默认为 L 等级,等级不一样,生成的图案不同,但扫描的结果是一样的
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
//EncodeHintType.MARGIN:设置二维码边距,单位像素,值越小,二维码距离四周越近
hints.put(EncodeHintType.MARGIN, 1);
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, CODE_WIDTH, CODE_HEIGHT, hints);
BufferedImage bufferedImage = new BufferedImage(CODE_WIDTH, CODE_HEIGHT, BufferedImage.TYPE_INT_BGR);
for (int x = 0; x < CODE_WIDTH; x++) {
for (int y = 0; y < CODE_HEIGHT; y++) {
bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? FRONT_COLOR : BACKGROUND_COLOR);
}
}
return bufferedImage;
}
}
...@@ -65,6 +65,7 @@ public class LogUtils { ...@@ -65,6 +65,7 @@ public class LogUtils {
* @return * @return
*/ */
public static String getMsg(Throwable e) { public static String getMsg(Throwable e) {
logger.error("转换系统异常:{}", e.getMessage(), e);
if (e instanceof PlatException) { if (e instanceof PlatException) {
return e.getMessage(); return e.getMessage();
} else { } else {
......
...@@ -35,6 +35,7 @@ datasource.type=dbcp ...@@ -35,6 +35,7 @@ datasource.type=dbcp
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/iplat?useSSL=false&useUnicode=true&characterEncoding=utf-8 jdbc.url=jdbc:mysql://127.0.0.1:3306/iplat?useSSL=false&useUnicode=true&characterEncoding=utf-8
#jdbc.url=jdbc:mysql://10.135.13.5:30005/iplat?useSSL=false&useUnicode=true&characterEncoding=utf-8 #jdbc.url=jdbc:mysql://10.135.13.5:30005/iplat?useSSL=false&useUnicode=true&characterEncoding=utf-8
#jdbc.username=dbprod32
jdbc.username=root jdbc.username=root
#jdbc.password=t9UrgcELp= #jdbc.password=t9UrgcELp=
jdbc.password=123456 jdbc.password=123456
......
...@@ -28,9 +28,9 @@ org.quartz.jobStore.clusterCheckinInterval = 20000 ...@@ -28,9 +28,9 @@ org.quartz.jobStore.clusterCheckinInterval = 20000
org.quartz.dataSource.appDS.driver = com.mysql.jdbc.Driver org.quartz.dataSource.appDS.driver = com.mysql.jdbc.Driver
#org.quartz.dataSource.appDS.URL = jdbc:mysql://10.135.13.5:30005/iplat?useSSL=false&useUnicode=true&characterEncoding=utf-8 #org.quartz.dataSource.appDS.URL = jdbc:mysql://10.135.13.5:30005/iplat?useSSL=false&useUnicode=true&characterEncoding=utf-8
org.quartz.dataSource.appDS.URL = jdbc:mysql://127.0.0.1:3306/iplat?useSSL=false&useUnicode=true&characterEncoding=utf-8 org.quartz.dataSource.appDS.URL = jdbc:mysql://127.0.0.1:3306/iplat?useSSL=false&useUnicode=true&characterEncoding=utf-8
#org.quartz.dataSource.appDS.user = dbprod32
org.quartz.dataSource.appDS.user = root org.quartz.dataSource.appDS.user = root
#org.quartz.dataSource.appDS.password =root #org.quartz.dataSource.appDS.password =t9UrgcELp=
org.quartz.dataSource.appDS.password =123456 org.quartz.dataSource.appDS.password =123456
org.quartz.dataSource.appDS.maxConnections = 30 org.quartz.dataSource.appDS.maxConnections = 30
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment