Skip to content

Instantly share code, notes, and snippets.

@JoelGeraci-Datalogics
Last active June 10, 2016 22:02
Show Gist options
  • Save JoelGeraci-Datalogics/dd9e214d4c584d61f5b1 to your computer and use it in GitHub Desktop.
Save JoelGeraci-Datalogics/dd9e214d4c584d61f5b1 to your computer and use it in GitHub Desktop.
This sample finds images in a PDF file and decodes any barcodes that are found.
/*
* Copyright Datalogics, Inc. 2015
*/
package pdfjt.cookbook.forms;
import com.adobe.internal.io.ByteArrayByteWriter;
import com.adobe.internal.io.ByteReader;
import com.adobe.internal.io.InputStreamByteReader;
import com.adobe.pdfjt.core.types.ASName;
import com.adobe.pdfjt.pdf.document.PDFDocument;
import com.adobe.pdfjt.pdf.document.PDFOpenOptions;
import com.adobe.pdfjt.pdf.graphics.xobject.PDFXObjectImage;
import com.adobe.pdfjt.pdf.graphics.xobject.PDFXObjectImageWithLocation;
import com.adobe.pdfjt.pdf.graphics.xobject.PDFXObjectImageWithLocationMap;
import com.adobe.pdfjt.services.imageconversion.BufferedImageWrapper;
import com.adobe.pdfjt.services.imageconversion.ImageManager;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
/**
* This sample finds images in a PDF file and decodes any barcodes that are
* found.
*/
public class ReadBarcodeFromPDF {
private static final String inputPDFPath = "http://dev.datalogics.com/cookbook/forms/";
private static final List<String> fileNameList = Arrays.asList("ReadBarcodeImage_QRCode.pdf", "ReadBarcodeImage_DataMatrix.pdf",
"ReadBarcodeImage_PDF417.pdf");
public static void main(String[] args) throws Exception {
/*
* For speed, declare which formats we're actually expecting. Only these
* three will be tried.
*/
List<BarcodeFormat> formats = Arrays.asList(BarcodeFormat.QR_CODE, BarcodeFormat.DATA_MATRIX, BarcodeFormat.PDF_417);
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.POSSIBLE_FORMATS, formats);
/*
* There's nothing in the image but the barcode, and the barcode is
* probably lined up straight
*/
hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
// Try harder to get a valid decode.
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
for (String fileName : fileNameList) {
/*
* Read in each PDF file
*/
URLConnection connection = new URL(inputPDFPath + fileName).openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
connection.connect();
InputStream fis = connection.getInputStream();
ByteReader byteReader = new InputStreamByteReader(fis);
PDFDocument pdfDocument = PDFDocument.newInstance(byteReader, PDFOpenOptions.newInstance());
/*
* Create a map of all of the images in the PDF Document then
* iterate through them converting each into a BufferedImage
*/
PDFXObjectImageWithLocationMap pdfXObjectMap = ImageManager.getPDFXObjectMap(pdfDocument);
Iterator<ASName> keyIterator = pdfXObjectMap.keySet().iterator();
while (keyIterator.hasNext()) {
ASName key = keyIterator.next();
/*
* Zebra Crossing requires a BufferedImage as input to the
* decoding process so we need to create one from each
* PDFXObjectImage using the ImageManager, a class that
* facilitates converting images between standard Java
* representations and that used by PDF.
*/
PDFXObjectImageWithLocation pdfXObjectImageWithLocation = pdfXObjectMap.get(key);
PDFXObjectImage pdfXObjectImage = pdfXObjectImageWithLocation.getXImage();
/*
* Only try to decode the image if it's grayscale; don't try with color images.
*/
if (pdfXObjectImage.getColorSpace().getName() == ASName.k_DeviceGray) {
ByteArrayByteWriter byteArrayByteWriter = new ByteArrayByteWriter();
BufferedImageWrapper bufferedImageWrapper = ImageManager.toBufferedImage(pdfXObjectImage, byteArrayByteWriter);
BufferedImage bufferedImage = bufferedImageWrapper.getBufferedImage();
/*
* Now that we have the BufferedImage, we can use the Zebra
* Crossing tools to decode the image.
*/
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
MultiFormatReader reader = new MultiFormatReader();
try {
Result result = reader.decode(bitmap, hints);
System.out.println(fileName + " - " + result.getBarcodeFormat() + " Found:");
System.out.println(result.getText());
System.out.println();
} catch (Exception e) {
/*
* The image probably wasn't a barcode or wasn't one of
* the three we tried to read
*/
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment