1.导入依赖
<dependency>
<groupId>QRCode</groupId>
<artifactId>QRCode</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.1</version>
</dependency>
2.引入工具类
/**
* 生成二维码
*
* @return
*/
public static String encodeQRCode(String text, String path) {
int width = 150;
int height = 150;
String format = "png";
//定义二维码的参数
HashMap hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.MARGIN, 2);
//生成二维码
BitMatrix bitMatrix = null;
try {
bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
} catch (WriterException e) {
e.printStackTrace();
}
Path file = new File(path).toPath();
try {
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
} catch (IOException e) {
e.printStackTrace();
}
return file.toString();
}
/**
* 对二维码图片进行解码
*
* @param filePath 二维码路径
* @return 解码后对内容
*/
public static String decodeQRCode(String filePath) {
MultiFormatReader formatReader = new MultiFormatReader();
File file = new File(filePath);
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
//定义二维码的参数
HashMap hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//为了解决com.google.zxing.NotFoundException报错问题
hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
//结果展示
Result result = null;
try {
result = formatReader.decode(binaryBitmap, hints);
} catch (NotFoundException e) {
e.printStackTrace();
}
return result.getText();
}
3.测试controller
@Api("二维码")
@RestController
@RequestMapping(value = "/test/qrCode")
public class QrCodeController {
@Value("${oss.endPoint}")
private String endpoint;
@Value("${oss.accessKeyId}")
private String accessKeyId;
@Value("${oss.accessKeySecret}")
private String accessKeySecret;
@Value("${oss.bucketName}")
private String bucketName;
@Value("${oss.url}")
private String url;
@Value("${oss.qrCodeUrl}")
private String qrCodeUrl;
@Value("${oss.qrCodeAbsolutePath}")
private String qrCodeAbsolutePath;
@ControllerEndpoint(operation = "qrCode", exceptionMessage = "qrCode")
@ApiOperation("")
@PostMapping("/qr")
public String qrCode() {
User user = (User) SecurityUtils.getSubject().getPrincipal();
//生成二维码 //内容+二维码保存路径
String image = QRCodeUtils.encodeQRCode((user.getUsername()) + "," + "传参", qrCodeAbsolutePath);
//解析二维码
String qrCodeResult = QRCodeUtils.decodeQRCode(qrCodeAbsolutePath);
//获取真实的文件名 去掉后缀
String fileName = UploadUtils.subFileName(image);
//生成的文件名
String uuidFileName = UploadUtils.generateRandonFileName(fileName);
String dir = "XXXX";
// 上传文件流。
String key = "file/" + dir + "/" + uuidFileName;
//创建OSSClient实例。
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// 上传文件。
ossClient.putObject(bucketName, key, new File(image));
// 关闭OSSClient。
String qrCode = qrCodeUrl + key;
ossClient.shutdown();
HashMap map = new HashMap();
map.put("qrCode", qrCode);
map.put("qrCodeResult", qrCodeResult);
return map.toString();
}
4 .生成的二维码
5.解析的数据
Response body
{qrCode=https://XXXX.com/file/XXX/a54ccffeadf540c1ab58e225642f41ae.jpg, qrCodeResult=XXX,传参}