-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQR.java
62 lines (51 loc) · 1.72 KB
/
QR.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
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import com.github.sarxos.webcam.Webcam;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
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 QR {
public static String ReadQr() {
String resu;
Webcam webcam = Webcam.getDefault(); // non-default (e.g. USB) webcam can be used too
webcam.setViewSize(new Dimension(640,480));
webcam.open();
do {
Result result = null;
BufferedImage image = null;
if (webcam.isOpen()) {
if ((image = webcam.getImage()) == null) {
continue;
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
result = new MultiFormatReader().decode(bitmap);
} catch (NotFoundException e) {
//ImageIO.write(webcam.getImage(), "PNG", new File("C:\\Users\\THE KING IS HERE\\Desktop\\hello-world.png"));
result=null;
// fall thru, it means there is no QR code in image
}
}
if (result != null) {
Toolkit.getDefaultToolkit().beep();
resu=result.getText();
return resu;
//try {
//TimeUnit.SECONDS.sleep(1);
//} catch (InterruptedException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
//}
}
}while(true);
}
}