import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.EnumMap; import java.util.EnumSet; import java.util.HashMap; import java.util.Hashtable; import java.util.List; import java.util.Map; import java.util.Vector; import javax.imageio.ImageIO; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.ImageType; import org.apache.pdfbox.rendering.PDFRenderer; import com.google.zxing.qrcode.QRCodeReader; import com.google.zxing.BarcodeFormat; import com.google.zxing.BinaryBitmap; import com.google.zxing.DecodeHintType; import com.google.zxing.EncodeHintType; import com.google.zxing.LuminanceSource; import com.google.zxing.MultiFormatReader; import com.google.zxing.MultiFormatWriter; import com.google.zxing.NotFoundException; import com.google.zxing.Result; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import com.ns.support.QrPdf; public class My20190205QrCodeTest { private static String path = "C:\\NS2\\test_files\\520044839973-ARV.pdf";// public static void main(String[] args) { My20190205QrCodeTest a = new My20190205QrCodeTest(); try { a.testPdf(); } catch (Exception e) { e.printStackTrace(); } System.out.println("---------------- END ------------------"); } private void testPdf() { try { String line = scanQRCode(1); System.out.println("---------------- test7 result:::" + line); } catch (Exception e) { e.printStackTrace(); } } private String scanQRCode(int pageIndex) { // Hints for scanning Vector<BarcodeFormat> decodeFormat = new Vector<BarcodeFormat>(); decodeFormat.add(BarcodeFormat.QR_CODE); Hashtable<DecodeHintType, Object> hintMap = new Hashtable<DecodeHintType, Object>(); hintMap.put(DecodeHintType.TRY_HARDER, true); hintMap.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormat); MultiFormatReader qrcodeReader = new MultiFormatReader(); qrcodeReader.setHints(hintMap); try { // Try lowest DPI first. BufferedImage pageImage = getPageImage(); LuminanceSource source = new BufferedImageLuminanceSource(pageImage); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); // By using decodeWithState, we keep the Hints that we set earlier. Result result = qrcodeReader.decodeWithState(bitmap); String text = result.getText(); // System.out.println(" -- text:::" + text); return text; } catch (IOException e) { e.printStackTrace(); } catch (NotFoundException e) { e.printStackTrace(); } // This should never happen, ever... return null; } private BufferedImage getPageImage() throws IOException { Path docPath = Paths.get(path); PDDocument pdfDoc = PDDocument.load(docPath.toFile()); PDFRenderer renderer = new PDFRenderer(pdfDoc); // renderImageWithDPI(page number, image size, format) BufferedImage image = renderer.renderImageWithDPI(0, 150, ImageType.BINARY); // entire page info // BufferedImage image = renderer.renderImage(0); pdfDoc.close(); return image; } }