开发者社区> 问答> 正文

Java 二维码开发包 zxing 使用教程:报错

有一个开源的 Java 类库叫做 “zxing” (Zebra Crossing),可以用来读写不同类型的条形码,包括二维码。

我测试了 zxing,它可以读取一个嵌在 100 dpi 的灰度文档中的条形码!
这篇文章讲述了如何使用 zxing 来读写条形码。
QR Code
UPC-A

PDF417 code

1. 下载 zxing 类库

你可以在这里下载 zxing 的源代码。你需要用下载的源代码来编译出两个jar,core.jar 和 javase.jar。

2. 在你的项目中引入 zxing 类库

你需要把 core.jar 和 javase.jar 添加到你项目的 classpath 中,然后你可以调用 zxing 的 API 了。下面的例子讲述了一个通用的方法来读写各种条形码,以及如何用编码/解码暗示来增强条形码引擎。

3. 读取条形码

你首先以输入流的形式载入图形,然后调用这个工具方法。

/**
  * Decode method used to read image or barcode itself, and recognize the barcode,
  * get the encoded contents and returns it.
  * @param file image that need to be read.
  * @param config configuration used when reading the barcode.
  * @return decoded results from barcode.
  */
 public static String decode(File file, Map<DecodeHintType, Object> hints) throws Exception {
     // check the required parameters
     if (file == null || file.getName().trim().isEmpty())
         throw new IllegalArgumentException("File not found, or invalid file name.");
     BufferedImage image;
     try {
         image = read(file);
     } catch (IOException ioe) {
         throw new Exception(ioe.getMessage());
     }
     if (image == null)
         throw new IllegalArgumentException("Could not decode image.");
     LuminanceSource source = new BufferedImageLuminanceSource(image);
     BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
     MultiFormatReader barcodeReader = new MultiFormatReader();
     Result result;
     String finalResult;
     try {
         if (hints != null && ! hints.isEmpty())
             result = barcodeReader.decode(bitmap, hints);
         else
             result = barcodeReader.decode(bitmap);
         // setting results.
         finalResult = String.valueOf(result.getText());
     } catch (Exception e) {
         e.printStackTrace();
         throw new BarcodeEngine().new BarcodeEngineException(e.getMessage());
     }
     return finalResult;
}

4. 写入条形码

你可以把一段文本写入条形码:

/**
  * Encode method, used to generate barcode with contents and
  * configurations provided.
  * @param file the image.
  * @param contents the contents to be encoded.
  * @param barcodeFormat the required barcode formate.
  * @param width image width.
  * @param height image height.
  * @param config any required configurations.
  */
 public static void encode(File file, String contents, BarcodeFormat barcodeFormat, int width,
                           int height, Map<EncodeHintType, Object> hints) throws Exception {
     // set default values
     if (barcodeFormat == null)
         barcodeFormat = DEFAULT_BARCODE_FORMAT;
     if (width <= 9)
         width = 300;
     if (height <= 9)
         height = 300;
     // check the required parameters
     if (file == null || file.getName().trim().isEmpty())
         throw new IllegalArgumentException("File not found, or invalid file name.");
     if (contents == null || contents.trim().isEmpty())
         throw new IllegalArgumentException("Can't encode null or empty contents.");
     try {
         MultiFormatWriter barcodeWriter = new MultiFormatWriter();
         BitMatrix matrix;
         if (config != null && ! hints.isEmpty())
             matrix =
                     barcodeWriter.encode(contents, barcodeFormat, width, height, hints);
         else
             matrix = barcodeWriter.encode(contents, barcodeFormat, width, height);
         String imageFormat = file.getName().substring(file.getName().indexOf(".") + 1);
         writeToFile(matrix, imageFormat, file);
     } catch (Exception e) {
         throw new Exception(e.getMessage());
     }
}

5. JavaDoc 和帮助文档

Javadoc 就在你下载的 zip 文件中。你可以通过 javadoc 查看所有被支持的条形码类型。


原文链接OSChina.NET 编译


展开
收起
kun坤 2020-06-07 16:40:36 993 0
1 条回答
写回答
取消 提交回答
  • 请问里面的参数是什么个情况File file, Map<DecodeHintType, Object> hints

    ######这个可以参考官方文档啊~######我按照你说的方法编译了一下,但是没有你上面说的方法呀######你好,请问开发板上怎么做二维码识别?开发板未定,也不确定是外接摄像头还是自带######这demo写得真蛋疼啊######额, 可以试试看这个 java barcode
    ######

    这里还有 qr code in java

    2020-06-07 16:40:41
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载