前言
最近接到一个需求 是通过硬件SAM识别身份证信息 实时返回给后台
但是现在硬件提供方的API只有主动获取身份证信息的方法
其原理是手动点击一个按钮 然后执行一个方法 返回你想要的数据
解决思路
解决思路是 利用线程 每多少秒执行一次方法 去监听客户有没有放身份证上去
这样来达到 我们想要的效果
及:只要客户放身份证到模块上 在几秒内 就会显示该客户的身份证信息
代码
在Activity完全创建后 执行一个线程操作
//定义的变量
boolean result = false;
//默认的值
int count = 0;
@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
new Thread(() -> {
while (!result) {
try {
Thread.sleep(5 * 1000); //设置暂停的时间 5 秒
//代表执行的次数
count++;
getUserInfo();//监听数据
if (count == 10) {
result = true;
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
).start();
}
获取用户的信息方法
private void getUserInfo() {
//起android的ui线程
runOnUiThread(new Runnable() {
@Override
public void run() {
ImageView mImageView = new ImageView(RealNameActivity.this);
int ret = 0;
llGroup.removeAllViews();
String[] decodeInfo = new String[13];
//获取组件
if (iDCardDevice == null) {
iDCardDevice = new publicSecurityIDCardLib(RealNameActivity.this);
}
Arrays.fill(BmpFile, (byte) 0x00);
try {
//获取读卡器的状态
ret = iDCardDevice.PICC_Reader_ForeignerIDCard(decodeInfo, BmpFile, pkName);
if (ret < 0) {
showErrorMsg("读卡错误,原因:" + decodeInfo[12]);
} else if (ret == 0) {
showString("读取成功");
showString("中华人民共和国居民身份证");
showString("姓名:" + decodeInfo[0] + "\n" + "性别:" + decodeInfo[1] + "\n" + "民族:" + decodeInfo[2] + "\n"
+ "出生日期:" + decodeInfo[3] + "\n" + "地址:" + decodeInfo[4] + "\n" + "身份号码:" + decodeInfo[5] + "\n"
+ "签发机关:" + decodeInfo[6] + "\n" + "有效期限:" + decodeInfo[7] + "-" + decodeInfo[8] + "\n"
+ decodeInfo[9] + "\n");
//获取数据信息
userName = decodeInfo[0].trim();
userSex = decodeInfo[1].trim();
userID = decodeInfo[5].trim();
mImageView.setScaleType(ImageView.ScaleType.MATRIX);
int[] colors = iDCardDevice.convertByteToColor(BmpFile);
Bitmap bm = Bitmap.createBitmap(colors, 102, 126, Bitmap.Config.ARGB_8888);
bm.setHasAlpha(true);
Bitmap bm1 = Bitmap.createScaledBitmap(bm,
(102 * 2), (126 * 2), false); //这里你可以自定义它的大小
mImageView.setImageBitmap(bm1);
llGroup.addView(mImageView);
} else if (ret == 1) {
showString("读取成功");
showString("外国人永久居住证");
showString("英文姓名:" + decodeInfo[0] + "\n" + "性别:" + decodeInfo[1] + "\n" + "证号:" + decodeInfo[2] + "\n"
+ "地区代码:" + decodeInfo[3] + "\n" + "中文姓名:" + decodeInfo[4] + "\n" + "签发日期:" + decodeInfo[5] + "\n" + "终止日期:" + decodeInfo[6] + "\n"
+ "出生日期:" + decodeInfo[7] + "\n" + "版本号:" + decodeInfo[8] + "\n" + "机关代码:" + decodeInfo[9] + "\n" + "证件标识" + decodeInfo[10] + "\n" + "预留项:" + decodeInfo[11] + "\n");
//获取数据信息
userName = decodeInfo[0].trim();
userSex = decodeInfo[1].trim();
userID = decodeInfo[5].trim();
int[] colors = iDCardDevice.convertByteToColor(BmpFile);
Bitmap bm = Bitmap.createBitmap(colors, 102, 126, Bitmap.Config.ARGB_8888);
Bitmap bm1 = Bitmap.createScaledBitmap(bm,
(102 * 2), (126 * 2), false); //这里你可以自定义它的大小
mImageView.setScaleType(ImageView.ScaleType.MATRIX);
mImageView.setImageBitmap(bm1);
llGroup.addView(mImageView);
}
} catch (UnsupportedEncodingException | InterruptedException e1) {
e1.printStackTrace();
Log.e(TAG, " ================== e1 ===========" + e1.getMessage());
}
}
});
}
总结
这个就是练习 线程和android的 runOnUiThread()
的用法!