12

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. 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.

EDIT

Barcode scanner is USB one. If we scan something it will output the result into the textbox which has focus. But I have no textbox working on the page opened. Can i work with some hidden textbox and read the value there ?

Deepak
  • 6,684
  • 18
  • 69
  • 121
  • I'm not sure what you're asking (down-vote not from me). – Moonbeam Jul 18 '11 at 14:23
  • Definately requires much more details. Is barcode scanner a part of your application or third-party stand-alone aplication? What do you need textbox field for? How is/should/can barcode scanner communicate with your application? – bezmax Jul 18 '11 at 14:27
  • Usually barcode scanners are plain HID devices which work like keyboard - send keystrokes to whatever window has the focus. You can try capture the keystrokes within your application. – pingw33n Jul 18 '11 at 15:11
  • yes i could get the keystrokes but how do i inform the application tat a keystroke has started to happen before i transfer the focus to a text field ?\ – Deepak Jul 18 '11 at 15:16
  • what's dataType (image, byte, stream...) is possible catch on scanner output, API or driver must saying something about that – mKorbel Jul 18 '11 at 15:18
  • the output is something like this ?20003; – Deepak Jul 18 '11 at 15:22
  • @Deepak You don't need to inform application about that, this is a work for GUI system. In your scenario at some point the application realizes it's in need for barcode, it popups a window prompting the user to do the scanning while capturing any keystroke it receives optionally outputting them inside the window. The scan is done when the user presses OK and/or application receives valid barcode (enough numbers). – pingw33n Jul 18 '11 at 15:32
  • but how do i trigger an event like popup when the scanning is done. In other words the application doesn know anything until the scanning is done. so how to handle this problem ? – Deepak Jul 18 '11 at 15:35
  • @Deepak so the application doesn't know when to wait for scanning? In this case it can accept any numeric keystroke while measuring time between them - barcode scanner usually sends keystrokes fast enough with the same rate. This will allow the application to roughly distinguish (accident) keystrokes by user from keystrokes by scanner. This is the easy way. Or if the scanner have some SDK or you fill like writing driver for it you could communicate directly (this is the hard way). – pingw33n Jul 18 '11 at 15:44
  • ` In this case it can accept any numeric keystroke while measuring time between them` i dont get u properly. I am sorry can u explain me ? – Deepak Jul 18 '11 at 15:53
  • @Deepak The application has to somehow recognize sequences of keystrokes as input from the scanner. Because keystrokes can be triggered by user with regular keyboard I'm suggesting the way how to distinguish scanner input from user input. The scanner sends keystrokes fast and at the same rate (I guess), so if the application receives keystrokes fast enough and at a stable rate it can be almost sure they're coming from the scanner. – pingw33n Jul 18 '11 at 16:09
  • so how do i receive the keystrokes withhout having a text field focused ? – Deepak Jul 18 '11 at 16:35
  • @Deepak I never worked with Swing, but you can ask this as new question. – pingw33n Jul 18 '11 at 20:55

3 Answers3

9

Since barcode scanner is just a device which sends keycodes and ENTER after reading of each barcode, I'd use a key listener.

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

});
Jan Vorcak
  • 19,261
  • 14
  • 54
  • 90
  • 3
    The problem with this one is that the user could just press some keys with his keyboard and then press ENTER, and the application would take that as a captured barcode. Furthermore not every barcode reader sends a ENTER suffix char – Jaime Hablutzel May 15 '14 at 02:38
7

Since was not able to get input via frame.addKeyListener I have used this utility class which uses KeyboardFocusManager :

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

}
Jaydeep Karena
  • 159
  • 1
  • 5
  • 14
Cyrusmith
  • 747
  • 1
  • 9
  • 17
  • 1
    Very good solution, but there are some barcode readers that doesn't send ENTER key suffix by default, and with your code I can see no way to capture a barcode if ENTER is not sent at the end – Jaime Hablutzel May 15 '14 at 02:40
1

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:

  • It support barcode readers that doesn't send the ENTER at the end of barcode string.
  • 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.

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

Community
  • 1
  • 1
Jaime Hablutzel
  • 6,117
  • 5
  • 40
  • 57