-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update zxing to newest version and upgrade example to Java 8
- Loading branch information
Showing
5 changed files
with
172 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...re-examples/webcam-capture-qrcode/src/main/java/com/github/sarxos/example2/QrCapture.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.github.sarxos.example2; | ||
import java.awt.FlowLayout; | ||
import java.awt.event.WindowAdapter; | ||
import java.awt.event.WindowEvent; | ||
import java.awt.image.BufferedImage; | ||
import java.io.Closeable; | ||
import java.util.concurrent.Exchanger; | ||
|
||
import javax.swing.JFrame; | ||
|
||
import com.github.sarxos.webcam.Webcam; | ||
import com.github.sarxos.webcam.WebcamPanel; | ||
import com.github.sarxos.webcam.WebcamResolution; | ||
import com.google.zxing.BinaryBitmap; | ||
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 QrCapture extends JFrame implements Closeable { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private Webcam webcam = null; | ||
private BufferedImage image = null; | ||
private Result result = null; | ||
private Exchanger<String> exchanger = new Exchanger<String>(); | ||
|
||
public QrCapture() { | ||
|
||
super(); | ||
|
||
setLayout(new FlowLayout()); | ||
setTitle("Capture"); | ||
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); | ||
|
||
addWindowListener(new WindowAdapter() { | ||
|
||
@Override | ||
public void windowClosing(WindowEvent e) { | ||
close(); | ||
} | ||
}); | ||
|
||
webcam = Webcam.getDefault(); | ||
webcam.setViewSize(WebcamResolution.QVGA.getSize()); | ||
webcam.open(); | ||
|
||
add(new WebcamPanel(webcam)); | ||
|
||
pack(); | ||
setVisible(true); | ||
|
||
final Thread daemon = new Thread(() -> { | ||
while (isVisible()) { | ||
read(); | ||
} | ||
}); | ||
daemon.setDaemon(true); | ||
daemon.start(); | ||
} | ||
|
||
private static BinaryBitmap toBinaryBitmap(BufferedImage image) { | ||
return new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image))); | ||
} | ||
|
||
private void read() { | ||
|
||
if (!webcam.isOpen()) { | ||
return; | ||
} | ||
if ((image = webcam.getImage()) == null) { | ||
return; | ||
} | ||
|
||
try { | ||
result = new MultiFormatReader().decode(toBinaryBitmap(image)); | ||
} catch (NotFoundException e) { | ||
return; // fall thru, it means there is no QR code in image | ||
} | ||
|
||
if (result != null) { | ||
try { | ||
exchanger.exchange(result.getText()); | ||
} catch (InterruptedException e) { | ||
return; | ||
} finally { | ||
dispose(); | ||
} | ||
} | ||
} | ||
|
||
public String getResult() throws InterruptedException { | ||
return exchanger.exchange(null); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
webcam.close(); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
.../webcam-capture-qrcode/src/main/java/com/github/sarxos/example2/WebcamQRCodeExample2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.github.sarxos.example2; | ||
import java.awt.FlowLayout; | ||
import java.awt.event.ActionEvent; | ||
|
||
import javax.swing.AbstractAction; | ||
import javax.swing.JButton; | ||
import javax.swing.JFrame; | ||
import javax.swing.JOptionPane; | ||
|
||
public class WebcamQRCodeExample2 extends JFrame { | ||
|
||
public WebcamQRCodeExample2() { | ||
|
||
setTitle("Main frame"); | ||
setLayout(new FlowLayout()); | ||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
|
||
add(new JButton(new AbstractAction("CAPTURE QR") { | ||
|
||
@Override | ||
public void actionPerformed(ActionEvent e) { | ||
final Thread thread = new Thread(() -> { | ||
try (QrCapture qr = new QrCapture()) { | ||
showMessage("QR code text is:\n" + qr.getResult() + ""); | ||
} catch (InterruptedException ex) { | ||
ex.printStackTrace(); | ||
} | ||
}); | ||
thread.setDaemon(true); | ||
thread.start(); | ||
} | ||
})); | ||
|
||
pack(); | ||
setVisible(true); | ||
} | ||
|
||
private void showMessage(String text) { | ||
JOptionPane.showMessageDialog(null, text); | ||
} | ||
|
||
public static void main(String[] args) { | ||
new WebcamQRCodeExample2(); | ||
} | ||
} |