QR code decoding by zxing

package com.ns.utils;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;

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.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class QRCodeZxingUtil {

	public static Map<String, String> scanQRCode(String dir, String fileName, String textKey) {
		Map<String, String> mp = new HashMap<String, String>();
		// 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);
		// We try for several images of the PDF page at several DPI settings,
		// starting at the lowest setting, this might help for speed...
		int[] dpiSettings = { 96, 150, 200, 250, 300, 350 };
		for (int i = 0; i < dpiSettings.length; i++) {
			try {
				// Try lowest DPI first.
				BufferedImage pageImage = getPageImage(dir, fileName, dpiSettings[i]);
				if (pageImage != null) {
					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(" Sucess at DPI -> " + dpiSettings[i]);
					pageImage.flush();

					if (text != null && text.length() > 0) {
						mp.put(textKey, text);
						mp.put("DPI", Integer.toString(dpiSettings[i]));
					}
				}
				if (mp.containsKey(textKey)) {
					return mp;
				}
			} catch (IOException e) {
				System.out.print(" IOException at DPI -> " + dpiSettings[i]);
			} catch (NotFoundException e) {
				System.out.print(" NotFoundException at DPI -> " + dpiSettings[i]);
			}
		}
		return null;
	}

	private static BufferedImage getPageImage(String dir, String fileName, int dpi) throws IOException {
		BufferedImage image = null;
		Path docPath = Paths.get(dir.concat(fileName));
		PDDocument pdfDoc = PDDocument.load(docPath.toFile());
		try {
			PDFRenderer renderer = new PDFRenderer(pdfDoc);
			image = renderer.renderImageWithDPI(0, dpi, ImageType.BINARY); // entire page info
		} finally {
			pdfDoc.close();
		}		
		return image;
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.