Java 类com.google.zxing.client.j2se.BufferedImageLuminanceSource 实例源码

项目:IJPay    文件:ZxingKit.java   
/**
 * @param srcImgFilePath
 *            要解码的图片地址
 * @return {Result}
 */
@SuppressWarnings("finally")
public static Result decode(String srcImgFilePath) {
    Result result = null;
    BufferedImage image;
    try {
        File srcFile = new File(srcImgFilePath);
        image = ImageIO.read(srcFile);
        if (null != image) {
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

            Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
            result = new MultiFormatReader().decode(bitmap, hints);
        } else {
            throw new IllegalArgumentException ("Could not decode image.");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        return result;
    }
}
项目:framework    文件:BarCodeUtils.java   
/**
 * 条形码解码
 */
public static String decode(BufferedImage image) {
    Result result = null;
    try {
        if (image == null) {
            System.out.println("the decode image may be not exit.");
        }
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

        result = new MultiFormatReader().decode(bitmap, null);
        return result.getText();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
项目:framework    文件:QRCodeUtils.java   
/**
 * 解析二维码
 *
 * @param file 二维码图片
 * @return
 * @throws Exception
 */
public static String decode(File file) throws Exception {
    BufferedImage image;
    image = ImageIO.read(file);
    if (image == null) {
        return null;
    }
    BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    Result result;
    Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
    hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
    result = new MultiFormatReader().decode(bitmap, hints);
    String resultStr = result.getText();
    return resultStr;
}
项目:iBase4J-Common    文件:QrcodeUtil.java   
public static String decodeQr(String filePath) {
    String retStr = "";
    if ("".equalsIgnoreCase(filePath) && filePath.length() == 0) {
        return "图片路径为空!";
    }
    try {
        BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filePath));
        LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
        Binarizer binarizer = new HybridBinarizer(source);
        BinaryBitmap bitmap = new BinaryBitmap(binarizer);
        HashMap<DecodeHintType, Object> hintTypeObjectHashMap = new HashMap<>();
        hintTypeObjectHashMap.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        Result result = new MultiFormatReader().decode(bitmap, hintTypeObjectHashMap);
        retStr = result.getText();
    } catch (Exception e) {
        logger.error("", e);
    }
    return retStr;
}
项目:automat    文件:QrcodeUtil.java   
public static String decodeQr(String filePath) {
    String retStr = "";
    if ("".equalsIgnoreCase(filePath) && filePath.length() == 0) {
        return "图片路径为空!";
    }
    try {
        BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filePath));
        LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
        Binarizer binarizer = new HybridBinarizer(source);
        BinaryBitmap bitmap = new BinaryBitmap(binarizer);
        HashMap<DecodeHintType, Object> hintTypeObjectHashMap = new HashMap<>();
        hintTypeObjectHashMap.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        Result result = new MultiFormatReader().decode(bitmap, hintTypeObjectHashMap);
        retStr = result.getText();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return retStr;
}
项目:Fetax-AI    文件:CodeUtil.java   
/**
* 解析条形码
*/
  public static String decodes(String imgPath) {
      BufferedImage image = null;
      Result result = null;
      try {
          image = ImageIO.read(new File(imgPath));
          if (image == null) {
              System.out.println("the decode image may be not exit.");
          }
          LuminanceSource source = new BufferedImageLuminanceSource(image);
          BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

          result = new MultiFormatReader().decode(bitmap, null);
          return result.getText();
      } catch (Exception e) {
          e.printStackTrace();
      }
      return null;
  }
项目:JAVA-    文件:QrcodeUtil.java   
public static String decodeQr(String filePath) {
    String retStr = "";
    if ("".equalsIgnoreCase(filePath) && filePath.length() == 0) {
        return "图片路径为空!";
    }
    try {
        BufferedImage bufferedImage = ImageIO.read(new FileInputStream(filePath));
        LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
        Binarizer binarizer = new HybridBinarizer(source);
        BinaryBitmap bitmap = new BinaryBitmap(binarizer);
        HashMap<DecodeHintType, Object> hintTypeObjectHashMap = new HashMap<>();
        hintTypeObjectHashMap.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        Result result = new MultiFormatReader().decode(bitmap, hintTypeObjectHashMap);
        retStr = result.getText();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return retStr;
}
项目:angit    文件:ZxingHelper.java   
/**
 * 条形码解码
 *
 * @param imgPath 文件路径
 * @return String string
 */
public static String decode(String imgPath) {
    BufferedImage image;
    Result result;
    try {
        image = ImageIO.read(new File(imgPath));
        if (image == null) {
            LOGGER.error("the decode image may be not exit.");
            return null;
        }
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

        result = new MultiFormatReader().decode(bitmap, null);
        return result.getText();
    } catch (Exception e) {
        LOGGER.error("条形码解析错误", e);
    }
    return null;
}
项目:angit    文件:ZxingHelper.java   
/**
 * 二维码解码
 *
 * @param imgPath 文件路径
 * @return String string
 */
public static String decode2(String imgPath) {
    BufferedImage image;
    Result result;
    try {
        image = ImageIO.read(new File(imgPath));
        if (image == null) {
            LOGGER.error("the decode image may be not exit.");
            return null;
        }
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

        EnumMap<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class);
        hints.put(DecodeHintType.CHARACTER_SET, "GBK");

        result = new MultiFormatReader().decode(bitmap, hints);
        return result.getText();
    } catch (Exception e) {
        LOGGER.error("二维码解码错误", e);
    }
    return null;
}
项目:awe-awesomesky    文件:CodeUtil.java   
/**
* 解析条形码
*/
  public static String decodes(String imgPath) {
      BufferedImage image = null;
      Result result = null;
      try {
          image = ImageIO.read(new File(imgPath));
          if (image == null) {
              System.out.println("the decode image may be not exit.");
          }
          LuminanceSource source = new BufferedImageLuminanceSource(image);
          BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

          result = new MultiFormatReader().decode(bitmap, null);
          return result.getText();
      } catch (Exception e) {
          e.printStackTrace();
      }
      return null;
  }
项目:simbest-cores    文件:QrCodeUtil.java   
/**
 * 读取二维码
 * @param qrCodeFile
 * @return
 */
public String readQrCode(File qrCodeFile){
    String ret = null;
    try {           
        QRCodeReader reader = new QRCodeReader();
        BufferedImage image = ImageIO.read(qrCodeFile);
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        Binarizer binarizer = new HybridBinarizer(source);
        BinaryBitmap imageBinaryBitmap = new BinaryBitmap(binarizer);
        Result result = reader.decode(imageBinaryBitmap);
        ret = result.getText();
    } catch (IOException |NotFoundException | ChecksumException | FormatException e) {
        Exceptions.printException(e);
    }
    return ret;     
}
项目:Shop-for-JavaWeb    文件:ZxingHandler.java   
/**
 * 条形码解码
 * 
 * @param imgPath
 * @return String
 */
public static String decode(String imgPath) {
    BufferedImage image = null;
    Result result = null;
    try {
        image = ImageIO.read(new File(imgPath));
        if (image == null) {
            System.out.println("the decode image may be not exit.");
        }
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

        result = new MultiFormatReader().decode(bitmap, null);
        return result.getText();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
项目:Shop-for-JavaWeb    文件:ZxingHandler.java   
/**
 * 二维码解码
 * 
 * @param imgPath
 * @return String
 */
public static String decode2(String imgPath) {
    BufferedImage image = null;
    Result result = null;
    try {
        image = ImageIO.read(new File(imgPath));
        if (image == null) {
            System.out.println("the decode image may be not exit.");
        }
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

        Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
        hints.put(DecodeHintType.CHARACTER_SET, "GBK");

        result = new MultiFormatReader().decode(bitmap, hints);
        return result.getText();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
项目:document-management-system    文件:BarcodeTextExtractor.java   
/**
 * Decode all barcodes in the image
 */
private String multiple(BufferedImage img) throws NotFoundException, ChecksumException, FormatException {
    long begin = System.currentTimeMillis();
    LuminanceSource source = new BufferedImageLuminanceSource(img);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    com.google.zxing.Reader reader = new MultiFormatReader();
    MultipleBarcodeReader bcReader = new GenericMultipleBarcodeReader(reader);
    Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
    hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
    StringBuilder sb = new StringBuilder();

    for (Result result : bcReader.decodeMultiple(bitmap, hints)) {
        sb.append(result.getText()).append(" ");
    }

    SystemProfiling.log(null, System.currentTimeMillis() - begin);
    log.trace("multiple.Time: {}", System.currentTimeMillis() - begin);
    return sb.toString();
}
项目:jfinal-plus    文件:ZxingKit.java   
/**
 * @param srcImgFilePath 要解码的图片地址
 * @return
 */
public Result decode(String srcImgFilePath) {
    Result result = null;
    BufferedImage image;
    try {
        File srcFile = new File(srcImgFilePath);
        image = ImageIO.read(srcFile);
        if (null != image) {
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

            Hashtable hints = new Hashtable();
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
            result = new MultiFormatReader().decode(bitmap, hints);
        } else {
            log.debug("Could not decode image.");
        }
    } catch (Throwable t) {
        log.error("decode-ex:{}", t);
        throw Throwables.propagate(t);
    } finally {
        return result;
    }
}
项目:qr-code-reader    文件:QRDecoder.java   
/**
 * Decodes a QR code from a BufferedImage object.
 * 
 * @param image
 * @return a Result object containing the decoded data or information about
 *         an unsuccessful decoding attempt
 * @throws Exception
 */
private Result decode(BufferedImage image) throws Exception {

    // create a luminance source from the BufferedImage
    LuminanceSource lumSource = new BufferedImageLuminanceSource(image);
    // create a binary bitmap from the luminance source. a Binarizer
    // converts luminance data to 1 bit data.
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(lumSource));

    // a reader for decoding
    QRCodeReader reader = new QRCodeReader();

    // attempt decoding and return result
    Hashtable<DecodeHintType, Boolean> hints = new Hashtable<DecodeHintType, Boolean>();
    hints.put(DecodeHintType.TRY_HARDER, true);
    return reader.decode(bitmap, hints);
}
项目:bither-desktop-java    文件:QRCodeEncoderDecoder.java   
public static String decode(BufferedImage image) {

        // convert the image to a binary bitmap source
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

        // decode the barcode
        QRCodeReader reader = new QRCodeReader();

        try {
            @SuppressWarnings("rawtypes")
            Hashtable hints = new Hashtable();
            Result result = reader.decode(bitmap, hints);


            return result.getText();
        } catch (ReaderException e) {
            // the data is improperly formatted
        }

        return "";
    }
项目:EasyVote    文件:VotingQRCode.java   
/**
 * Versucht aus dem �bergeben BufferedImage ein QR Code zu finden und �bersetzt dieses in einen String.
 * 
 * @param qrcodeImage : BufferedImage 
 * @return String mit dem Inhalt des QRCodes
 * @throws Exception 
 */
private static String readQRCode(BufferedImage qrcodeImage) throws Exception{
    //Die Parameter anlegen
    Hashtable<DecodeHintType, Object> hintMap = new Hashtable<DecodeHintType, Object>();
       hintMap.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);

    //Bild zu BinaryBitmap verwandeln
    BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(qrcodeImage);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

    //QR Leser initialisieren...
    QRCodeReader reader = new QRCodeReader();
    Result result;
    //...und lesen:
    result = reader.decode(bitmap,hintMap);

    return result.getText();
}
项目:common_utils    文件:QRCode.java   
public String decode(BufferedImage image) throws NotFoundException {
    /*判断是否是图片*/
    if (image == null) {
        System.out.println("Could not decode image");
    }
        /*解析二维码用到的辅助类*/
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
        /*解码设置编码方式为:UTF-8*/
    hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");

    Result result = new MultiFormatReader().decode(bitmap, hints);
    String resultStr = result.getText();
    return resultStr;
}
项目:wowdoge.org    文件:DialogImportPrivate.java   
private void scanQR(BufferedImage i) throws ReaderException, ChecksumException, FormatException {
    Image img = i.getScaledInstance(200, -1, Image.SCALE_SMOOTH);
    // Create a buffered image with transparency
    i = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);

    // Draw the image on to the buffered image
    Graphics2D bGr = i.createGraphics();
    bGr.drawImage(img, 0, 0, null);
    bGr.dispose();

    btnDragOrPaste.setIcon(new ImageIcon(i));
    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(i)));
    //Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(2);
       //hints.put(EncodeHintType.CHARACTER_SET, "ISO-8859-1");
    //Vector decodeFormats = new Vector<BarcodeFormat>();
    //decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS);
    //Hashtable hints = new Hashtable<DecodeHintType, Object>(3);
    //hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
    Result result = new QRCodeReader().decode(binaryBitmap);//, hints);
    //System.out.println("QR Code : "+result.getText());
    textField.setText(result.getText());
    //System.out.println( s );
}
项目:wowdoge.org    文件:ButtonQRScan.java   
private void scanQR(BufferedImage i) throws ReaderException, ChecksumException, FormatException {
    Image img = i.getScaledInstance(200, -1, Image.SCALE_SMOOTH);
    // Create a buffered image with transparency
    i = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);

    // Draw the image on to the buffered image
    Graphics2D bGr = i.createGraphics();
    bGr.drawImage(img, 0, 0, null);
    bGr.dispose();

    setIcon(new ImageIcon(i));
    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(i)));
    //Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(2);
       //hints.put(EncodeHintType.CHARACTER_SET, "ISO-8859-1");
    //Vector decodeFormats = new Vector<BarcodeFormat>();
    //decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS);
    //Hashtable hints = new Hashtable<DecodeHintType, Object>(3);
    //hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
    Result result = new QRCodeReader().decode(binaryBitmap);//, hints);
    //System.out.println("QR Code : "+result.getText());
    scannedQRText = result.getText();
    fireEvent();
    //System.out.println( s );
}
项目:bither-desktop-java    文件:QRCodeEncoderDecoder.java   
public static String decode(BufferedImage image) {

        // convert the image to a binary bitmap source
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

        // decode the barcode
        QRCodeReader reader = new QRCodeReader();

        try {
            @SuppressWarnings("rawtypes")
            Hashtable hints = new Hashtable();
            Result result = reader.decode(bitmap, hints);


            return result.getText();
        } catch (ReaderException e) {
            // the data is improperly formatted
        }

        return "";
    }
项目:StreamingQR    文件:ReceiveTest.java   
/**
 * Try decoding standard QR code without any of the
 * sequence information inserted into payload.
 */
@Test
public void testDecodeQrWithNoSequenceInfo() {
  // Decode qr code received from transmitter as screenshot
  String filename = "fooScreenshot_noReservedBits.png";
  String expectedText   = "foo";

  BufferedImage b = getImageResourceAndCheckNotNull(filename);
  LuminanceSource lumSrc = new BufferedImageLuminanceSource(b);

  assertNotNull("Unable to convert BufferedImage to LuminanceSrc", lumSrc);
  try {
    Result result = Receive.decodeSingle(lumSrc);
    assertEquals("Expect decoded result to match expected", expectedText, result.getText());
  } catch (NotFoundException e) {
    fail("Unable to find QR in image, "+filename + ". " + e.getMessage());
  }
}
项目:sparkbit    文件:QRCodeEncoderDecoder.java   
public String decode(BufferedImage image) {

        // convert the image to a binary bitmap source
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

        // decode the barcode
        QRCodeReader reader = new QRCodeReader();

        try {
            @SuppressWarnings("rawtypes")
            Hashtable hints = new Hashtable();
            Result result = reader.decode(bitmap, hints);
            log.info("Decoded image successfully, result was : '" + result.getText() + "'");

            return result.getText();
        } catch (ReaderException e) {
            // the data is improperly formatted
            log.debug(e.getMessage());
            log.error("Error while decoding image", e);
        }

        return "";
    }
项目:JavaToolKit    文件:ReadQRCode.java   
private static String readQrcode(BufferedImage image){
    MultiFormatReader formatReader = new MultiFormatReader();
    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
    HashMap hints = new HashMap();//创建属性
    hints.put(EncodeHintType.AZTEC_LAYERS, "utf-8");//设置编码
    Result result = null;
    try {
        result = formatReader.decode(binaryBitmap,hints);
    } catch (NotFoundException e) {
        e.printStackTrace();
    }
    System.out.println("结果 "+result.toString());
    return result.toString();
}
项目:digital-display-garden-iteration-2-spraguesanborn    文件:QRCodeMaker.java   
public static String readQRCode(String filePath, String charset, Map hintMap)
        throws FileNotFoundException, IOException, NotFoundException {
    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
            new BufferedImageLuminanceSource(
                    ImageIO.read(new FileInputStream(filePath)))));
    Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap,
            hintMap);
    return qrCodeResult.getText();
}
项目:jeeves    文件:QRCodeUtils.java   
public static String decode(InputStream input)
        throws IOException, NotFoundException {
    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
            new BufferedImageLuminanceSource(
                    ImageIO.read(input))));
    Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap);
    return qrCodeResult.getText();
}
项目:tephra    文件:QrCodeImpl.java   
@Override
public String read(InputStream inputStream) {
    try {
        String string = reader.decode(new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(inputStream))))).getText();
        inputStream.close();

        return string;
    } catch (Throwable e) {
        logger.warn(e, "读取二维码图片内容时发生异常!");

        return null;
    }
}
项目:demo    文件:QRGeneratorTest.java   
private static void parse() throws IOException, NotFoundException, ChecksumException, FormatException {
    BufferedImage image = ImageReader.readImage(Paths.get("d:/qr.png").toUri());
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    Binarizer bin = new HybridBinarizer(source);
    BinaryBitmap bitmap = new BinaryBitmap(bin);
    Result result = new QRCodeReader().decode(bitmap);
    System.out.println(result.toString());
}
项目:spring-boot    文件:MyQRCodeUtils.java   
/**
 * 解码 QRCode 图片,解析出其内容
 *
 * @param imageURI QRCode 图片 URI
 * @return 解析后的内容
 * @throws IOException
 */
public static String decodeQRCodeImage(URI imageURI) throws IOException {

    BufferedImage bufferedImage = ImageReader.readImage(imageURI);
    LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    QRCodeReader reader = new QRCodeReader();
    try {
        Result result = reader.decode(bitmap);
        return result.getText();
    } catch (ReaderException e) {
        e.printStackTrace();
    }
    return "";
}
项目:document-management-system    文件:BarcodeTextExtractor.java   
/**
 * Decode only one barcode
 */
@SuppressWarnings("unused")
private String simple(BufferedImage img) throws NotFoundException, ChecksumException, FormatException {
    long begin = System.currentTimeMillis();
    LuminanceSource source = new BufferedImageLuminanceSource(img);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    com.google.zxing.Reader reader = new MultiFormatReader();
    Result result = reader.decode(bitmap);

    SystemProfiling.log(null, System.currentTimeMillis() - begin);
    log.trace("simple.Time: {}", System.currentTimeMillis() - begin);
    return result.getText();
}
项目:Camel    文件:BarcodeDataFormat.java   
/**
 * Reads the message from a code.
 */
private String readImage(final Exchange exchange, final InputStream stream) throws Exception {
    final MultiFormatReader reader = new MultiFormatReader();
    final BufferedInputStream in = exchange.getContext()
            .getTypeConverter()
            .mandatoryConvertTo(BufferedInputStream.class, stream);
    final BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(in))));
    final Result result = reader.decode(bitmap, readerHintMap);

    // write the found barcode format into the header
    exchange.getOut().setHeader(Barcode.BARCODE_FORMAT, result.getBarcodeFormat());

    return result.getText();
}
项目:Camel    文件:BarcodeTestBase.java   
private void checkFormat(File file, BarcodeFormat format) throws IOException {
    Reader reader = new MultiFormatReader();
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(file))));
    Result result;
    try {
        result = reader.decode(bitmap);
    } catch (ReaderException ex) {
        throw new IOException(ex);
    }

    assertEquals(format, result.getBarcodeFormat());
}
项目:jeewx-api    文件:QRCode.java   
/**
 * 解析QRCode二维码
 */
@SuppressWarnings("unchecked")
public static void decode(File file) {
    try {
        BufferedImage image;
        try {
            image = ImageIO.read(file);
            if (image == null) {
                System.out.println("Could not decode image");
            }
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
            Result result;
            @SuppressWarnings("rawtypes")
            Hashtable hints = new Hashtable();
            //解码设置编码方式为:utf-8
            hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
            result = new MultiFormatReader().decode(bitmap, hints);
            String resultStr = result.getText();
            System.out.println("解析后内容:" + resultStr);
        } catch (IOException ioe) {
            System.out.println(ioe.toString());
        } catch (ReaderException re) {
            System.out.println(re.toString());
        }
    } catch (Exception ex) {
        System.out.println(ex.toString());
    }
}
项目:PanBox    文件:QRCodeProcessor.java   
@Override
public IplImage processImage(IplImage in) {
    LuminanceSource source = new BufferedImageLuminanceSource(
            in.getBufferedImage());
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    Result result;
    try {
        result = new MultiFormatReader().decode(bitmap);
        lastResult = result.getText();
        lastFoundTime = System.currentTimeMillis();
        fire(result.getText());
    } catch (NotFoundException e) {
        // that's ok
        if (!lastResult.equals("")) // if result was not empty, clear old
                                    // result
        {
            long mt1 = System.currentTimeMillis();
            if (mt1 - lastFoundTime > CLEAR_RESULT_TIMEOUT) {
                lastFoundTime = mt1;
                fire("");
                lastResult = "";
            }
        }
    }

    return in;
}
项目:oath    文件:TestQRCodeWriter.java   
private static String getQRCodeImageRawText(Path path) throws IOException, NotFoundException {
    Map<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
    hints.put(DecodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.name());
    try(FileInputStream fis = new FileInputStream(path.toFile())) {
        BufferedImage bi = ImageIO.read(fis);
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bi)));
        Result result = new MultiFormatReader().decode(binaryBitmap, hints);
        return result.getText();
    }
}
项目:arong    文件:QrCodeUtil.java   
/**
 * 加载二维码图片得到Result对象
 * @param bufferedImage 二维码图片
 * @return
 */
public static Result loadResult(BufferedImage bufferedImage){
    LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
    hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
    Result result = null;
    try {
        result = new MultiFormatReader().decode(bitmap, hints);
    } catch (NotFoundException e) {
        e.printStackTrace();
    }
    return result;
}
项目:StreamingQR    文件:ReceiveTest.java   
@Test
public void testEncodeThenDecode() throws IOException {
  Transmit t = new Transmit(350,350);
  byte[] inputBytes = "foo".getBytes(Charsets.ISO_8859_1);

  // Generate some QR codes from input string
  Iterable<BitmapImage> qrCodes = null;
  try {
    qrCodes = t.encodeQRCodes(inputBytes);
  } catch (TransmitException e) {
    fail("Encoding failed "+ e.getMessage());
  }
  Iterator<BitmapImage> iter = qrCodes.iterator();
  assertTrue("QR encoding failed to return at least one element", iter.hasNext());
  BitmapImage encodedQRImage = iter.next();

  // Convert them to images so we can run QR decoder on them
  BufferedImage b = UtilsTest.toBufferedImage(encodedQRImage);
  LuminanceSource lumSrc = new BufferedImageLuminanceSource(b);
  Result result = decodeAndCheckValidQR(lumSrc, null);
  PartialMessage m = PartialMessage.createFromResult(result, Integer.MAX_VALUE);

  // Expect this small input will generate and decode a single QR code.
  assertNotNull("Expected QR code to be formatted for QRLib", m);
  assertEquals("Should only have 1 chunk" , 1, m.getTotalChunks());
  assertEquals("Unexpected chunkId" , 1, m.getChunkId());
  assertArrayEquals("Original input does not match decoded result",
      inputBytes,m.getPayload());
}
项目:StreamingQR    文件:ReceiveTest.java   
/**
 * Opens a test file in resources directory and converts it to ZXing's
 * LuminanceSource image type. Causes unit tests to fail if conversion fails.
 *
 * @param filename The image file that will be converted.
 */
private LuminanceSource getLuminanceImgAndCheckNotNull(String filename) {
  BufferedImage b = getImageResourceAndCheckNotNull(filename);
  LuminanceSource lumSrc = new BufferedImageLuminanceSource(b);
  assertNotNull("Unable to convert BufferedImage to LuminanceSrc", lumSrc);
  return lumSrc;
}
项目:StreamingQR    文件:UtilsTest.java   
/**
 * Convert from Java's BufferedImage type to ZXing's BitMatrix type.
 * Returns null when there is no QR code found in the image.
 *
 * @param img The BufferedImage to convert to BitMatrix
 * @return The BitMatrix of the QR code found in img
 */
private static BitMatrix toBitMatrix (BufferedImage img){
  BufferedImageLuminanceSource lumSrc = new BufferedImageLuminanceSource(img);
  HybridBinarizer hb = new HybridBinarizer(lumSrc);
  try {
    return hb.getBlackMatrix();
  } catch (NotFoundException e) {
    // Ok to ignore, returning null when QR code not found.
    // PMD complained about empty catch block
    return null;
  }
}