Handle barcode scan in Java

前端 未结 4 646
长情又很酷
长情又很酷 2021-01-16 06:57

I want to have my application react to barcodes being scanned to trigger button presses. For example the user could scan the ((PRINT)) barcode to activate the print button.<

4条回答
  •  隐瞒了意图╮
    2021-01-16 07:44

    I had a problem just like yours, and created a project (currently proof of concept with some problems) to make barcode handling in swing easier.

    It is based in the fact that the barcode readers emulate a keyboard but differently to humans they "type" with a constant timing. It will basically allow you to listen to "barcode read" events.

    The project location: https://github.com/hablutzel1/swingbarcodelistener

    Demo usage:

    public class SimpleTest extends JFrame {
        public SimpleTest() throws HeadlessException {
    
            // start of listening for barcode events
            Toolkit.getDefaultToolkit().addAWTEventListener(new BarcodeAwareAWTEventListener(new BarcodeCapturedListener() {
                @Override
                public void barcodeCaptured(String barcode) {
                    JOptionPane.showMessageDialog(SimpleTest.this, "barcode captured: " + barcode);
                }
            }), AWTEvent.KEY_EVENT_MASK);
            // end of listening for barcode events
    
    
            getContentPane().setLayout(new FlowLayout());
            getContentPane().add(new JLabel("Capture barcode demo"));
            getContentPane().add(new JTextField(25));
        }
    
        public static void main(String[] args) {
            SimpleTest simpleTest = new SimpleTest();
            simpleTest.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            simpleTest.setVisible(true);
            simpleTest.pack();
        }
    }
    

    It has some problems now but as a starting point I think it is ok, if you have time to improve it it would be great.

提交回复
热议问题