Barcode print in java while barcode scanner scans

2019-08-18 20:51发布

问题:

I want to read the barcode and print it in a console in a simple java program as soon as the barcode scans on top of any barcode. Is there any way?

回答1:

final Scanner scanner;

    try 
    {
        scanner = new Scanner();

        try {
            scanner.release();
            scanner.close();
        } catch (Exception e) {

        }

        scanner.open("MotorolaScannerUSB");

        scanner.claim(100);
        scanner.setDeviceEnabled(true);
        scanner.setDataEventEnabled(true);

        scanner.addDataListener(new DataListener() 
        {
            public void dataOccurred(DataEvent arg0) {
                Scanner scn = (Scanner) arg0.getSource();

                if (scn.equals(scanner)) {                      
                    try {
                        scanner.claim(100);
                        System.out.println(new String(scanner.getScanData()));
                        scanner.claim(100);
                        scanner.setDeviceEnabled(true);
                        scanner.setDataEventEnabled(true);
                    } catch (JposException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
    catch (Exception e) {
        System.err.println("error");
    }


标签: java barcode