Zxing和QRCode生成和解析二维码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Zxing和QRCode⽣成和解析⼆维码
⼀、⼆维码的分类
线性堆叠式⼆维码、矩阵式⼆维码、邮政码。
⼆、⼆维码的优缺点
优点:1. ⾼密度编码,信息容量⼤;2.编码范围⼴;3.容错能⼒强;4.译码可靠性⾼;5.可引⼊加密措施;6.成本低,易制作,持久耐⽤。
缺点:1.⼆维码技术成为⼿机病毒、钓鱼⽹站传播的新渠道;2.信息容易泄露。
三、三⼤国际标准
1.PDF417:不⽀持中⽂;
2.DM:专利未公开,需要⽀付专利费⽤;
3.QR Code:专利公开,⽀持中⽂。
其中,QR Code具有识读速度快、数据密度⼤、占⽤空间⼩的优势。
四、纠错能⼒
L级:约可纠错7%的数据码字
M级:约可纠错15%的数据码字
Q级:约可纠错25%的数据码字
H级:约可纠错30%的数据码字
五、ZXing⽣成/读取⼆维码
再次,创建⼀个Java项⽬。
将ZXing源⽂件中的core/src/main/java/com和javase/src/main/java/com两个⽂件复制到项⽬中,编译成jar⽂件;
最后,在以后的开发中就可以使⽤该jar⽂件。
ZXing⽣成⼆维码的代码如下:
1import java.io.File;
2import java.nio.file.Path;
3import java.util.HashMap;
4
5import com.google.zxing.BarcodeFormat;
6import com.google.zxing.EncodeHintType;
7import com.google.zxing.MultiFormatWriter;
8import com.google.zxing.WriterException;
9import com.google.zxing.client.j2se.MatrixToImageWriter;
10import mon.BitMatrix;
11import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
12
13//⽣成⼆维码
14public class CreateQRCode {
15
16public static void main(String[] args) {
17int width = 300; //⼆维码宽度
18int height = 300; //⼆维码⾼度
19 String format = "png"; //⼆维码图⽚格式
20 String content = ""; //⼆维码内容
21
22//定义⼆维码参数
23 HashMap hints = new HashMap();
24 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //定义内容字符集的编码
25 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); //定义纠错等级
26 hints.put(EncodeHintType.MARGIN, 2); //边框空⽩
27
28try {
29 BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
30
31 Path file = new File("E:/img.png").toPath();
32
33 MatrixToImageWriter.writeToPath(bitMatrix, format, file);
34//MatrixToImageWriter.writeToStream(bitMatrix, format, stream);
35
36 } catch (Exception e) {
37 e.printStackTrace();
38 }
39 }
40 }
ZXing读取⼆维码信息的代码如下:
1import java.awt.image.BufferedImage;
2import java.io.File;
3import java.io.IOException;
4import java.util.HashMap;
5
6import javax.imageio.ImageIO;
7
8import com.google.zxing.Binarizer;
9import com.google.zxing.BinaryBitmap;
10import com.google.zxing.EncodeHintType;
11import com.google.zxing.LuminanceSource;
12import com.google.zxing.MultiFormatReader;
13import com.google.zxing.NotFoundException;
14import com.google.zxing.Result;
15import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
16import mon.BitArray;
17import mon.BitMatrix;
18import mon.HybridBinarizer;
19
20public class ReadQRCode {
21
22public static void main(String[] args) {
23
24try {
25 MultiFormatReader formatReader = new MultiFormatReader();
26
27 File file = new File("E:/img.png");
28
29 BufferedImage image = ImageIO.read(file); //读取此⽂件识别成⼀个图⽚
30
31 BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image))); 32
33//定义⼆维码参数
34 HashMap hints = new HashMap();
35 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //定义内容字符集的编码
36
37
38 Result result = formatReader.decode(binaryBitmap,hints);
39
40 System.out.println("解析结果:" + result.toString());
41 System.out.println("⼆维码格式类型:" + result.getBarcodeFormat());
42 System.out.println("⼆维码⽂本内容:" + result.getText());
43 } catch (Exception e) {
44 e.printStackTrace();
45 }
46 }
47 }
六、QRCode⽣成/读取⼆维码
QRCode⽣成和读取⼆维码的jar是分开的,下载⽹址如下:
QRCode⽣成⼆维码代码如下:
1import java.awt.Color;
2import java.awt.Graphics2D;
3import java.awt.image.BufferedImage;
4import java.io.File;
5import java.io.IOException;
6import java.io.UnsupportedEncodingException;
7
8import javax.imageio.ImageIO;
9
10import com.swetake.util.Qrcode;
11
12//⽣成⼆维码
13public class CreateQRCode {
14
15public static void main(String[] args) throws IOException {
16
17//计算⼆维码图⽚的⾼宽⽐
18//API⽂档规定计算图⽚宽⾼的⽅式,v是版本号(1~40)
19int v = 7;
20int width = 67 + 12 * (v - 1); //计算公式
21int height = 67 + 12 * (v - 1);
22
23 Qrcode x = new Qrcode();
24
25/**
26 * 纠错等级分为
27 * level L : 最⼤ 7% 的错误能够被纠正;
28 * level M : 最⼤ 15% 的错误能够被纠正;
29 * level Q : 最⼤ 25% 的错误能够被纠正;
30 * level H : 最⼤ 30% 的错误能够被纠正;
31*/
32 x.setQrcodeErrorCorrect('L'); //设置纠错等级
33 x.setQrcodeEncodeMode('B'); //N代表数字 A代表a-Z B代表其他字符
34 x.setQrcodeVersion(v); //版本号(1~40)
35 String qrData = ""; //内容信息
36
37//缓冲区
38 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 39
40//绘图
41 Graphics2D gs = bufferedImage.createGraphics();
42
43 gs.setBackground(Color.WHITE); //背景⾊
44 gs.setColor(Color.black);
45 gs.clearRect(0, 0, width, height); //清除画板内容
46
47//偏移量 2或7
48int pixoff = 2;
49
50/**
51 * 1.注意for循环⾥⾯的i,j的顺序,
52 * s[j][i]⼆维数组的j,i的顺序要与这个⽅法中的 gs.fillRect(j*3+pixoff,i*3+pixoff, 3, 3);
53 * 顺序匹配,否则会出现解析图⽚是⼀串数字
54 * 2.注意此判断if (d.length > 0 && d.length < 120)
55 * 是否会引起字符串长度⼤于120导致⽣成代码不执⾏,⼆维码空⽩
56 * 根据⾃⼰的字符串⼤⼩来设置此配置
57*/
58//把要填充的内容转化成字节数
59byte[] d = qrData.getBytes("utf-8"); //汉字转化格式
60if (d.length > 0 && d.length < 120) {
61boolean[][] s = x.calQrcode(d);
62
63for (int i = 0; i < s.length; i++) {
64for (int j = 0; j < s.length; j++) {
65if (s[j][i]) {
66//把d中的内容填充
67 gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
68 }
69 }
70 }
71 }
72
73 gs.dispose(); //结束写⼊
74 bufferedImage.flush(); //结束buffered
75 ImageIO.write(bufferedImage, "png", new File("E:/img.png")); //将图⽚写⼊到指定路径下
76
77 }
78 }
QRCode解析⼆维码代码如下:
1import java.awt.image.BufferedImage;
2import java.io.File;
3import java.io.IOException;
4
5import javax.imageio.ImageIO;
6
7import jp.sourceforge.qrcode.QRCodeDecoder;
8
9public class ReadQRCode {
10
11public static void main(String[] args) throws IOException {
12
13//图⽚路径
14 File file = new File("E:/img.png");
15//读取图⽚到缓冲区
16 BufferedImage bufferedImage = ImageIO.read(file);
17//QRCode解码器
18 QRCodeDecoder codeDecoder = new QRCodeDecoder();
19
20/**
21 *codeDecoder.decode(new MyQRCodeImage())
22 *这⾥需要实现QRCodeImage接⼝,MyQRCodeImage.java实现接⼝类
23*/
24//通过解析⼆维码获得信息
25 String result = new String(codeDecoder.decode(new MyQRCodeImage(bufferedImage)), "utf-8");
26 System.out.println(result);
27 }
28 }
MyQRCodeImage.java类实现QRCodeImage接⼝
1import java.awt.image.BufferedImage;
2
3import jp.sourceforge.qrcode.data.QRCodeImage;
4
5public class MyQRCodeImage implements QRCodeImage{
6
7 BufferedImage bufferedImage;
8
9public MyQRCodeImage(BufferedImage bufferedImage) {
10this.bufferedImage = bufferedImage;
11 }
12
13//宽
14 @Override
15public int getWidth() {
16return bufferedImage.getWidth();
17 }
18
19//⾼
20 @Override
21public int getHeight() {
22return bufferedImage.getHeight();
23 }
24
25//颜⾊
26 @Override
27public int getPixel(int i, int j) {
28return bufferedImage.getRGB(i, j);
29 }
30 }
七、注意事项
jar包可放置在lib包下,还要设置此项⽬的builde path,选中项⽬,右击,选择Builde Path,点击Configure Builde Path,在libraries下点击Add Jars添加jar⽂件。