package com.pennant.qrcode.reader; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.Serializable; import java.util.Hashtable; import javax.imageio.ImageIO; import javax.imageio.stream.ImageInputStream; import org.apache.commons.codec.binary.Base64; import com.google.zxing.BarcodeFormat; import com.google.zxing.BinaryBitmap; import com.google.zxing.DecodeHintType; import com.google.zxing.LuminanceSource; import com.google.zxing.NotFoundException; import com.google.zxing.Reader; import com.google.zxing.Result; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.multi.qrcode.QRCodeMultiReader; import com.google.zxing.qrcode.QRCodeReader; public class QRCodeReaderTest implements Serializable { private static final long serialVersionUID = 1293284494250774077L; /** * Method for Reading Pdf File BarCode(QRCode) * * @param inputStream * @param fileType * @return * @throws NotFoundException * @throws IOException */ public String getQRBarcode(String encodedString) throws NotFoundException, IOException { // Get Base64 and convert to inputstream ByteArrayInputStream rawInputStream = new ByteArrayInputStream(Base64.decodeBase64(encodedString)); // Convert inputstream to image input stream ImageInputStream imageInput = ImageIO.createImageInputStream(rawInputStream); // Read buffered image from image input stream BufferedImage bimage = ImageIO.read(imageInput); // Get the QR Code from the image as string return parseQR(bimage); } /** * Method for Parse Quick Reference Code From Buffered image * * @param image * @return * @throws Exception */ private String parseQR(BufferedImage image) throws NotFoundException { String text = ""; try { LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap( new com.google.zxing.common.HybridBinarizer(source)); Hashtable hints = new Hashtable(); hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE); hints.put(DecodeHintType.TRY_HARDER, BarcodeFormat.QR_CODE); QRCodeMultiReader multiReader = new QRCodeMultiReader(); Result[] results = multiReader.decodeMultiple(bitmap, hints); for (int k = 0; k < results.length; k++) { text = text + results[k].getText(); } } catch (NotFoundException e) { System.err.println(e.getMessage()); e.printStackTrace(); } if (text != null && text.length() > 0) { //text = text.replace("|", "~"); //String[] listQrCode = text.split("~"); String[] listQrCode = text.split("|"); //System.out.println("listQrCode.length" + listQrCode.length); if (listQrCode.length > 0) { String pdfReqType = ""; pdfReqType = listQrCode[0]; //System.out.println("pdfReqType" + pdfReqType); /* for (int i = 0; i < listQrCode.length; i++) { System.out.println(listQrCode[i]); } */ } } return text; } }