用Java处理条形码扫描 [英] Handle barcode scan in Java

查看:1814
本文介绍了用Java处理条形码扫描的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的应用程序对正在扫描的条形码作出反应以触发按钮按下。例如,用户可以扫描((PRINT))条形码以激活打印按钮。

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.

条形码将被视为用户输入了一些文本。我不确定扫描仪是否会设置为附加一个输入或一个标签或者最后没有任何内容,所以我不想在此基础上做出假设。

The barcodes will just be treated as if the user had entered some text. I am not sure if the scanners will be set up to append an enter or a tab or nothing on the end, so I don't want to make an assumption on that basis.

该应用程序是一个Java / Swing应用程序。

The application is a Java/Swing application.

我查看了键绑定/动作地图等,但它们似乎专注于键和弦/单关键条目。我需要它才能在输入整个字符串之前不触发绑定。

I have looked at keybindings/action maps and the like, but they seem to be focussed on key chords/single key entries. I need it to not trigger the binding until the whole string is entered.

棘手的一点是,这应该适用于用户在屏幕上聚焦的任何地方。它们通常不会输入(字符,所以某种触发器可能会起作用。我不知道如何处理其余字符串。

The tricky bit is that this should work wherever the user is focussed in the screen. They will not normally enter ( characters, so some kind of trigger on that might work. I am not sure how to handle the rest of the string though.

编辑:道歉如果在问题中不清楚,但条形码扫描仪对应用程序来说不是什么特殊,它就像另一个键盘。所以用户不会输入(打印),但实际上这就是条码扫描器将会这样做,如果这是有意义的。

Apologies if it wasn't clear in the question, but the barcode scanner isn't anything "special" to the application, it's just like another keyboard. So the user won't be typing in (print), but effectively that is what the barcode scanner will be doing, if that makes sense.

因此只有两种方法可以触发打印:按下按钮或键入字符串(打印)棘手的部分是用户可以专注于应用程序的任何位置。我只担心应用程序是否具有整体焦点,而不是用户关注的是哪个字段。特定屏幕具有检查按钮和左/右选择器,因此用户不一定要输入字段。

So there are only two ways to trigger the print: pressing the button, or "typing" the string (print). The tricky part is that the user can be focussed anywhere on the application. I am only worried about if the application has focus as a whole, not which field the user is focussed on. The particular screen in question has checkbuttons and left/right selectors on it, so the user isn't necessarily going to be typing in to a field.

推荐答案

我遇到了问题像你的,并创建一个项目(目前有一些问题的概念验证),使摆动条形码处理更容易。

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.

项目位置: https://github.com/hablutzel1/swingbarcodelistener

演示用法:

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.

这篇关于用Java处理条形码扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆