【问题标题】:How to read barcode from handheld barcode scanner using zxing in Java如何使用 Java 中的 zxing 从手持条码扫描仪读取条码
【发布时间】:2013-10-13 01:31:41
【问题描述】:

我在 java 中使用名为“zxing”(斑马线)的开源 Java 库。我的代码在这里

package eg.com.taman.bc.tut;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.google.zxing.qrcode.decoder.Mode;
import eg.com.tm.barcode.processor.BarcodeEngine;
import eg.com.tm.barcode.processor.config.DecodeConfig;
import eg.com.tm.barcode.processor.config.EncodeConfig;
import java.io.File;
import java.util.Map;


public class BarcodeApplication {

   public static void main(String[] args) {

      // File will be used for creating the QRCode barcode type.
      File qrCodeFile = new File("C:\\barcode\\QRCode.png");


      // Building the encoding configurations - using builder battern
      EncodeConfig encodeConfig =
              new EncodeConfig.Builder().createDirectories(Boolean.TRUE)
              .isQRCodeFormat(Boolean.TRUE)
              .withErrorCorrLevel(ErrorCorrectionLevel.M).build();

      // Generating the QRCode barcode

      String content = "This is the contents of the barcode. 7654321 (QRCode)";

      BarcodeEngine.encode(qrCodeFile, content, BarcodeFormat.QR_CODE, 200, 200, encodeConfig);

      encodeConfig =
              new EncodeConfig.Builder().createDirectories(Boolean.TRUE).
              withCharactersMode(Mode.ALPHANUMERIC).build();




      System.out.println("------------------- Begins Writing barcodes -------------------\n");
      System.out.println("Is QRCode Created? " + (qrCodeFile.exists() ? "Yes " : "Not not ") + "Created");
      System.out.println("\n------------------- Finished Writing barcodes -------------------");

      // Now we are going to decode (read) back contents of created barcodes

      // Building the decoding configurations - using builder battern
      DecodeConfig decodeConfig =
              new DecodeConfig.Builder()
              .withHumanBarcodes(Boolean.TRUE)
              .build();


      Map<BarcodeEngine.DecodeResults, Object> results = BarcodeEngine.decode(qrCodeFile, decodeConfig);

      String decodeText = (String) results.get(BarcodeEngine.DecodeResults.RESULT);
      String barcodeType = ((BarcodeFormat) results.get(BarcodeEngine.DecodeResults.BARCODE_FORMATE)).name();

      System.out.println("\n------------------- Begins reading barcodes -------------------\n");
      System.out.println("The decoded contents is: \"" + decodeText + "\", Barcode type is: " + barcodeType);



      System.out.println("The decoded contents is: \"" + decodeText + "\", Barcode type is: " + barcodeType);

      System.out.println("\n------------------- Finished reading barcodes -------------------");
      System.out.println("decode Text : "+decodeText);
      System.out.println("barcode Type : "+barcodeType);
   }
}

代码将 Qr 条码读取为图像文件。现在我想使用手持条码扫描仪来读取条码。有什么帮助吗??????

我正在使用 Java 桌面应用程序而不是 Android。

【问题讨论】:

  • 我已经附上了我的代码,所以如果有什么不清楚的地方请告诉我。
  • 那么问题是“手持条码扫描器是如何读取条码的?”是这样吗?
  • 您能否解释一下您发布的代码与问题的相关性?在我看来,这与使用扫描仪读取二维码无关。

标签: java barcode zxing barcode-scanner barcode-printing


【解决方案1】:

听起来您正在寻找支持 HID 模式的扫描仪。

您的选择是Bluetooth scanner with HID mode 或 USB 扫描仪(大多数行为类似于键盘)。

一旦您选择了一个,HID 模式在所有扫描仪上基本相同,您可以在 Stackoverflow 上找到许多关于捕获扫描仪输入并将其与普通键盘上的用户输入分开的问题。

【讨论】:

    【解决方案2】:

    我的理解是 zxing 用于生成和处理二维码 images ...就像您在代码中所做的那样。它不是驱动条形码扫描仪的 API。如果您想要其中之一,则需要说明您尝试使用的设备。

    【讨论】:

    • 我不知道该买哪种设备,但我需要一个像键盘一样的编程设备。
    • 在您决定购买/使用哪一个之前,我们无法建议您如何对其进行编程。你将需要自己研究这个。关于选择硬件的建议是题外话。
    • 为什么设备很重要?鉴于整个事情都是关于图像的,那么我们只需要知道生成图像的位置。如果它不写入文件系统,则必须以其他方式传递已知格式的图像。您能否举一个示例设备来说明需要了解的设备?
    • @eigenfield 好吧,我们甚至不知道整件事是否与图像有关,对吗?很多条码扫描器只是充当键盘。
    【解决方案3】:

    我已经做了一些工作,在大多数情况下它都运行良好。 我的代码是:

        InputStream barCodeInputStream = new FileInputStream("test.png");
        BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream);
        LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Reader reader = new MultiFormatReader();
        Result result = reader.decode(bitmap);
        System.out.println("Barcode text is " + result.getText());
    

    这在二维码和条形码上都可以正常工作:)

    【讨论】:

    • 是的,在 maven 项目中尝试,这样 lib 将自动下载。
    猜你喜欢
    • 2014-12-11
    • 2021-09-24
    • 2014-03-05
    • 2017-09-30
    • 2020-12-03
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多