Skip to content

Instantly share code, notes, and snippets.

@friek
Created October 10, 2018 20:13
Show Gist options
  • Save friek/b961018a99d064d87d5e8c2d95942df7 to your computer and use it in GitHub Desktop.
Save friek/b961018a99d064d87d5e8c2d95942df7 to your computer and use it in GitHub Desktop.
zxing barcode scanner example
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.multi.GenericMultipleBarcodeReader;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.EnumMap;
import java.util.Map;
public class BarcodeScanner
{
public static void main(final String[] args)
{
if (args.length != 1)
{
System.err.println("Usage: " + BarcodeScanner.class.getCanonicalName() + " <image dir>");
System.exit(1);
}
File inputDir = new File(args[0]);
if (!inputDir.isDirectory())
{
System.err.println("Input " + inputDir + " is not a directory");
System.exit(1);
}
for (File input : inputDir.listFiles())
{
if (!input.isFile())
continue;
String filename = input.getName();
try (BufferedInputStream bfin = new BufferedInputStream(new FileInputStream(input)))
{
BufferedImage bfi = ImageIO.read(bfin);
if (bfi == null)
{
System.err.println("Could not read image from " + filename);
continue;
}
LuminanceSource ls = new BufferedImageLuminanceSource(bfi);
BinaryBitmap bmp = new BinaryBitmap(new HybridBinarizer(ls));
GenericMultipleBarcodeReader reader = new GenericMultipleBarcodeReader(new MultiFormatReader());
Result[] results;
Map<DecodeHintType, Object> decodeHints = new EnumMap<>(DecodeHintType.class);
decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
results = reader.decodeMultiple(bmp, decodeHints);
System.out.println("Found " + results.length + " barcodes in " + filename);
int i = 0;
for (Result result : results)
System.out.println("Barcode " + ++i + ": " + result.getText());
}
catch (NotFoundException e)
{
System.err.println("No barcodes found in " + filename);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
System.out.println("---------------------------------------------------------");
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>nl.mumasoft</groupId>
<artifactId>barcodescanner</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<version>2.3.2</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment