java使用Zxing读取图片的条码

第一步:导入pom

		
			com.google.zxing
			javase
			3.3.3
		

第二步:

	/**
	 * @param imgPath
	 * @return String
	 */
	public static String decode(String imgPath) {
		try {
			BufferedImage image = ImageIO.read(new File(imgPath));
			if (image == null) {
				return "b";
			}
			LuminanceSource source = new BufferedImageLuminanceSource(image);
			BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

			Map hints = new HashMap<>();
			hints.put(DecodeHintType.CHARACTER_SET, "GBK");
			hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
			hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);

			Result result = new MultiFormatReader().decode(bitmap, hints);
			return result.getText();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "a";
	}

 

你可能感兴趣的:(JAVA)