简体   繁体   English

如何在Java中使用Zxing从手持式条形码扫描仪读取条形码

[英]How to read barcode from handheld barcode scanner using zxing in Java

I am using the open source Java library called ' zxing ' (Zebra Crossing) In java. 我正在使用Java中称为' zxing '(斑马线)的开源Java库。 My code is here 我的代码在这里

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);
   }
}

The code reads a Qr barcode as image file. 该代码读取Qr条形码作为图像文件。 Now i want to use the handheld barcode scanner to read the barcode. 现在,我想使用手持式条形码扫描仪读取条形码。 Any help ????? 有帮助吗?????

I am working in java desktop application Not Android. 我正在Java桌面应用程序中工作,而不是Android。

Sounds like you are looking for a scanner that support HID mode. 听起来您正在寻找支持HID模式的扫描仪。

Your options are a Bluetooth scanner with HID mode or a USB scanner (most act like keyboards). 您可以选择具有HID模式蓝牙扫描仪或USB扫描仪(最像键盘)。

Once you've picked one, HID mode is basically the same on all scanners and you can find many questions on Stackoverflow about capturing scanner input and separating it from user input on a normal keyboard. 选择一个后,所有扫描仪的HID模式基本相同,您可以在Stackoverflow上找到许多有关捕获扫描仪输入并将其与普通键盘上的用户输入分开的问题。

My understanding is that zxing is for generating and processing QR code images ... as you are doing in your code. 我的理解是zxing用于生成和处理QR代码图像 ……就像您在代码中所做的一样。 It is not an API for driving a barcode scanner. 它不是用于驱动条形码扫描仪的API。 If you want one of those, you will need to say something about the device you are trying to use. 如果您要使用其中之一,则需要对要使用的设备说几句话。

I have worked on it a little bit, and in most of cases it's working fine. 我做了一些工作,在大多数情况下,它都可以正常工作。 My code is: 我的代码是:

    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());

This will work fine on QR as well as Barcode :) 这将在QR以及条形码上正常工作:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM