自定义一个身份证回调接口
import com.zkteco.android.biometric.module.idcard.meta.IDCardInfo;
/**
* developer : xuyanyun
* tel : 139159*****
* createTime : 2018/8/14 16:36
* viewName :
* remark :
* hasBug : unKnown
*/
public interface ReadCardInterface {
/**
* 获取身份证信息
* @param info
*/
void getIdentityInfo(IDCardInfo info);
/**
* 获取身份证失败
*/
void getIdentityInfoFailed();
/**
* 相同的身份证
*/
void getSameIdentityInfo();
/**
* 身份证拿开
*/
void leaveCard();
}
写一个工具类
import android.os.AsyncTask;
import com.hdd.lancity.pad.App;
import com.zkteco.android.biometric.core.device.ParameterHelper;
import com.zkteco.android.biometric.core.device.TransportType;
import com.zkteco.android.biometric.module.idcard.IDCardReader;
import com.zkteco.android.biometric.module.idcard.IDCardReaderFactory;
import com.zkteco.android.biometric.module.idcard.exception.IDCardReaderException;
import com.zkteco.android.biometric.module.idcard.meta.IDCardInfo;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
/**
* developer : xuyanyun
* tel : 139159*****
* createTime : 2018/9/29 22:33
* viewName :
* remark :
* hasBug : unKnown
*/
public enum IDeviceControlUtil {
INS;
private final int VID = 1024; //IDR VID
private final int PID = 50010; //IDR PID
/**
* 身份证读卡
*/
private IDCardReader idCardReader = null;
/**
* 身份证读卡器是否打开
*/
private boolean bopen;
/**
* 是否循环读取
* false 单次 true 循环
*/
private boolean isCircle = false;
/**
* 是否暂停读取
* false 不暂停 true 暂停
*/
private boolean isPause = false;
private boolean isRunningTask = false;
private ReadCardInterface readCardInterface;
public void setReadCardInterface(ReadCardInterface readCardInterface) {
this.readCardInterface = readCardInterface;
}
/**
* 获取身份证读卡器对象
* 第一步
*
* @return
*/
public void initDevcie() {
Map<String, Object> idrparams = new HashMap();
idrparams.put(ParameterHelper.PARAM_KEY_VID, VID);
idrparams.put(ParameterHelper.PARAM_KEY_PID, PID);
idCardReader = IDCardReaderFactory.createIDCardReader(App.getIns(), TransportType.USB, idrparams);
}
private IDCardReader getReader(){
if (null == idCardReader){
initDevcie();
}
return idCardReader;
}
//获取IDCardReader对象,然后打开设备,打开成功开始读取
/**
* 打开身份证读卡器
* 第二步
*/
public void openDevice() {
try {
if (bopen) return;
getReader().open(0);
getReader().getSAMID(0);
bopen = true;
} catch (IDCardReaderException e) {
bopen = false;
// Log.e("", "连接设备失败, 错误码:" + e.getErrorCode() + "\n错误信息:" + e.getMessage() + "\n 内部错误码=" + e.getInternalErrorCode());
}
}
/**
* 查找设备
*/
private void findDevice() {
try {
getReader().findCard(0);
} catch (IDCardReaderException e) {
}
}
/**
* 选择设备
*/
private void selectDevice() {
try {
getReader().selectCard(0);
} catch (IDCardReaderException e) {
}
}
/**
* 关闭设备
*/
public void closeDevice() {
try {
if (bopen) {
getReader().close(0);
bopen = false;
}
} catch (IDCardReaderException e) {
// Log.e("", "关闭设备失败, 错误码:" + e.getErrorCode() + "\n错误信息:" + e.getMessage() + "\n 内部错误码=" + e.getInternalErrorCode());
}
}
/**
* 销毁
*/
public void destoryReader() {
destoryTimer();
if(null!=mAsyncTask){
mAsyncTask.cancel(true);
}
IDCardReaderFactory.destroy(getReader());
idCardReader = null ;
}
//读取身份证的现成
private GetIDInfoTask mAsyncTask;
//当身份证已经读取到后,在后面的都是同一个
private long hasRead = 0l ;
private class GetIDInfoTask extends AsyncTask<Void, Integer, IDCardInfo> {
@Override
protected void onPreExecute() {//before thread
super.onPreExecute();
}
@Override
protected IDCardInfo doInBackground(Void... arg0) {
if (!bopen)
return null;
IDCardInfo idCardInfo = null;
try {
idCardInfo = new IDCardInfo();
findDevice();
selectDevice();
boolean ret = getReader().readCard(0, 1, idCardInfo);
if (ret) {
return idCardInfo;
}
return null;
} catch (IDCardReaderException e) {
}
return null;
}
// 最终处理
@Override
protected void onPostExecute(IDCardInfo result) {
super.onPostExecute(result);
if (null != result) {
if (0 == hasRead ){
readCardInterface.getIdentityInfo(result);
}else{
readCardInterface.getSameIdentityInfo();
}
hasRead ++ ;
} else {
//只会调用一次
if (hasRead!=0){
readCardInterface.leaveCard();
}else{
readCardInterface.getIdentityInfoFailed();
}
hasRead = 0 ;
}
//不管读取成功失败都要判断是不是循环读取
if (isCircle) {
loopRead();
}
}
}
/**
* 读取身份证
*/
public void startRead() {
mAsyncTask = new GetIDInfoTask();
mAsyncTask.execute();
}
private Timer timer;
private TimerTask task;
//循环读取
private void loopRead() {
if (null == timer) {
timer = new Timer();
}
if (null == task) {
task = new TimerTask() {
@Override
public void run() {
if (!isPause) {
startRead();
return;
}
}
};
}
//2秒钟读取一次
if (!isRunningTask) {
isRunningTask = true;
timer.schedule(task, 0, 1500);
}
}
//销毁定时器
public void destoryTimer() {
if (null != timer) {
timer.cancel();
timer = null;
}
if (null != task) {
task.cancel();
task = null;
}
}
/**
* @param bb false 单次 true 循环
*/
public void readTimes(boolean bb) {
isCircle = bb;
}
/**
* false 不暂停 true 暂停
*
* @param bb 这个主要是在onStop 和onResume里使用
* onStop true onResume false
*/
public void pauseRead(boolean bb) {
isPause = bb;
}
}
使用方法
1、在application里面初始化
IDeviceControlUtil.INS.initDevcie();
2、在使用的Activity里面做动作,一定要继承接口
//回调接口
IDeviceControlUtil.INS.setReadCardInterface(this);
//打开设备
IDeviceControlUtil.INS.openDevice();
//循环读取or单次读取
IDeviceControlUtil.INS.readTimes(true);
//开始
IDeviceControlUtil.INS.startRead();
3、必须要销毁了
IDeviceControlUtil.INS.closeDevice();
IDeviceControlUtil.INS.destoryReader();
----------------------------------------------------------------------------------------------
获取身份证头像的方法
public int imgLength = 3 * 102 * 126; private Bitmap getHeadBitmap(IDCardInfo idCardInfo) { Bitmap bitmap = null; if (idCardInfo.getPhotolength() > 0) { byte[] buf = new byte[imgLength]; if (1 == WLTService.wlt2Bmp(idCardInfo.getPhoto(), buf)) { bitmap = IDPhotoHelper.INS.Bgr2Bitmap(buf); } } return bitmap ; }
请注意
WLTService和IDPhotoHelper的包名为
com.zkteco.android.IDReader
package com.zkteco.android.IDReader; import android.graphics.Bitmap; /** * developer : xuyanyun * tel : 139159***** * createTime : 2018/9/29 22:19 * viewName : 中控智慧byte转bitmap * remark : * hasBug : unKnown */ public enum IDPhotoHelper { INS ; public Bitmap Bgr2Bitmap(byte[] bgrbuf) { int width = WLTService.imgWidth; int height = WLTService.imgHeight; Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); int row = 0, col = width-1; for (int i = bgrbuf.length-1; i >= 3; i -= 3) { int color = bgrbuf[i] & 0xFF; color += (bgrbuf[i-1] << 8) & 0xFF00; color += ((bgrbuf[i-2]) << 16) & 0xFF0000; bmp.setPixel(col--, row, color); if (col < 0) { col = width-1; row++; } } return bmp; } }
package com.zkteco.android.IDReader; /** * Created by scarx on 2016/2/5. */ public class WLTService { static { System.loadLibrary("wlt2bmp"); System.loadLibrary("zkwltdecode"); } public static int imgWidth = 102; public static int imgHeight = 126; public static int imgLength = 3 * 102 * 126; public static native int wlt2Bmp(byte[] inbuf, byte[] outbuf); }
这两个动态库,请自便,他们的demo里面有。