一、准备工作
1、https://github.com/zxing/zxing/releases下载zxing项目的压缩包,并进行解压缩后备用;
2.因为下载下来的zxing并没有给我们提供jar包,为了得到这样的一个jar包,我们在eclipse中建一个java项目zxing;
3.然后把下载下来的core下面的com包(路径:/Users/xxx/Downloads/zxing-zxing-3.3.3/core/src/main/java)拷贝到eclipse项目中的src下;
将javase下面的com包(路径:/Users/xxx/Downloads/zxing-zxing-3.3.3/javase/src/main/java)也拷贝至eclipse的src下;
4.将eclipse的zxing项目导出成jar包,忽略导出过程中提示的警告;
这个jar包就是之后用作生成二维码使用的,此时可以将这个eclipse的项目删除掉啦;
接下来就是通过zxing来实现二维码的生成啦。
二、通过zxing生成二维码
1.eclipse,创建java项目:erya_zxing,并创建类CreateQRCode;
2.项目右键-new-Folder,取名为lib;
将准备工作中生成的jar包拷贝至lib中;
项目-右键-Build Path-Configure Build Path;
添加jar包:
3、代码部分
package com.erya.zxing;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
//生成二维码
public class CreateQRCode {
public static void main(String[] args){
int width = 300;
int height = 300;
String format = "png";
String content = "https://blog.csdn.net/Lydia0309";
//定义二维码的参数
HashMap hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);
hints.put(EncodeHintType.MARGIN, 2);
//生成二维码
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints);
Path file = new File("/Users/xxx/Desktop/qrcode/1.png").toPath();
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
运行结果图如下:
三、通过zxing解析二维码
直接上代码:
package com.erya.zxing;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
public class ReadQRCode {
public static void main(String[] args) {
try {
MultiFormatReader formatReader=new MultiFormatReader();
File file=new File("/Users/xxx/Desktop/qrcode/1.png");
BufferedImage image=ImageIO.read(file);
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 = formatReader.decode(binaryBitmap,hints);
System.out.println("解析结果:"+result.toString());
System.out.println("二维码格式类型:"+result.getBarcodeFormat());
System.out.println("二维码文本内容:"+result.getText());
} catch (NotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
中间遇到报错:com.google.zxing.NotFoundException
参考了一篇文章,抛错原因是代码识别的时候就会把透明色识别为黑色,这样就导致整个二维码图片全是黑色像素,所以zxing抛出com.google.zxing.NotFoundException异常,所以加上一句hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);即可解决问题,参考文章链接https://blog.csdn.net/cyl1226541/article/details/70557232
运行结果如下图: