Find QR-Code in PDF

2,705 views
Skip to first unread message

Ernst-August Stehr

unread,
Apr 14, 2014, 1:51:16 PM4/14/14
to zx...@googlegroups.com
Hi all,

I want to read the QR code in attached PDF file. I have tried to directly read the code as well as to use a Detector first. Neither finds any barcode let alone recognizes them. My smartphone easily reads the code. If I manually crop the image just to the QR code it works, but not if other content is on the picture. However I need to handle a barcode and content on a page.

// Code here
BufferedImage image = page.convertToImage(); // Convert PDF page to image
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = null;
bitmap = new BinaryBitmap(new HybridBinarizer(source));
com.google.zxing.qrcode.QRCodeReader qr = new com.google.zxing.qrcode.QRCodeReader();
Result qrCodeResult = qr.decode(bitmap);

// as well as an indirect approach to first find a barcode at all:
// BufferedImage as above
BitMatrix matrix = new HybridBinarizer(source).getBlackMatrix();
com.google.zxing.qrcode.detector.Detector detektiv = new com.google.zxing.qrcode.detector.Detector(matrix);
DetectorResult detResult = detektiv.detect();

Kind Regards
Ernst

Ernst-August Stehr

unread,
Apr 14, 2014, 5:27:22 PM4/14/14
to Hannes Erven, zx...@googlegroups.com
Dear Hannes,

Thank you very much for the fast answer, however it does not work yet. I still do not get the barcode read.
The worst case is to crop the image beforehand, however that means that I need to position the barcode correctly in the first place and for another application (which scans a number of pictures from a digital camera in order to split and assign them to different orders) won't work as I can not guarantee the exact positioning in a smartphone camera.

However in the latter application it found at least one barcode(the number _035.jpg, but not the _034.jpg)

Kind regards

Ernst


-----Ursprüngliche Nachricht-----
Von: Hannes Erven [mailto:han...@erven.at]
Gesendet: Montag, 14. April 2014 21:24
An: Ernst-August Stehr; zx...@googlegroups.com
Betreff: Re: [zxing] Find QR-Code in PDF

Hi Ernst-August,


unfortunately your attachment did not make it to the group, but never mind. The QRCodeReader makes some assumptions about the size a code has in relation to the whole image, which absolutely makes sense in the context of interactively scanning with the camera.

For your use case, I recommend using the com.google.zxing.multi.qrcode.QRCodeMultiReader, which is more relaxed about that.

It's almost a one-line change to your code:
QRCodeMultiReader mr = new QRCodeMultiReader(); final Result[] rs = mr.decodeMultiple(bitmap);


Best regards,

-hannes
Neu2QR.pdf
WP_20140413_034.jpg
WP_20140413_035.jpg

Hannes Erven

unread,
Apr 14, 2014, 6:18:31 PM4/14/14
to Ernst-August Stehr, zx...@googlegroups.com
Dear Ernst-August,


> Thank you very much for the fast answer, however it does not work
> yet. I still do not get the barcode read.

I've fed some pages from your PDF into my application and unfortunately,
it also does not detect the QR codes. But I must add that this
application usually deals with original digital documents (e.g. a
QR-Code added by Photoshop), which ZXing detects rather reliably.

A quick examination of page 1 from your PDF (set a breakpoint on
MultiFinderPatternFinder#selectBestPatterns) shows that Xing finds the
two top finders, but it cannot detect the lower left finder-pattern. I
guess that is due to the largish amount of gray fragments within the
lines. Probably you could adjust the colours to help detection.

As to the camera images, the files seem quite dark and also
blurred/distorted to me. As a general rule, ZXing usually operates
better on images with non-huge resolution so maybe you could get better
results by proprocessing them, that is reduce resolution, optimize
colours to b/w, probably sharpen a bit.
Other people on this list will have more experience with this, although
it has been remarked multiple times that interactive scanning is
apparently easier to accomplish (several images per second, user moving
the device to avoid shadows, more time for autofocus and exposure to
settle).


When I experimented with the MultiQRCodeReader back then, my use case
was to locate and identify up to 20 QR-Codes on a picture taken by a
digital camera. As it turned out we couldn't find stable conditions that
would have made reliable detection possible for that: light (shadows),
unsteady photographer's hands and so on, it just did not work out.
I finally decided to use regular 2D-Barcodes and a cheap Bluetooth-USB
device to scan them [manually one by one], and my client is very happy
with that. BTW the codes are still created with Zxing ;-)


Good luck & best regards,

-hannes

Hannes Erven

unread,
Apr 14, 2014, 3:23:35 PM4/14/14
to Ernst-August Stehr, zx...@googlegroups.com

Lachezar Dobrev

unread,
Apr 15, 2014, 4:58:43 AM4/15/14
to Ernst-August Stehr, Hannes Erven, zxing
I have implemented a similar requirement for the application we're
working on. It deals with multi-page documents, by finding a QR code
on every page, and attaching it to the appropriate object in the data
base.
For reading the PDF I opted for PDFBox for its ability to extract
images from the document (I do not print/render the document), and the
relatively easy way to split a PDF into multiple PDFs page by page.

With the attached Neu2QR.pdf my code successfully found all the 5 QR
codes in 5 pages, and failed to find any in the other 4 pages.

Admittedly my code is obnoxiously slow (about 5 seconds per page) as
it performs various combinations of rescaling and blur to help work
around scanning and detection issues, but that's something I found out
was very needed to increase the success rate.

For instance the QR code on page 8 was found by processing image 3
in the page, after applying a blur with 5x5 kernel, but the QR code on
page 6 was found after down-scaling image 3 2x2 and applying a 3x3
blur kernel. And both scaling and blurring images is CPU intensive
when dealing with pages that have multi-mega-pixel images in them
(scanned pages).

The code I made is not publishable, but it is not something terribly
hard to come by. Examples exist on the Internet for extracting images
from PDFs using PDFBox. Examples exist on the Internet on how to
Blur/Rescale images using Java Image Processing. And examples for
reading QR Codes with BufferedImageLuminanceSource exist in ZXing's
documentation.

BTW. The attached PDF is weird. It has page pieces stacked on top of
each other, and some images are inverse, but that might be PDF Box
specific.
> --
> You received this message because you are subscribed to the Google Groups "zxing" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to zxing+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages