开发者

How do I parse magnetic strip card data input in Java Swing textbox?

开发者 https://www.devze.com 2023-04-07 14:35 出处:网络
I\'m trying to write an application which will search and retrieve a user\'s profile using data using an id key found using a scanned barcode or embedded in a magnetic strip card\'s data.The latter on

I'm trying to write an application which will search and retrieve a user's profile using data using an id key found using a scanned barcode or embedded in a magnetic strip card's data. The latter one is cause me grief. The magstripe data needs to be parsed prior to searching for the user profile.

My question, is there a way to capture the text scanned into the textbox and parse it before it gets displayed in the textbox?

My reader/scanner is the keyboard emulation type so its as if each character encoded on th开发者_如何学编程e stripe is typed out in the textbox. I guess a solution (but is it the best?) would be to intercept each keystroke (emulated by the magnetic stripe reader), store them in a buffer and display an empty character until the end of the read string. Once the end of card's data is read, I could parse and display the id part of id. Problem is... how do you know it's the end of the card's data string if they get inputted as individual char keystroke?


Answer to your first question is to set a DocumentFilter on the Document of the textfield.

..how do you know it's the end of the card's data string if they get inputted as individual char keystroke?

Your MSR would surely emit a START_STRING and an END_OF_LINE_STRING as a combination of some predefined characters. Read the data specification of the MSR device. Once you have that, you could implement the insertString of the filter similar to this pseudo code

if str == START_CHARACTER
    then clear buffer

if str == EOL_CHARACTER
    then parse and do super.insertString

else
    append string to buffer

Again, the parse logic can be implemented using the data specification of the MSR.

(MSR = Magnetic Stripe Reader)


You should be able to use a Document Filter to intercept the text as it is added to the Document of the JTextField. Then when you receive the end of string character you can parse the text and insert it into the text field.

0

精彩评论

暂无评论...
验证码 换一张
取 消