Getting input from barcode scanner internally without textbox

前端 未结 3 1959
时光说笑
时光说笑 2020-12-13 11:17

I have a barcode scanner and in my java application I have to bring a popup to display all the information associated with the barcode from database when the product is scan

相关标签:
3条回答
  • 2020-12-13 11:59

    In some way similar to @Cyrusmith solution I have created a 'proof of concept' solution (with several limitations right now, but you are invited to fix them :) ) trying to solve the limitations on the previous solutions in this post:

    • It support barcode readers that doesn't send the ENTER at the end of barcode string.
    • If the focus is currently on a swing text component and barcode is captured, the barcode doesn't get to the text component and only to the barcode listener.

    See https://stackoverflow.com/a/22084579/320594

    0 讨论(0)
  • 2020-12-13 12:09

    Since barcode scanner is just a device which sends keycodes and ENTER after reading of each barcode, I'd use a key listener.

    final Frame frame = new Frame();
    frame.setVisible(true);
    
    frame.addKeyListener(new KeyAdapter() {
    
        @Override
        public void keyReleased(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_ENTER) {
                // your code is scanned and you can access it using frame.getBarCode()
                // now clean the bar code so the next one can be read
                frame.setBarCode(new String());
            } else {
                // some character has been read, append it to your "barcode cache"
                frame.setBarCode(frame.getBarCode() + e.getKeyChar());
            }
        }
    
    });
    
    0 讨论(0)
  • 2020-12-13 12:09

    Since was not able to get input via frame.addKeyListener I have used this utility class which uses KeyboardFocusManager :

    public class BarcodeReader {
    
    private static final long THRESHOLD = 100;
    private static final int MIN_BARCODE_LENGTH = 8;
    
    public interface BarcodeListener {
    
        void onBarcodeRead(String barcode);
    }
    
    private final StringBuffer barcode = new StringBuffer();
    private final List<BarcodeListener> listeners = new CopyOnWriteArrayList<BarcodeListener>();
    private long lastEventTimeStamp = 0L;
    
    public BarcodeReader() {
    
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
            @Override
            public boolean dispatchKeyEvent(KeyEvent e) {
                if (e.getID() != KeyEvent.KEY_RELEASED) {
                    return false;
                }
    
                if (e.getWhen() - lastEventTimeStamp > THRESHOLD) {
                    barcode.delete(0, barcode.length());
                }
    
                lastEventTimeStamp = e.getWhen();
    
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    if (barcode.length() >= MIN_BARCODE_LENGTH) {
                        fireBarcode(barcode.toString());
                    }
                    barcode.delete(0, barcode.length());
                } else {
                    barcode.append(e.getKeyChar());
                }
                return false;
            }
        });
    
    }
    
    protected void fireBarcode(String barcode) {
        for (BarcodeListener listener : listeners) {
            listener.onBarcodeRead(barcode);
        }
    }
    
    public void addBarcodeListener(BarcodeListener listener) {
        listeners.add(listener);
    }
    
    public void removeBarcodeListener(BarcodeListener listener) {
        listeners.remove(listener);
    }
    
    }
    
    0 讨论(0)
提交回复
热议问题