- 引入依赖
<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>
- 编写工具类
其中包含了 生成二维码、解析上传二维码内容(直解析utf-8和GBK编码的二维码)、解析本地二维码内容,自定义编码格式
package com.meng.weixin.util;
import cn.hutool.extra.qrcode.BufferedImageLuminanceSource;
import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
public class QRCodeUtil {
public static void getQRCode(String contents,String format,String filepath) throws IOException, NotFoundException {
getQRCode(contents, format, filepath,"utf-8");
}
public static void getQRCode(String contents,String format,String filepath,String character) throws IOException, NotFoundException {
getQRCode(300,300,contents,format,filepath,character);
}
public static void getQRCode(int width,int height,String contents,String format,String filepath,String character) throws IOException, NotFoundException {
HashMap map = new HashMap();
map.put(EncodeHintType.CHARACTER_SET,character);
map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
map.put(EncodeHintType.MARGIN, 0);
try {
BitMatrix bm = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height,map);
Path file = new File(filepath).toPath();
MatrixToImageWriter.writeToPath(bm, format, file);
} catch (WriterException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String analysis(File QRImg) throws IOException, NotFoundException {
BufferedImage image = ImageIO.read(QRImg);
BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
HashMap map = new HashMap();
map.put(DecodeHintType.CHARACTER_SET, "utf-8");
Result result = new MultiFormatReader().decode(bb, map);
String QRContents=result.getText();
if (result.getText().contains("�")){
System.out.println("乱码了,更换编码格式(GBK)");
map.put(DecodeHintType.CHARACTER_SET, "GBK");
result = new MultiFormatReader().decode(bb, map);
QRContents=result.getText();
}
System.out.println("二维码文本内容:"+QRContents);
return QRContents;
}
public static String analysis(String filePath) throws IOException, NotFoundException {
BufferedImage image = ImageIO.read(new File(filePath));
BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
HashMap map = new HashMap();
map.put(DecodeHintType.CHARACTER_SET, "utf-8");
Result result = new MultiFormatReader().decode(bb, map);
String QRContents=result.getText();
if (result.getText().contains("�")){
System.out.println("乱码了,更换编码格式(GBK)");
map.put(DecodeHintType.CHARACTER_SET, "GBK");
result = new MultiFormatReader().decode(bb, map);
QRContents=result.getText();
}
System.out.println("二维码文本内容:"+QRContents);
return QRContents;
}
public static String analysis(String filePath,String character) throws IOException, NotFoundException {
BufferedImage image = ImageIO.read(new File(filePath));
BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
HashMap map = new HashMap();
map.put(DecodeHintType.CHARACTER_SET,character);
Result result = new MultiFormatReader().decode(bb, map);
String QRContents=result.getText();
return QRContents;
}
}
- 测试
写一个接口测试
@PostMapping("/QRanalysis")
public Result QRanalysis(@RequestParam("QRImg") MultipartFile QRImg) throws IOException, NotFoundException {
String analysis = QRCodeUtil.analysis(MultipartFileToFile(QRImg));
return Result.ok(analysis);
}
public static File MultipartFileToFile(MultipartFile multiFile) {
String fileName = multiFile.getOriginalFilename();
String prefix = fileName.substring(fileName.lastIndexOf("."));
try {
File file = File.createTempFile(fileName, prefix);
multiFile.transferTo(file);
return file;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
- 结果
生成的二维码在这里: