加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

在Java中使用Zxing读取QRCode

发布时间:2020-12-14 16:45:19 所属栏目:Java 来源:网络整理
导读:关于使用Zxing的一些问题… 我写下面的代码从图像中读取条形码: public class BarCodeDecode { /** * @param args */ public static void main(String[] args) { try { String tmpImgFile = "D:FormCode128.TIF"; MapDecodeHintType,Object tmpHintsMap =
关于使用Zxing的一些问题…

我写下面的代码从图像中读取条形码:

public class BarCodeDecode 
{
    /**
     * @param args
     */
    public static void main(String[] args) 
    {
        try
        {
            String tmpImgFile = "D:FormCode128.TIF";

            Map<DecodeHintType,Object> tmpHintsMap = new EnumMap<DecodeHintType,Object>(DecodeHintType.class);
            tmpHintsMap.put(DecodeHintType.TRY_HARDER,Boolean.TRUE);
            tmpHintsMap.put(DecodeHintType.POSSIBLE_FORMATS,EnumSet.allOf(BarcodeFormat.class));
            tmpHintsMap.put(DecodeHintType.PURE_BARCODE,Boolean.FALSE);

            File tmpFile = new File(tmpImgFile);
            String tmpRetString = BarCodeUtil.decode(tmpFile,tmpHintsMap);
            //String tmpRetString = BarCodeUtil.decode(tmpFile,null);
            System.out.println(tmpRetString);
        }
        catch (Exception tmpExpt)
        {
            System.out.println("main: " + "Excpt err! (" + tmpExpt.getMessage() + ")");
        }
        System.out.println("main: " + "Program end.");
    }

}

public class BarCodeUtil 
{
    private static BarcodeFormat DEFAULT_BARCODE_FORMAT = BarcodeFormat.CODE_128;

    /**
      * Decode method used to read image or barcode itself,and recognize the barcode,* get the encoded contents and returns it.
      * @param whatFile image that need to be read.
      * @param config configuration used when reading the barcode.
      * @return decoded results from barcode.
      */
     public static String decode(File whatFile,Map<DecodeHintType,Object> whatHints) throws Exception 
     {
         // check the required parameters
         if (whatFile == null || whatFile.getName().trim().isEmpty())
             throw new IllegalArgumentException("File not found,or invalid file name.");
         BufferedImage tmpBfrImage;
         try 
         {
             tmpBfrImage = ImageIO.read(whatFile);
         } 
         catch (IOException tmpIoe) 
         {
             throw new Exception(tmpIoe.getMessage());
         }
         if (tmpBfrImage == null)
             throw new IllegalArgumentException("Could not decode image.");
         LuminanceSource tmpSource = new BufferedImageLuminanceSource(tmpBfrImage);
         BinaryBitmap tmpBitmap = new BinaryBitmap(new HybridBinarizer(tmpSource));
         MultiFormatReader tmpBarcodeReader = new MultiFormatReader();
         Result tmpResult;
         String tmpFinalResult;
         try 
         {
             if (whatHints != null && ! whatHints.isEmpty())
                 tmpResult = tmpBarcodeReader.decode(tmpBitmap,whatHints);
             else
                 tmpResult = tmpBarcodeReader.decode(tmpBitmap);
             // setting results.
             tmpFinalResult = String.valueOf(tmpResult.getText());
         } 
         catch (Exception tmpExcpt) 
         {
             throw new Exception("BarCodeUtil.decode Excpt err - " + tmpExcpt.toString() + " - " + tmpExcpt.getMessage());
         }
         return tmpFinalResult;
    }
}

我尝试读取包含code128和QRCode的以下两个图像.

它可以适用于code128但不适用于QRCode.
任何人都知道为什么…

解决方法

好奇你的代码对我有用,但是我不得不删除跟随提示.

tmpHintsMap.put(DecodeHintType.PURE_BARCODE,Boolean.FALSE);

当我的图像不是纯条形码时,这个提示打破了我的结果.

谢谢!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读