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?
解决方案
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");
}