-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqrCodeScanner.java
103 lines (94 loc) · 3.61 KB
/
qrCodeScanner.java
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import com.swetake.util.Qrcode;
import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;
import jp.sourceforge.qrcode.data.QRCodeSymbol;
import jp.sourceforge.qrcode.exception.DecodingFailedException;
import jp.sourceforge.qrcode.reader.QRCodeImageReader;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
class QRCodeScanner {
String path = "";
public QRCodeScanner(String path){
this.path = path;
}
public List<String> decode() {
//System.out.println("开始解析二维码!!");
/* 读取二维码图像数据 */
File imageFile = new File(path);
BufferedImage image;
List<String> decodedData = new ArrayList<String> (){{add("Not an Image");add("null");}};
try {
image = ImageIO.read(imageFile);
}
catch (IOException e) {
//System.out.println("读取二维码图片失败: " + e.getMessage());
return decodedData;
}
//System.out.println("读取二维码图片失败: " + e.getMessage());
/* 解二维码 */
QRCodeDecoder decoder = new QRCodeDecoder();
try {
decodedData.set(0,new String(decoder.decode(new J2SEImageGucas(image))));
}
catch (DecodingFailedException e) {
decodedData.set(0, "Not a QR Code");
//System.out.println("读取二维码图片失败: " + e.getMessage());
return decodedData;
}
int[][] intImage = imageToIntArray(new J2SEImageGucas(image));
QRCodeImageReader imageReader = new QRCodeImageReader();
//QRCodeImageReader imageReader = new QRCodeImageReader();
QRCodeSymbol qrCodeSymbol = imageReader.getQRCodeSymbol(intImage);
decodedData.set(1, qrCodeSymbol.getVersionReference());
/*这里打印出来的就是version和mask pattern 怎么返回就交给你了
int[][] intImage = imageToIntArray(new J2SEImageGucas(image));
imageReader = new QRCodeImageReader();
QRCodeImageReader imageReader = new QRCodeImageReader();
QRCodeSymbol qrCodeSymbol = imageReader.getQRCodeSymbol(intImage);
System.out.println("解析内容如下:"+decodedData);
System.out.println("Version: " + qrCodeSymbol.getVersionReference());
System.out.println("Mask pattern: " + qrCodeSymbol.getMaskPatternRefererAsString());
*/
return decodedData;
}
private static int[][] imageToIntArray(QRCodeImage image) {
int width = image.getWidth();
int height = image.getHeight();
int[][] intImage = new int[width][height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
intImage[x][y] = image.getPixel(x,y);
}
}
return intImage;
}
public static void main(String[] args) {
QRCodeScanner sc = new QRCodeScanner("E:/can/3.jpg");
System.out.println(sc.decode());
}
}
class J2SEImageGucas implements QRCodeImage {
BufferedImage image;
public J2SEImageGucas(BufferedImage image) {
this.image = image;
}
public int getWidth() {
return image.getWidth();
}
public int getHeight() {
return image.getHeight();
}
public int getPixel(int x, int y) {
return image.getRGB(x, y);
}
}