Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. public static String readQRCode2(String fileName) {
  2.         File file = new File(fileName);
  3.         BufferedImage image = null;
  4.         BinaryBitmap bitmap = null;
  5.         Result result = null;
  6.  
  7.         try {
  8.             image = ImageIO.read(file);
  9.             int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
  10.             RGBLuminanceSource source = new RGBLuminanceSource(image.getWidth(), image.getHeight(), pixels);
  11.             bitmap = new BinaryBitmap(new HybridBinarizer(source));
  12.         } catch (IOException e) {
  13.             e.printStackTrace();
  14.         }
  15.  
  16.         if (bitmap == null) {
  17.             return null;
  18.         }
  19.  
  20.         QRCodeReader reader = new QRCodeReader();
  21.         try {
  22.             Map<DecodeHintType, Object> hintMap = new Hashtable<DecodeHintType, Object>();
  23.             hintMap.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
  24.             hintMap.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.allOf(BarcodeFormat.class));
  25.             result = reader.decode(bitmap, hintMap);
  26.             log.info("Decoded image successfully, result was : '" + result.getText() + "'");
  27.             return result.getText();
  28.         } catch (NotFoundException e) {
  29.             e.printStackTrace();
  30.         } catch (ChecksumException e) {
  31.             e.printStackTrace();
  32.         } catch (FormatException e) {
  33.             e.printStackTrace();
  34.         }
  35.  
  36.         return null;
  37.  
  38.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement