简体   繁体   English

在没有文本框的情况下在内部从条形码扫描仪获取输入

[英]Getting input from barcode scanner internally without textbox

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 scanned using barcode.我有一个条形码扫描仪,在我的 java 应用程序中,当使用条形码扫描产品时,我必须弹出一个弹出窗口以显示与数据库中的条形码相关的所有信息。 I have no textbox on the application I have to handle this part internally.我必须在内部处理这部分的应用程序上没有文本框。 How do I do this?我该怎么做呢? any suggestion?有什么建议吗? I am using swing for UI.我正在为 UI 使用 swing。

EDIT编辑

Barcode scanner is USB one.条码扫描器是USB之一。 If we scan something it will output the result into the textbox which has focus.如果我们扫描某些东西,它将 output 将结果放入具有焦点的文本框中。 But I have no textbox working on the page opened.但是我没有在打开的页面上工作的文本框。 Can i work with some hidden textbox and read the value there?我可以使用一些隐藏的文本框并在那里读取值吗?

Since barcode scanner is just a device which sends keycodes and ENTER after reading of each barcode, I'd use a key listener.由于条形码扫描仪只是在读取每个条形码后发送键码和 ENTER 的设备,因此我会使用键监听器。

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());
        }
    }

});

Since was not able to get input via frame.addKeyListener I have used this utility class which uses KeyboardFocusManager:由于无法通过 frame.addKeyListener 获得输入,我使用了这个使用 KeyboardFocusManager 的实用程序 class:

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);
}

}

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:在某种程度上类似于@Cyrusmith 解决方案,我创建了一个“概念证明”解决方案(现在有几个限制,但请您修复它们:))试图解决这篇文章中先前解决方案的限制:

  • It support barcode readers that doesn't send the ENTER at the end of barcode string.它支持在条形码字符串末尾不发送 ENTER 的条形码阅读器。
  • 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.如果当前焦点位于 swing 文本组件上并且条码被捕获,则条码不会到达文本组件,而只会到达条码侦听器。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM