近期有个需求,安卓设备外接扫码枪,系统读取扫码枪扫描的内容。 网上查了一些例子,拿过来用的时候都不太好使,最后没办法,自己实现了一个(目前只支持英文字母和英文符号)。 代码如下: 1
近期有个需求,安卓设备外接扫码枪,系统读取扫码枪扫描的内容。
网上查了一些例子,拿过来用的时候都不太好使,最后没办法,自己实现了一个(目前只支持英文字母和英文符号)。
代码如下:
1. 重写Activity的onKeyDown方法,收集扫码枪返回的keycode。
ArrayList<Integer> scannedCodes = new ArrayList<Integer>();
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode!= KeyEvent.KEYCODE_ENTER){ //扫码枪以回车为结束
scannedCodes.add(keyCode);
}else{ //结束
handleKeyCodes();
}
return super.onKeyDown(keyCode, event);
}
2. 处理接收倒的keycode,handleKeyCodes()方法。
private void handleKeyCodes(){
FnString fnString = new FnString();
String result = "";
boolean hasshift = false;
for(int keyCode : scannedCodes){
result += fnString.keyCodeToChar(keyCode, hasShift);
hasShift = (keyCode == KeyEvent.KEYCODE_SHIFT_LEFT);
}
Toast.makeText(context, result, Toast.LENGTH_LONG).show();
scannedCodes.clear();
}
3. keycode转换为字符的方法
public String keyCodeToChar(int code, boolean isShift){
switch(code){
case KeyEvent.KEYCODE_SHIFT_LEFT: return "";
//数字键10个 + 符号10个
case KeyEvent.KEYCODE_0: return isShift ? ")" : "0";
case KeyEvent.KEYCODE_1: return isShift ? "!" : "1";
case KeyEvent.KEYCODE_2: return isShift ? "@" : "2";
case KeyEvent.KEYCODE_3: return isShift ? "#" : "3";
case KeyEvent.KEYCODE_4: return isShift ? "$" : "4";
case KeyEvent.KEYCODE_5: return isShift ? "%" : "5";
case KeyEvent.KEYCODE_6: return isShift ? "^" : "6";
case KeyEvent.KEYCODE_7: return isShift ? "&" : "7";
case KeyEvent.KEYCODE_8: return isShift ? "*" : "8";
case KeyEvent.KEYCODE_9: return isShift ? "(" : "9";
//字母键26个小写 + 26个大写
case KeyEvent.KEYCODE_A: return isShift ? "A" : "a";
case KeyEvent.KEYCODE_B: return isShift ? "B" : "b";
case KeyEvent.KEYCODE_C: return isShift ? "C" : "c";
case KeyEvent.KEYCODE_D: return isShift ? "D" : "d";
case KeyEvent.KEYCODE_E: return isShift ? "E" : "e";
case KeyEvent.KEYCODE_F: return isShift ? "F" : "f";
case KeyEvent.KEYCODE_G: return isShift ? "G" : "g";
case KeyEvent.KEYCODE_H: return isShift ? "H" : "h";
case KeyEvent.KEYCODE_I: return isShift ? "I" : "i";
case KeyEvent.KEYCODE_J: return isShift ? "J" : "j";
case KeyEvent.KEYCODE_K: return isShift ? "K" : "k";
case KeyEvent.KEYCODE_L: return isShift ? "L" : "l";
case KeyEvent.KEYCODE_M: return isShift ? "M" : "m";
case KeyEvent.KEYCODE_N: return isShift ? "N" : "n";
case KeyEvent.KEYCODE_O: return isShift ? "O" : "o";
case KeyEvent.KEYCODE_P: return isShift ? "P" : "p";
case KeyEvent.KEYCODE_Q: return isShift ? "Q" : "q";
case KeyEvent.KEYCODE_R: return isShift ? "R" : "r";
case KeyEvent.KEYCODE_S: return isShift ? "S" : "s";
case KeyEvent.KEYCODE_T: return isShift ? "T" : "t";
case KeyEvent.KEYCODE_U: return isShift ? "U" : "u";
case KeyEvent.KEYCODE_V: return isShift ? "V" : "v";
case KeyEvent.KEYCODE_W: return isShift ? "W" : "w";
case KeyEvent.KEYCODE_X: return isShift ? "X" : "x";
case KeyEvent.KEYCODE_Y: return isShift ? "Y" : "y";
case KeyEvent.KEYCODE_Z: return isShift ? "Z" : "z";
//符号键11个 + 11个
case KeyEvent.KEYCODE_COMMA: return isShift ? "<" : ",";
case KeyEvent.KEYCODE_PERioD: return isShift ? ">" : ".";
case KeyEvent.KEYCODE_SLASH: return isShift ? "?" : "/";
case KeyEvent.KEYCODE_BACKSLASH: return isShift ? "|" : "\\";
case KeyEvent.KEYCODE_APOSTROPHE: return isShift ? "\"" : "'";
case KeyEvent.KEYCODE_SEMICOLON: return isShift ? ":" : ";";
case KeyEvent.KEYCODE_LEFT_BRACKET: return isShift ? "{" : "[";
case KeyEvent.KEYCODE_RIGHT_BRACKET: return isShift ? "}" : "]";
case KeyEvent.KEYCODE_GRAVE: return isShift ? "~" : "`";
case KeyEvent.KEYCODE_EQUALS: return isShift ? "+" : "=";
case KeyEvent.KEYCODE_MINUS: return isShift ? "_" : "-";
default: return "?";
}
}
效果1:
效果2:
--结束END--
本文标题: Android实现读取扫码枪内容(条形码)
本文链接: https://lsjlt.com/news/137026.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0