title: Decoding QR Code embedded into PDF with ZXing on Java backend
body: <p>In a Java backend server application I want to decode a QR code embedded into a PDF file using the zxing library.
  I adapted the example from <a href="https://gist.github.com/JoelGeraci-Datalogics/dd9e214d4c584d61f5b1" rel="nofollow noreferrer">https://gist.github.com/JoelGeraci-Datalogics/dd9e214d4c584d61f5b1</a> to work with the pdfbox  library as follows:</p>
  <p><strong>ReadBarcodeFromPDF.java</strong></p>
  <pre><code>import com.google.zxing.*;
  import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
  import com.google.zxing.common.HybridBinarizer;
  
  import java.awt.image.BufferedImage;
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.net.URLConnection;
  import java.util.Arrays;
  import java.util.Hashtable;
  import java.util.List;
  
  import org.apache.pdfbox.pdmodel.PDDocument;
  import org.apache.pdfbox.rendering.ImageType;
  import org.apache.pdfbox.rendering.PDFRenderer;
  
  public class ReadBarcodeFromPDF {
  
      private static final List&lt;String&gt; urlList = Arrays.asList(
              &quot;http://dev.datalogics.com/cookbook/forms/ReadBarcodeImage_QRCode.pdf&quot;,
              &quot;http://dev.datalogics.com/cookbook/forms/ReadBarcodeImage_DataMatrix.pdf&quot;,
              &quot;http://dev.datalogics.com/cookbook/forms/ReadBarcodeImage_PDF417.pdf&quot;
      );
  
      public static void main(String[] args) throws Exception {
          for (String url : urlList) {
              readCode(url);
          }
      }
  
      private static String readCode(String url) throws MalformedURLException, IOException, NotFoundException {
          URLConnection connection = new URL(url).openConnection();
          connection.connect();
  
          try (InputStream inputStream = connection.getInputStream()) {
              try (PDDocument document = PDDocument.load(inputStream)) {
                  PDFRenderer pdfRenderer = new PDFRenderer(document);
                  BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(0, 300, ImageType.BINARY);
                  LuminanceSource luminanceSource = new BufferedImageLuminanceSource(bufferedImage);
                  HybridBinarizer hybridBinarizer = new HybridBinarizer(luminanceSource);
                  BinaryBitmap bitmap = new BinaryBitmap(hybridBinarizer);
                  MultiFormatReader reader = new MultiFormatReader();
                  Hashtable&lt;DecodeHintType, Object&gt; hints = new Hashtable&lt;&gt;();
                  hints.put(DecodeHintType.POSSIBLE_FORMATS, Arrays.asList(
                          BarcodeFormat.QR_CODE,
                          BarcodeFormat.DATA_MATRIX,
                          BarcodeFormat.PDF_417
                  ));
                  hints.put(DecodeHintType.PURE_BARCODE, Boolean.FALSE);
                  hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
                  reader.setHints(hints);
                  Result result = reader.decodeWithState(bitmap);
                  return result.getText();
              }
          }
      }
  }
  </code></pre>
  <p>The example is supposed to recognize codes from PDF documents with different code types:</p>
  <ol>
  <li>QR Code: <a href="http://dev.datalogics.com/cookbook/forms/ReadBarcodeImage_QRCode.pdf" rel="nofollow noreferrer">http://dev.datalogics.com/cookbook/forms/ReadBarcodeImage_QRCode.pdf</a></li>
  <li>Data Matrix: <a href="http://dev.datalogics.com/cookbook/forms/ReadBarcodeImage_DataMatrix.pdf" rel="nofollow noreferrer">http://dev.datalogics.com/cookbook/forms/ReadBarcodeImage_DataMatrix.pdf</a></li>
  <li>PDF417: <a href="http://dev.datalogics.com/cookbook/forms/ReadBarcodeImage_PDF417.pdf" rel="nofollow noreferrer">http://dev.datalogics.com/cookbook/forms/ReadBarcodeImage_PDF417.pdf</a></li>
  </ol>
  <p>But it fact it recognizes only the PDF417. The QR Code (in which I am interested) and the data matrix (in which I am not interested) are not recognized.</p>
  <p><strong>The output is</strong></p>
  <pre><code>com.google.zxing.NotFoundException
  com.google.zxing.NotFoundException
  11/15/2010  Tony    Blue
  </code></pre>
  <p><strong>I also tried</strong></p>
  <p>Different arguments in line</p>
  <pre><code>pdfRenderer.renderImageWithDPI(0, 300, ImageType.BINARY);
  </code></pre>
  <ul>
  <li>Different values for <code>dpi</code> (2nd argument) between <code>100</code> and <code>300</code></li>
  <li>All available values for <code>imageType</code> (3rd argument)</li>
  <li>Newest version of the library (3.5.0)</li>
  </ul>
  <p><strong>pom.xml</strong></p>
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;org.apache.pdfbox&lt;/groupId&gt;
          &lt;artifactId&gt;pdfbox&lt;/artifactId&gt;
          &lt;version&gt;2.0.26&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;com.google.zxing&lt;/groupId&gt;
          &lt;artifactId&gt;javase&lt;/artifactId&gt;
          &lt;version&gt;3.3.3&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>