设备二维码生成功能
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package com.njcn.csdevice.pojo.param;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 类的介绍:
|
||||
*
|
||||
* @author xuyang
|
||||
* @version 1.0.0
|
||||
* @createTime 2023/8/21 16:00
|
||||
*/
|
||||
@Data
|
||||
public class QrParam {
|
||||
|
||||
private String type = "NDID";
|
||||
|
||||
private String data;
|
||||
|
||||
}
|
||||
@@ -138,5 +138,10 @@ public class CsEquipmentDeliveryPO extends BaseEntity {
|
||||
@TableField(value = "module_number")
|
||||
private Integer moduleNumber;
|
||||
|
||||
/**
|
||||
* 二维码文件路径
|
||||
*/
|
||||
@TableField(value = "qr_path")
|
||||
private String qrPath;
|
||||
|
||||
}
|
||||
@@ -114,4 +114,7 @@ public class CsEquipmentDeliveryVO extends BaseEntity {
|
||||
|
||||
@ApiModelProperty(value="设备软件信息")
|
||||
private String softinfoId ;
|
||||
|
||||
@ApiModelProperty(value="二维码路径")
|
||||
private String qrPath ;
|
||||
}
|
||||
@@ -95,6 +95,20 @@
|
||||
<version>1.0.0</version>
|
||||
<scope>compile</scope>
|
||||
</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>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.njcn.csdevice.service.impl;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
@@ -13,16 +14,16 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.njcn.common.pojo.exception.BusinessException;
|
||||
import com.njcn.csdevice.enums.AlgorithmResponseEnum;
|
||||
import com.njcn.csdevice.mapper.CsEquipmentDeliveryMapper;
|
||||
import com.njcn.csdevice.pojo.param.CsEquipmentDeliveryAddParm;
|
||||
import com.njcn.csdevice.pojo.param.CsEquipmentDeliveryAuditParm;
|
||||
import com.njcn.csdevice.pojo.param.CsEquipmentDeliveryQueryParm;
|
||||
import com.njcn.csdevice.pojo.param.ProjectEquipmentQueryParm;
|
||||
import com.njcn.csdevice.pojo.param.*;
|
||||
import com.njcn.csdevice.pojo.po.*;
|
||||
import com.njcn.csdevice.pojo.vo.CsEquipmentDeliveryVO;
|
||||
import com.njcn.csdevice.pojo.vo.DeviceManagerVO;
|
||||
import com.njcn.csdevice.pojo.vo.ProjectEquipmentVO;
|
||||
import com.njcn.csdevice.service.*;
|
||||
import com.njcn.csdevice.util.QRCodeUtil;
|
||||
import com.njcn.db.constant.DbConstant;
|
||||
import com.njcn.oss.constant.OssPath;
|
||||
import com.njcn.oss.utils.FileStorageUtil;
|
||||
import com.njcn.system.api.DicDataFeignClient;
|
||||
import com.njcn.system.enums.DicDataEnum;
|
||||
import com.njcn.web.factory.PageFactory;
|
||||
@@ -32,6 +33,12 @@ import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
@@ -61,6 +68,8 @@ public class CsEquipmentDeliveryServiceImpl extends ServiceImpl<CsEquipmentDeliv
|
||||
|
||||
private final DicDataFeignClient dicDataFeignClient;
|
||||
|
||||
private final FileStorageUtil fileStorageUtil;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = {Exception.class})
|
||||
public Boolean save(CsEquipmentDeliveryAddParm csEquipmentDeliveryAddParm) {
|
||||
@@ -72,6 +81,18 @@ public class CsEquipmentDeliveryServiceImpl extends ServiceImpl<CsEquipmentDeliv
|
||||
BeanUtils.copyProperties (csEquipmentDeliveryAddParm,csEquipmentDeliveryPo);
|
||||
csEquipmentDeliveryPo.setStatus ("1");
|
||||
csEquipmentDeliveryPo.setRunStatus(1);
|
||||
//生成二维码文件
|
||||
try {
|
||||
QrParam qrParam = new QrParam();
|
||||
qrParam.setData(csEquipmentDeliveryAddParm.getNdid());
|
||||
BufferedImage bufferedImage = QRCodeUtil.getBufferedImage(JSONObject.toJSONString(qrParam));
|
||||
InputStream inputStream = bufferedImageToInputStream(bufferedImage);
|
||||
String fileName = csEquipmentDeliveryAddParm.getNdid() + ".png";
|
||||
String filePath = fileStorageUtil.uploadStream(inputStream,OssPath.APP_DEVICE_QR,fileName);
|
||||
csEquipmentDeliveryPo.setQrPath(filePath);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return this.save (csEquipmentDeliveryPo);
|
||||
}
|
||||
|
||||
@@ -180,7 +201,13 @@ public class CsEquipmentDeliveryServiceImpl extends ServiceImpl<CsEquipmentDeliv
|
||||
} else {
|
||||
queryWrapper.in("cs_equipment_delivery.run_status", Arrays.asList(1,2));
|
||||
}
|
||||
return this.baseMapper.page(new Page<>(PageFactory.getPageNum(queryParam), PageFactory.getPageSize(queryParam)), queryWrapper);
|
||||
Page<CsEquipmentDeliveryVO> page = this.baseMapper.page(new Page<>(PageFactory.getPageNum(queryParam), PageFactory.getPageSize(queryParam)), queryWrapper);
|
||||
page.getRecords().forEach(item->{
|
||||
if (!Objects.isNull(item.getQrPath())){
|
||||
item.setQrPath(fileStorageUtil.getFileUrl(item.getQrPath()));
|
||||
}
|
||||
});
|
||||
return page;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -261,4 +288,15 @@ public class CsEquipmentDeliveryServiceImpl extends ServiceImpl<CsEquipmentDeliv
|
||||
this.update(lambdaUpdateWrapper);
|
||||
}
|
||||
|
||||
public InputStream bufferedImageToInputStream(BufferedImage image) {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
try {
|
||||
ImageIO.write(image, "png", os);
|
||||
InputStream input = new ByteArrayInputStream(os.toByteArray());
|
||||
return input;
|
||||
} catch (IOException e) {
|
||||
log.error("提示:", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
package com.njcn.csdevice.util;
|
||||
|
||||
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 lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.filechooser.FileSystemView;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.OutputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 类的介绍:
|
||||
* 二维码工具
|
||||
* @author xuyang
|
||||
* @version 1.0.0
|
||||
* @createTime 2023/8/21 13:57
|
||||
*/
|
||||
@Slf4j
|
||||
public class QRCodeUtil {
|
||||
|
||||
/**
|
||||
* 二维码宽度,单位像素
|
||||
*/
|
||||
private static final int CODE_WIDTH = 400;
|
||||
|
||||
/**
|
||||
* 二维码高度,单位像素
|
||||
*/
|
||||
private static final int CODE_HEIGHT = 400;
|
||||
|
||||
/**
|
||||
* 二维码前景色,0x000000 表示黑色
|
||||
*/
|
||||
private static final int FRONT_COLOR = 0x000000;
|
||||
|
||||
/**
|
||||
* 二维码背景色,0xFFFFFF 表示白色
|
||||
*/
|
||||
private static final int BACKGROUND_COLOR = 0xFFFFFF;
|
||||
|
||||
public static void createCodeToFile(String content, File codeImgFileSaveDir, String fileName) {
|
||||
try {
|
||||
if (StringUtils.isBlank(content) || StringUtils.isBlank(fileName)) {
|
||||
return;
|
||||
}
|
||||
content = content.trim();
|
||||
if (codeImgFileSaveDir==null || codeImgFileSaveDir.isFile()) {
|
||||
//二维码图片存在目录为空,默认放在桌面...
|
||||
codeImgFileSaveDir = FileSystemView.getFileSystemView().getHomeDirectory();
|
||||
}
|
||||
if (!codeImgFileSaveDir.exists()) {
|
||||
//二维码图片存在目录不存在,开始创建...
|
||||
codeImgFileSaveDir.mkdirs();
|
||||
}
|
||||
//核心代码-生成二维码
|
||||
BufferedImage bufferedImage = getBufferedImage(content);
|
||||
File codeImgFile = new File(codeImgFileSaveDir, fileName);
|
||||
ImageIO.write(bufferedImage, "png", codeImgFile);
|
||||
log.info("二维码图片生成成功:" + codeImgFile.getPath());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成二维码并输出到输出流, 通常用于输出到网页上进行显示,输出到网页与输出到磁盘上的文件中,区别在于最后一句 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 createCodeToOutputStream(String content, OutputStream outputStream) {
|
||||
try {
|
||||
if (StringUtils.isBlank(content)) {
|
||||
return;
|
||||
}
|
||||
content = content.trim();
|
||||
//核心代码-生成二维码
|
||||
BufferedImage bufferedImage = getBufferedImage(content);
|
||||
//区别就是这一句,输出到输出流中,如果第三个参数是 File,则输出到文件中
|
||||
ImageIO.write(bufferedImage, "png", outputStream);
|
||||
log.info("二维码图片生成到输出流成功...");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//核心代码-生成二维码
|
||||
public 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user