条形码扫描仪扫描时,条形码在java中打印

时间:2013-03-12 16:13:54

标签: java barcode

一旦条形码扫描到任何条形码上,我想阅读条形码并在简单的java程序中将其打印在控制台中。有什么办法吗?

1 个答案:

答案 0 :(得分:-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");
    }