Skip to content

Commit

Permalink
Add example how to read QR code with web camera
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Nov 24, 2012
1 parent 7340d24 commit c86efe1
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 5 deletions.
1 change: 1 addition & 0 deletions webcam-capture-examples/pom.xml
Expand Up @@ -20,6 +20,7 @@
<module>webcam-capture-executable</module>
<module>webcam-capture-motiondetector</module>
<module>webcam-capture-painter</module>
<module>webcam-capture-qrcode</module>
</modules>

</project>
26 changes: 26 additions & 0 deletions webcam-capture-examples/webcam-capture-qrcode/.classpath
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions webcam-capture-examples/webcam-capture-qrcode/.project
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>webcam-capture-example-qrcode</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
35 changes: 35 additions & 0 deletions webcam-capture-examples/webcam-capture-qrcode/README.md
@@ -0,0 +1,35 @@
# Webcam Capture QR Code Reader Example

This example show how to read QR codes with Webcam Capture.

## What Is This

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

### Screenshoots:

First I took a picture of some sample QR code I found on Google
([link](http://www.google.pl/imgres?um=1&hl=pl&client=firefox-a&sa=N&tbo=d&rls=org.mozilla:en-US:official&biw=1366&bih=552&tbm=isch&tbnid=ZnVvKF0A2BGNTM:&imgrefurl=http://www.qrstuff.com/&docid=1A-zeI71ulZS1M&imgurl=http://www.qrstuff.com/images/sample.png&w=3000&h=3000&ei=yLSwUNTdCIOC4gS1xIGIBQ&zoom=1&iact=hc&vpx=154&vpy=141&dur=1160&hovh=225&hovw=225&tx=124&ty=78&sig=117284320547613276213&page=1&tbnh=149&tbnw=159&start=0&ndsp=24&ved=1t:429,r:1,s:0,i:87))
and then put it opposite to my build-in PC web camera.

This is picture from my cell phone:

![QR Code](https://raw.github.com/sarxos/webcam-capture/master/webcam-capture-examples/webcam-capture-qrcode/src/etc/resources/20121124.jpg "QR Code")

In the application window webcam image is displayed on the left side, and on the right
side there is a text area containing QR code data. In this case this is just a link
to the web page from where I took my QR code example.

![QR Code Example Application](https://raw.github.com/sarxos/webcam-capture/master/webcam-capture-examples/webcam-capture-qrcode/src/etc/resources/qrcode-zxing.png "QR Code Example Application")

## License

Copyright (C) 2012 Bartosz Firyn

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 changes: 30 additions & 0 deletions webcam-capture-examples/webcam-capture-qrcode/pom.xml
@@ -0,0 +1,30 @@
<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>

<parent>
<groupId>com.github.sarxos</groupId>
<artifactId>webcam-capture-examples</artifactId>
<version>0.3.5</version>
</parent>

<artifactId>webcam-capture-example-qrcode</artifactId>
<packaging>jar</packaging>

<name>Webcam Capture - QR Code Example</name>
<description>Example of how to read QR codes with your PC webcam</description>

<dependencies>
<dependency>
<groupId>com.github.sarxos</groupId>
<artifactId>webcam-capture</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>2.1</version>
</dependency>
</dependencies>

</project>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,104 @@
package com.github.sarxos.webcam;

import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import javax.swing.JFrame;
import javax.swing.JTextArea;

import com.github.sarxos.webcam.ds.buildin.WebcamDefaultDevice;
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 WebcamQRCodeExample extends JFrame implements Runnable, ThreadFactory {

private static final long serialVersionUID = 6441489157408381878L;

private Executor executor = Executors.newSingleThreadExecutor(this);

private Webcam webcam = null;
private WebcamPanel panel = null;
private JTextArea textarea = null;

public WebcamQRCodeExample() {
super();

setLayout(new FlowLayout());
setTitle("Read QR Code With Webcam");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

webcam = Webcam.getDefault();
webcam.setViewSize(WebcamDefaultDevice.SIZE_QVGA);

panel = new WebcamPanel(webcam);
panel.setPreferredSize(WebcamDefaultDevice.SIZE_QVGA);

textarea = new JTextArea();
textarea.setEditable(false);
textarea.setPreferredSize(WebcamDefaultDevice.SIZE_QVGA);

add(panel);
add(textarea);

pack();
setVisible(true);

executor.execute(this);
}

@Override
public void run() {

do {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}

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) {
// fall thru, it means there is no QR code in image
}
}

if (result != null) {
textarea.setText(result.getText());
}

} while (true);
}

@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "example-runner");
t.setDaemon(true);
return t;
}

public static void main(String[] args) {
new WebcamQRCodeExample();
}
}
Expand Up @@ -27,6 +27,13 @@

public class WebcamDefaultDevice implements WebcamDevice {

public static final Dimension SIZE_QQVGA = new Dimension(176, 144);
public static final Dimension SIZE_QVGA = new Dimension(320, 240);
public static final Dimension SIZE_CIF = new Dimension(352, 288);
public static final Dimension SIZE_HVGA = new Dimension(480, 400);
public static final Dimension SIZE_VGA = new Dimension(640, 480);
public static final Dimension SIZE_XGA = new Dimension(1024, 768);

/**
* Logger.
*/
Expand All @@ -38,11 +45,12 @@ public class WebcamDefaultDevice implements WebcamDevice {
*/
//@formatter:off
private final static Dimension[] DIMENSIONS = new Dimension[] {
new Dimension(176, 144),
new Dimension(320, 240),
new Dimension(352, 288),
new Dimension(640, 400),
new Dimension(640, 480),
SIZE_QQVGA,
SIZE_QVGA,
SIZE_CIF,
SIZE_HVGA,
SIZE_VGA,
SIZE_XGA,
};
//@formatter:on

Expand Down Expand Up @@ -129,6 +137,11 @@ public BufferedImage getImage() {
byte[] bytes = imageTask.getImage(size);
byte[][] data = new byte[][] { bytes };

if (bytes == null) {
LOG.error("Images byte array is null!");
return null;
}

DataBufferByte buffer = new DataBufferByte(data, bytes.length, OFFSET);
WritableRaster raster = Raster.createWritableRaster(sampleModel, buffer, null);

Expand Down

0 comments on commit c86efe1

Please sign in to comment.