简体   繁体   中英

Reading from a Barcode Scanner in Swing

I have installed a bar-code scanner from Datalogic, with the goal of reading the bar-code into a java textfield. However, when I scan the bar code in Swing, the resulting text is garbage. I cannot use this. On standalone java.awt.TextField is works fine, but when I integrate this into my code, it also produces garbage non-mappable characters.

Don't know if I'll need a specific driver for JAVA, I have tried converting the string from UTF-8 to ISO-88... to no avail.

Been looking at this since 2 days in-vain.

Any help will be greatly appreciated.

Thanks

-innocent

try resetting the scanner to remove all spurios characters/codes that might have been set up; ie according to the reference guide the scanner will send by default the barcode id for gs1-128 codes as an escape sequence, which might cause some trouble for swing

download the product reference guide from http://www.datalogic.com/eng/quickscan-i-lite-qw2100-pd-163.html

scan the barcode to enter programming mode

go to the relevant section and scan the codes to remove all preambles and to remove the aim label for all codes

you can also try the different types of keyboard emulation and codepage.

There is a problem with KeyEvents coming from a barcode scanner using ALT + NumPad method. Java generates KeyTyped events with random outputs when ALT key is pressed. The problem exists in the current versions of Java 7 and Java 8 JRE (I have tested it with JRE 7u67 and JRE 8u20 on Windows 8, Windows 7 and Ubuntu 14).

My solution is to register a KeyEventDispatcher that blocks the KeyEvents when the ALT method is active:

KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(
    new AltBugFixKeyEventDispatcher());

public class AltBugFixKeyEventDispatcher implements KeyEventDispatcher {

    private int i = -1;

    @Override
    public boolean dispatchKeyEvent(KeyEvent ke) {
        if (ke.isAltDown()) {
            switch (ke.getID()) {
                case KeyEvent.KEY_PRESSED:
                    if(ke.getKeyCode() == KeyEvent.VK_ALT){
                        i = 0;
                    }else if(Character.isDigit(ke.getKeyChar())){
                        i++;
                    }else{
                        i = -1;
                    }
                    break;
                case KeyEvent.KEY_RELEASED:
                    break;
                case KeyEvent.KEY_TYPED:
                    break;
            }
            if(i != -1){
                return true;
            }
        }
        return false;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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