Skip to content

Commit

Permalink
Update zxing to newest version and upgrade example to Java 8
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Aug 8, 2017
1 parent 35536a8 commit 5b985bf
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 8 deletions.
5 changes: 2 additions & 3 deletions webcam-capture-examples/webcam-capture-qrcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ This example show how to read QR and bar codes with Webcam Capture.

This example show how to process QR and bar codes with your webcam using
[Webcam Capture](https://github.com/sarxos/webcam-capture)
project together with [ZXing](https://github.com/zxing/zxing) library.
project together with awesome [ZXing](https://github.com/zxing/zxing) library.

Demo showing how it is working is **[available](http://youtu.be/JLUJWgveEco)**
on YouTube.
Youtube video demonstrating how it is working is [available here](http://youtu.be/JLUJWgveEco).

### Screenshoots:

Expand Down
21 changes: 18 additions & 3 deletions webcam-capture-examples/webcam-capture-qrcode/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

Expand All @@ -19,12 +20,26 @@
<groupId>com.github.sarxos</groupId>
<artifactId>webcam-capture</artifactId>
<version>${project.version}</version>
</dependency>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>2.1</version>
<version>3.3.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.sarxos.webcam;
package com.github.sarxos.example1;

import java.awt.Dimension;
import java.awt.FlowLayout;
Expand All @@ -10,6 +10,9 @@
import javax.swing.JFrame;
import javax.swing.JTextArea;

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.LuminanceSource;
import com.google.zxing.MultiFormatReader;
Expand All @@ -18,7 +21,6 @@
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;


public class WebcamQRCodeExample extends JFrame implements Runnable, ThreadFactory {

private static final long serialVersionUID = 6441489157408381878L;
Expand All @@ -43,6 +45,7 @@ public WebcamQRCodeExample() {

panel = new WebcamPanel(webcam);
panel.setPreferredSize(size);
panel.setFPSDisplayed(true);

textarea = new JTextArea();
textarea.setEditable(false);
Expand Down
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();
}
}
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();
}
}

0 comments on commit 5b985bf

Please sign in to comment.