qrcode zxing java_在Java中使用Zxing读取QRCode

关于使用Zxing的一些问题…

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

public class BarCodeDecode

{

/**

* @param args

*/

public static void main(String[] args)

{

try

{

String tmpImgFile = "D:\\FormCode128.TIF";

Map tmpHintsMap = new EnumMap(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 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.

任何人都知道为什么…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值