条码扫描器在Java上的实现 [英] Barcode Scanner implementation on Java

查看:381
本文介绍了条码扫描器在Java上的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的先生,我有个问题。我目前正在开展的学校Java项目要求我将USB条形码扫描器作为外部输入连接到我的笔记本电脑。我没有真正买过USB扫描仪,因为学生很贵。所以我必须收集证据表明这个扫描仪将与我的程序一起工作。



扫描仪能够从条形码读取(大概在线打印)并将其存储到变量中吗?如果是这样,扫描仪按下的动作事件是否会像键盘按键一样读取?如果是这样,代码行是什么样的?



此外,如果您可以发布您的条形码扫描器的经验,或提供任何建议,如哪些扫描仪购买,这将有助于很多。干杯!

解决方案

我最近不得不实施扫描仪系统与java进行交互。



我使用 Honeywell Voyager MS9540 USB条形码扫描器。



作为默认值,扫描仪将数据直接发送为键盘输入 - 否驱动程序需要。



但是很容易让这个模型直接与java进行交互,而不是使用键盘钩(在java中使用条形码作为变量,如前所述)。



此型号具有模拟串行端口的设置,然后可以使用javax.comm包读取扫描的数据。对于我来说,这是比键盘钩子更好的获得条形码数据,因为程序不需要焦点才能解释扫描(我不想创建一个全局键盘钩子)



我的java程序读取指定串行端口的所有输入,并将条形码写入数据库。我还设置程序将任何无法识别的条形码扫描传递到键盘(我的应用程序没有创建的任何条形码 - 我在我的条形码中使用了一个不同的签名),因此它可以作为常规条形码扫描器适用于任何其他应用程序从键盘读取条形码。



您可以通过执行一些密集的JNDI编码直接从任何USB扫描仪读取数据(没有该模型的串行端口仿真),但是没有准备花时间解决本地代码。



要配置此特定型号的串行端口仿真,只需扫描 文档。它是条形码为串行仿真模式。



此扫描仪 需要驱动程序进行串行端口仿真。我发现实施说明和所需的驱动程序 这里 (在软件选项卡下)。下载Honeywell扫描和移动(HSM)USB串行驱动程序的软件包。标题为HSM USB串行驱动程序入门指南的PDF有说明。



如果您不熟悉javax.comm API。请阅读Rick Proctor的 示例中的简介 - 它会告诉您在哪里获取jar以及放置文件的位置(javax.comm不是大多数java包的标准)。



我确定有其他扫描仪型号有串口模拟(我不为霍尼韦尔工作)。



我的条形码阅读器课程有一些有些脱落的版本:

  package scanhandler; 


import java.awt.AWTException;
import java.awt.Robot;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Enumeration;
import java.util.Properties;
import java.util.TooManyListenersException;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;

public class ScanHandler实现Runnable,SerialPortEventListener {

private static CommPortIdentifier myCommPortIdentifier;
private static枚举portList;
private static String TimeStamp;
private static String driverClass;
private static String connectionString;
private static String comPort;

private Connection myConnection;
私人InputStream myInputStream;
私人机器人myRobot;
私有Seri​​alPort mySerialPort;
private Thread myThread;


public Sc​​anHandler(){

//打开串口
try {
TimeStamp = new java.util.Date()的ToString();
mySerialPort =(SerialPort)myCommPortIdentifier.open(ComControl,2000);
//System.out.println(TimeStamp +:+ myCommPortIdentifier.getName()+为扫描仪输入打开);
} catch(PortInUseException e){
e.printStackTrace();
}

//获取串行输入流
try {
myInputStream = mySerialPort.getInputStream();
} catch(IOException e){
e.printStackTrace();
}

//在端口上添加一个事件监听器
try {
mySerialPort.addEventListener(this);
} catch(TooManyListenersException e){
e.printStackTrace();
}
mySerialPort.notifyOnDataAvailable(true);

//设置串行端口属性
try {
mySerialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
mySerialPort.setDTR(false);
mySerialPort.setRTS(false);

} catch(UnsupportedCommOperationException e){
e.printStackTrace();
}

//使机器人传递键盘数据
try {
myRobot = new Robot();
} catch(AWTException e){
e.printStackTrace();
}

//创建线程
myThread = new Thread(this);
myThread.start();
}

public void run(){
try {
Thread.sleep(100);
} catch(InterruptedException e){}
}

//扫描
public void serialEvent(SerialPortEvent event){

if (event.getEventType()== SerialPortEvent.DATA_AVAILABLE){

StringBuilder myStringBuilder = new StringBuilder();
int c;
尝试{

//将扫描的数据附加到字符串构建器
while((c = myInputStream.read())!= 10){
if c!= 13)myStringBuilder.append((char)c);
}

//如果条形码不以'5'开头,则发送到键盘缓冲区
if(myStringBuilder.charAt(0)!='5'){ (int i = 0; i< myStringBuilder.length(); i ++){
myRobot.keyPress((int)myStringBuilder.charAt(i));


myRobot.keyRelease((int)myStringBuilder.charAt(i));
}

//这里将扫描的条形码作为变量!
} else {
TimeStamp = new java.util.Date()。toString();
System.out.println(TimeStamp +:scanning input received:+ myStringBuilder.toString());
}

//关闭输入流
myInputStream.close();

} catch(IOException e){
e.printStackTrace();
}
}
}

public static void main(String [] args){

//读取ScanHandler属性
属性myProperties = new Properties();
try {
myProperties.load(new FileInputStream(config.properties));
comPort = myProperties.getProperty(ScanHandler.comPort);
} catch(IOException e){
e.printStackTrace();
}

尝试{

//获取我们的预定义COM端口
myCommPortIdentifier = CommPortIdentifier.getPortIdentifier(comPort);
ScanHandler reader = new ScanHandler();

} catch(Exception e){
TimeStamp = new java.util.Date()。toString();
System.out.println(TimeStamp +:+ comPort ++ myCommPortIdentifier);
System.out.println(TimeStamp +:msg1 - + e);
}
};
}


Good sirs, I have a question. The school Java project I am currently working on requires me to have a USB Barcode Scanner as an external input to be connected to my laptop. I haven't actually bought the USB Scanner since it's quite expensive for a student. So I have to gather evidence that this Scanner would work with my program.

Would the Scanner be able to read from a barcode (presumably printed off online) and store it into a variable? If so, is it true that the action event for the press of the scanner would be read exactly like a keyboard keypress? If so, what would the line of code look like?

Also, if you could post your experiences with Barcode Scanners, or give any advice, such as which Scanner to buy, that would help alot. Cheers!

解决方案

I recently had to implement a scanner system to interact with java.

I used Honeywell Voyager MS9540 USB barcode scanner.

As a default the scanner sent the data straight as keyboard input - no driver required.

But it was very easy to get this model to interact directly with java rather than using a keyboard hook (to use the barcodes as variables in java, as you mentioned).

This model has a setting to emulate a serial port, you can then read the scanned data using the javax.comm package. For me, this was much better than a keyboard hook to get the barcode data because the program does not need the focus before being able to interpret a scan (I would not want to create a global keyboard hook).

My java program reads all input from the specified serial port and writes the barcode to a database. I also setup the program to pass any unrecognized barcode scans to the keyboard (any barcode that my app did not create - I used a distinct signature on my barcodes) this was so it would work as a regular barcode scanner for any other apps that might read barcodes from the keyboard.

You could probably read data directly from any USB scanner (without the serial port emulation that this model has) by doing some intensive JNDI coding but I wasn't prepared to take the time to work out the native code.

To configure this particular model for serial port emulation all you do is scan a specific barcode in this document with the scanner you want to configure. It is the barcode titled "Serial Emulation Mode".

This scanner does require a driver for serial port emulation. I found the implementation instructions and the needed drivers here (under the "software" tab). Download the package titled: "Honeywell Scanning and Mobility (HSM) USB Serial Driver". The PDF titled "HSM USB Serial Driver Getting Started Guide" had the instructions.

If you are not familiar with the javax.comm API. Please read the intro in this example by Rick Proctor - it tells you where to get the jar and where to put the files (javax.comm does not come standard with most java packages).

I'm sure there are other scanner models around that have serial port emulation (I don't work for Honeywell).

Here's a somewhat stripped down version of my barcode reader class:

package scanhandler;


import java.awt.AWTException;
import java.awt.Robot;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Enumeration;
import java.util.Properties;
import java.util.TooManyListenersException;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;

public class ScanHandler implements Runnable, SerialPortEventListener {

    private static CommPortIdentifier   myCommPortIdentifier;
    private static Enumeration          portList;
    private static String               TimeStamp;
    private static String               driverClass;
    private static String               connectionString;
    private static String               comPort;    

    private Connection                  myConnection;
    private InputStream                 myInputStream;
    private Robot                       myRobot;
    private SerialPort                  mySerialPort;
    private Thread                      myThread;


    public ScanHandler() {

        // open serial port
        try {
            TimeStamp = new java.util.Date().toString();
            mySerialPort = (SerialPort) myCommPortIdentifier.open("ComControl", 2000);
            //System.out.println(TimeStamp + ": " + myCommPortIdentifier.getName() + " opened for scanner input");
        } catch (PortInUseException e) {
            e.printStackTrace();
        }

        // get serial input stream
        try {
            myInputStream = mySerialPort.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // add an event listener on the port
        try {
            mySerialPort.addEventListener(this);
        } catch (TooManyListenersException e) {
            e.printStackTrace();
        }
        mySerialPort.notifyOnDataAvailable(true);

        // set up the serial port properties
        try {
            mySerialPort.setSerialPortParams(9600,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
            mySerialPort.setDTR(false);
            mySerialPort.setRTS(false);

        } catch (UnsupportedCommOperationException e) {
            e.printStackTrace();
        }

        // make a robot to pass keyboard data
        try {
            myRobot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }

        // create the thread
        myThread = new Thread(this);
        myThread.start();
    }

    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {}
    }

    // on scan
    public void serialEvent(SerialPortEvent event) {

        if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {

            StringBuilder myStringBuilder = new StringBuilder();
            int c;
            try {

                // append the scanned data onto a string builder
                while ((c = myInputStream.read()) != 10){
                   if (c != 13)  myStringBuilder.append((char) c);
                }               

                // send to keyboard buffer if it the barcode doesn't start with '5'
                if (myStringBuilder.charAt(0) != '5') {

                    for (int i = 0; i < myStringBuilder.length(); i++) {
                        myRobot.keyPress((int) myStringBuilder.charAt(i));
                        myRobot.keyRelease((int) myStringBuilder.charAt(i));
                    }

                // here's the scanned barcode as a variable!
                } else {
                    TimeStamp = new java.util.Date().toString();
                    System.out.println(TimeStamp + ": scanned input received:" + myStringBuilder.toString());                    
                }

                // close the input stream
                myInputStream.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {

        // read ScanHandler properties
        Properties myProperties = new Properties();
        try {
            myProperties.load(new FileInputStream("config.properties"));
            comPort             = myProperties.getProperty("ScanHandler.comPort");
        } catch (IOException e) {
            e.printStackTrace();
        }              

        try {

            // get our pre-defined COM port
            myCommPortIdentifier = CommPortIdentifier.getPortIdentifier(comPort);
            ScanHandler reader = new ScanHandler();

        } catch (Exception e) {
            TimeStamp = new java.util.Date().toString();
            System.out.println(TimeStamp + ": " + comPort + " " + myCommPortIdentifier);
            System.out.println(TimeStamp + ": msg1 - " + e);
        }
    };    
}

这篇关于条码扫描器在Java上的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆