I want to read the barcode and print it in a console in a simple java program as soon as the barcode scans on top of any barcode. Is there any way?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答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");
}