keymasterviriya1150

QRCODE

Sep 19th, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. package com.javapapers.java;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9.  
  10. import javax.imageio.ImageIO;
  11.  
  12. import com.google.zxing.BarcodeFormat;
  13. import com.google.zxing.BinaryBitmap;
  14. import com.google.zxing.EncodeHintType;
  15. import com.google.zxing.MultiFormatReader;
  16. import com.google.zxing.MultiFormatWriter;
  17. import com.google.zxing.NotFoundException;
  18. import com.google.zxing.Result;
  19. import com.google.zxing.WriterException;
  20. import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
  21. import com.google.zxing.client.j2se.MatrixToImageWriter;
  22. import com.google.zxing.common.BitMatrix;
  23. import com.google.zxing.common.HybridBinarizer;
  24. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
  25.  
  26. public class QRCode {
  27.  
  28.     public static void main(String[] args) throws WriterException, IOException,
  29.             NotFoundException {
  30.         String qrCodeData = "Hello World!";
  31.         String filePath = "QRCode.png";
  32.         String charset = "UTF-8"; // or "ISO-8859-1"
  33.         Map<EncodeHintType, ErrorCorrectionLevel> hintMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>();
  34.         hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
  35.  
  36.         createQRCode(qrCodeData, filePath, charset, hintMap, 200, 200);
  37.         System.out.println("QR Code image created successfully!");
  38.  
  39.         System.out.println("Data read from QR Code: "
  40.                 + readQRCode(filePath, charset, hintMap));
  41.  
  42.     }
  43.  
  44.     public static void createQRCode(String qrCodeData, String filePath,
  45.             String charset, Map hintMap, int qrCodeheight, int qrCodewidth)
  46.             throws WriterException, IOException {
  47.         BitMatrix matrix = new MultiFormatWriter().encode(
  48.                 new String(qrCodeData.getBytes(charset), charset),
  49.                 BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight, hintMap);
  50.         MatrixToImageWriter.writeToFile(matrix, filePath.substring(filePath
  51.                 .lastIndexOf('.') + 1), new File(filePath));
  52.     }
  53.  
  54.     public static String readQRCode(String filePath, String charset, Map hintMap)
  55.             throws FileNotFoundException, IOException, NotFoundException {
  56.         BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
  57.                 new BufferedImageLuminanceSource(
  58.                         ImageIO.read(new FileInputStream(filePath)))));
  59.         Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap,
  60.                 hintMap);
  61.         return qrCodeResult.getText();
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment