1.部署依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency>
2.二维码的生成
String url="www.baidu.com";//二维码的内容
int widthHeight=500;
//二维码生成的路劲和名字
String filePathName="C:\\users\\asus\\desktop\\"+System.currentTimeMillis()+".jpg";
Path path= FileSystems.getDefault().getPath(filePathName);
QRCodeWriter writer=new QRCodeWriter();
BitMatrix matrix;
try {
matrix = writer.encode(url, BarcodeFormat.QR_CODE,widthHeight,widthHeight);
try {
//已生成二维码
MatrixToImageWriter.writeToPath(matrix,"JPG",path);
} catch (IOException e) {
e.printStackTrace();
}
} catch (WriterException e) {
e.printStackTrace();
}
System.out.println("完成");
3.二维码的解析
//要解析的二维码路径
String filename="C:\\users\\asus\\desktop\\1651062084114.jpg";
try {
BufferedImage image= ImageIO.read(new File(filename));
LuminanceSource source=new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap=new BinaryBitmap(new HybridBinarizer(source));
Map map=new HashMap();
map.put(DecodeHintType.CHARACTER_SET,"utf-8");
try {
Result result= new MultiFormatReader().decode(bitmap,map);
System.out.println("解析结果:"+result.toString());
System.out.println("二维码格式"+result.getBarcodeFormat());
System.out.println("文本内容"+result.getText());
System.out.println(result.getNumBits());
} catch (NotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}