Commit 15000ac4 authored by fangxinjiang's avatar fangxinjiang

二维码生成fxj 2024-06-14

parent 58f90fe2
...@@ -94,6 +94,18 @@ ...@@ -94,6 +94,18 @@
<version>4.1.1</version> <version>4.1.1</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<!-- 二维码生成JAR开始 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.5.1</version>
</dependency>
<!-- 二维码生成JAR结束 -->
</dependencies> </dependencies>
<build> <build>
......
...@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers; ...@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TAttaInfo; import com.yifu.cloud.plus.v1.yifu.archives.entity.TAttaInfo;
import com.yifu.cloud.plus.v1.yifu.archives.service.FileUploadService; import com.yifu.cloud.plus.v1.yifu.archives.service.FileUploadService;
import com.yifu.cloud.plus.v1.yifu.archives.service.TAttaInfoService; import com.yifu.cloud.plus.v1.yifu.archives.service.TAttaInfoService;
import com.yifu.cloud.plus.v1.yifu.archives.service.impl.FileUploadServiceImpl;
import com.yifu.cloud.plus.v1.yifu.archives.utils.QrCodeUtil;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common; import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.OSSUtil; import com.yifu.cloud.plus.v1.yifu.common.core.util.OSSUtil;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R; import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
...@@ -143,4 +145,16 @@ public class FileUploadController { ...@@ -143,4 +145,16 @@ public class FileUploadController {
} }
return R.ok(voList); return R.ok(voList);
} }
/**
* 获取附件
* @author fxj
* @return R
*/
@Operation(summary = "生成二维码并上传阿里云保存到本地", description = "生成二维码并上传阿里云保存到本地")
@SysLog("生成二维码并上传阿里云保存到本地")
@GetMapping("/createQrCode")
public R<FileVo> createQrCode(@RequestParam String domainId,@RequestParam String url,@RequestParam String name) {
return fileUploadService.createQrCode(domainId,url,name);
}
} }
package com.yifu.cloud.plus.v1.yifu.archives.service; package com.yifu.cloud.plus.v1.yifu.archives.service;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TAttaInfo;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R; import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.FileVo; import com.yifu.cloud.plus.v1.yifu.common.core.vo.FileVo;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
...@@ -15,4 +16,6 @@ import java.io.IOException; ...@@ -15,4 +16,6 @@ import java.io.IOException;
public interface FileUploadService { public interface FileUploadService {
R<FileVo> uploadImg(MultipartFile file, String filePath, Integer type, String domain) throws IOException; R<FileVo> uploadImg(MultipartFile file, String filePath, Integer type, String domain) throws IOException;
R<FileVo> createQrCode(String domainId, String url, String name);
} }
package com.yifu.cloud.plus.v1.yifu.archives.service.impl; package com.yifu.cloud.plus.v1.yifu.archives.service.impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TAttaInfo; import com.yifu.cloud.plus.v1.yifu.archives.entity.TAttaInfo;
import com.yifu.cloud.plus.v1.yifu.archives.service.FileUploadService; import com.yifu.cloud.plus.v1.yifu.archives.service.FileUploadService;
import com.yifu.cloud.plus.v1.yifu.archives.service.TAttaInfoService; import com.yifu.cloud.plus.v1.yifu.archives.service.TAttaInfoService;
import com.yifu.cloud.plus.v1.yifu.archives.utils.QrCodeUtil;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common; import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.OSSUtil; import com.yifu.cloud.plus.v1.yifu.common.core.util.OSSUtil;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R; import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.FileVo; import com.yifu.cloud.plus.v1.yifu.common.core.vo.FileVo;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.time.LocalDateTime; import java.time.LocalDateTime;
...@@ -25,6 +30,8 @@ public class FileUploadServiceImpl implements FileUploadService { ...@@ -25,6 +30,8 @@ public class FileUploadServiceImpl implements FileUploadService {
private final TAttaInfoService tAttaInfoService; private final TAttaInfoService tAttaInfoService;
private final QrCodeUtil codeUtil;
@Override @Override
public R<FileVo> uploadImg(MultipartFile file, String filePath, Integer type, String domain) throws IOException { public R<FileVo> uploadImg(MultipartFile file, String filePath, Integer type, String domain) throws IOException {
if (null == file) { if (null == file) {
...@@ -78,6 +85,57 @@ public class FileUploadServiceImpl implements FileUploadService { ...@@ -78,6 +85,57 @@ public class FileUploadServiceImpl implements FileUploadService {
} }
} }
private R<FileVo> uploadImg2(InputStream fileIn, String filePath, Integer type, String domain,String fileName) throws IOException {
if (null == fileIn) {
return R.failed("文件上传异常,请重新上传!");
}
if (Common.isEmpty(fileName)) {
return R.failed("文件名不能为空!");
}
if (fileName.length() > 100) {
return R.failed("文件名不能超过100个字符!");
}
//filePath不传默认存储空间的根目录
//jpg,jpeg,png,bmp
String key = "";
if (Common.isNotNull(filePath)) {
key = filePath + "/" + System.currentTimeMillis() + fileName;
} else {
key = System.currentTimeMillis() + fileName;
}
if (!Common.checkFile(key)) {
return R.failed("非法上传类型!");
}
boolean flag = ossUtil.uploadFileByStream(fileIn, key, null);
FileVo fileVo;
TAttaInfo attaInfo;
URL url;
if (flag) {
log.info("文件:" + fileName + "上传至存储空间" + ossUtil.getBucketName() + "成功!");
attaInfo = initUnitAttaForInsert(fileName, key, 0);
attaInfo.setRelationType(String.valueOf(type));
try {
attaInfo.setDomainId(domain);
attaInfo = tAttaInfoService.add(attaInfo);
} catch (Exception e) {
log.error("OSS文件上传接口异常:" + e.getMessage());
ossUtil.deleteObject(null, key);
return R.failed("fail:" + e.getMessage());
}
//有实体id则插入关系,用于编辑
if (Common.isNotNull(domain)) {
url = ossUtil.getObjectUrl(null, attaInfo.getAttaSrc());
fileVo = new FileVo(attaInfo.getId(), attaInfo.getAttaName(), url.toString());
return R.ok(fileVo, "success");
}
url = ossUtil.getObjectUrl(null, attaInfo.getAttaSrc());
fileVo = new FileVo(attaInfo.getId(), attaInfo.getAttaName(), url.toString());
return R.ok(fileVo, "success");
} else {
return R.failed("fail:上传失败");
}
}
/** /**
* 对象初始化 * 对象初始化
* @param fileName * @param fileName
...@@ -102,4 +160,21 @@ public class FileUploadServiceImpl implements FileUploadService { ...@@ -102,4 +160,21 @@ public class FileUploadServiceImpl implements FileUploadService {
unitAtta.setCreateTime(LocalDateTime.now()); unitAtta.setCreateTime(LocalDateTime.now());
return unitAtta; return unitAtta;
} }
}
@Override
public R<FileVo> createQrCode(String domainId, String url, String name) {
TAttaInfo attaInfo = tAttaInfoService.getOne(Wrappers.<TAttaInfo>query().lambda().eq(TAttaInfo::getAttaSrc,domainId).last(CommonConstants.LAST_ONE_SQL));
FileVo fileVo;
if (Common.isNotNull(attaInfo)){
fileVo = new FileVo(attaInfo.getId(), attaInfo.getAttaName(), url.toString());
return R.ok(fileVo);
}
InputStream in = codeUtil.createPng(url);
try {
return this.uploadImg2(in,"mvp_qr",CommonConstants.EIGHT_INT,domainId,name);
} catch (IOException e) {
log.error("获取二维码失败",e);
return R.failed(CommonConstants.RESULT_DATA_FAIL);
}
}
}
\ No newline at end of file
package com.yifu.cloud.plus.v1.yifu.archives.utils;
import cn.hutool.core.net.multipart.MultipartRequestInputStream;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TAttaInfo;
import com.yifu.cloud.plus.v1.yifu.common.core.util.OSSUtil;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
/**
* @Author fxj
* @Date 2024/6/13
* @Description
* @Version 1.0
*/
@Slf4j
@Component
public class QrCodeUtil {
public InputStream createPng(String qrCodeContent){
try {
// 定义二维码的配置参数
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); // 设置纠错级别(L、M、Q、H,从低到高)
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设置字符集
// 创建二维码编码器
QRCodeWriter qrCodeWriter = new QRCodeWriter();
// 生成二维码矩阵
BitMatrix bitMatrix = qrCodeWriter.encode(qrCodeContent, BarcodeFormat.QR_CODE, 250, 250, hints);
// 将BitMatrix转换为BufferedImage
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); // 黑白二值化
}
}
// 保存为PNG图片
File file = new File("output_qrcode.png");
ImageIO.write(image, "PNG", file);
InputStream in = new FileInputStream(file);
return in;
} catch (IOException e) {
log.error("failed:生成二维码失败",e.getMessage());
} catch (WriterException e) {
log.error("failed:生成二维码失败",e.getMessage());
}
return null;
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.yifu.cloud.plus.v1.yifu.archives.config.FddAutoConfigue com.yifu.cloud.plus.v1.yifu.archives.utils.QrCodeUtil,\
\ No newline at end of file com.yifu.cloud.plus.v1.yifu.archives.config.FddAutoConfigue
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