分享:用Python语言轻松实现二代身份证阅读器(读卡器)自动读卡,支持定制开发

人工智能的兴起,最近这段时间碰到很多开发工程师咨询Python语言二次开发使用身份证阅读器的问题,这次特增加了Python语言的demo,供大家参考使用。

先上Python界面的读取效果图:

分享:用Python语言轻松实现二代身份证阅读器(读卡器)自动读卡,支持定制开发_第1张图片

1、说明:
appMain.py为程序主文件,运行工程源程序必须安装python3+PyQt5运行环境,依赖库dll必须与源程序同一路径。

2、命令行中执行:
python3 appMain.py

3、Python调用用到的文件有:
est64.dl、DLL_File.dll、license.dat,这都是厂家提供的身份证读卡器的标准库文件。

4、调试用的读卡器
广东东信智能科技有限公司出品:EST-100GS第三代身份证读卡器

分享:用Python语言轻松实现二代身份证阅读器(读卡器)自动读卡,支持定制开发_第2张图片

 

 话不多说,直接上Python的源码。

#-*- coding : utf-8-*-
# 本Python读取身份证信息的demo支持的型号有:EST-100、EST-100GS、EST-100G、EST-100U、EST-200G、EST-J13X等系列身份证社保卡读卡器。
# 广东东信智能科技有限公司 www.eastcoms.com版权所有。

import sys
from ctypes import *

id_dev_name = b"USB1"
dll_path = './est64.dll'
id_image_path = './zp.bmp'

pBmpFile =  c_char_p(b'/0'*100)
#pFingerData =  c_char_p(b'/0'*1025)
#pBmpData =  c_char_p(b'/0'*77725)
#pBase64Data = c_char_p(b'/0'*6025)
pName = c_char_p(b'/0'*100)
pSex = c_char_p(b'/0'*100)
pNation = c_char_p(b'/0'*100)
pBirth = c_char_p(b'/0'*100)
pAddress = c_char_p(b'/0'*100)
pCertNo = c_char_p(b'/0'*100)
pDepartment = c_char_p(b'/0'*100)
pEffectData = c_char_p(b'/0'*100)
pExpire = c_char_p(b'/0'*100)
pData = c_char_p(b'/0'*100)
pErrMsg = c_char_p(b'/0'*100)
pTXZHM = c_char_p(b'/0'*100)
pTXZQFCS = c_char_p(b'/0'*100)

pEnName = c_char_p(b'/0'*200)
pEnNation = c_char_p(b'/0'*100)
pAuthorCode = c_char_p(b'/0'*100)
pCardVersion = c_char_p(b'/0'*100)

pErrMsg = c_char_p(b'/0'*100)

class idreader(object):
    def __init__(self, parent=None):
        self.dev_name = c_char_p(id_dev_name)
        self.id_dev = CDLL(dll_path)
        self.id_handle = self.id_dev.EU_Reader_Open(self.dev_name)

    def get_msg(self):
        global pBmpFile,pName,pSex,\
                pNation,pBirth,pAddress,pCertNo,pDepartment,pEffectData,\
                pExpire,pData,pErrMsg,pTXZHM,pTXZQFCS,pEnName,pEnNation,\
                pAuthorCode,pCardVersion
                #pFingerData,pBmpData,pBase64Data,
                
        pBmpFile.value = bytes(id_image_path,encoding="utf8")        
        id_msg_str = ""        
        self.id_handle = self.id_dev.EU_Reader_Open(self.dev_name)
        if self.id_handle <= 0:
            print('reader port open failure.')
            return (self.id_handle,)
            
        sUID = c_char_p(b'/0'*30)    
        self.id_dev.PEU_Reader_ID_ReadUID(self.id_handle, sUID)                
        ret = self.id_dev.PEU_Reader_ReadIDCard(self.id_handle, pErrMsg)
        if ret == 0:
            idcardType = int(-1)
            idcardType = self.id_dev.GetCardType()
            if idcardType == 0:
                id_msg_str = "证件类型:居民身份证" + "\r\n"
                self.id_dev.GetName(pName)
                id_msg_str += "姓名:" + str(pName.value,encoding='gbk') + "\r\n"                
                self.id_dev.GetSex(pSex)
                id_msg_str += "性别:" + str(pSex.value,encoding='gbk') + "\r\n"                
                self.id_dev.GetNation(pNation)
                id_msg_str += "民族:" + str(pNation.value,encoding='gbk') + "\r\n"                
                self.id_dev.GetBirth(pBirth)
                id_msg_str += "出生日期:" + str(pBirth.value,encoding='gbk') + "\r\n"                
                self.id_dev.GetAddress(pAddress)
                id_msg_str += "住址:" + str(pAddress.value,encoding='gbk').strip() + "\r\n"                
                self.id_dev.GetCertNo(pCertNo)
                id_msg_str += "身份证号码:" + str(pCertNo.value,encoding='gbk') + "\r\n"                
                self.id_dev.GetDepartemt(pDepartment)
                id_msg_str += "签发机关:" + str(pDepartment.value,encoding='gbk') + "\r\n"              
                self.id_dev.GetEffectDate(pEffectData)
                id_msg_str += "有效起始日期:" + str(pEffectData.value,encoding='gbk') + "\r\n"                
                self.id_dev.GetExpireDate(pExpire)
                id_msg_str += "有效截止日期:" + str(pExpire.value,encoding='gbk') + "\r\n"
                
            elif idcardType == 1:   
                id_msg_str = "证件类型:外国人永久居留证" + "\r\n"
                self.id_dev.GetName(pName)
                id_msg_str += "中文姓名:" + str(pName.value,encoding='gbk') + "\r\n"
                self.id_dev.GetEnName(pEnName)
                id_msg_str += "英文姓名:" + str(pEnName.value,encoding='gbk')                
                self.id_dev.GetSex(pSex)
                id_msg_str += "性别:" + str(pSex.value,encoding='gbk')                
                self.id_dev.GetNationalityCode(pEnNation) 
                id_msg_str += "国籍代码:" + str(pEnNation.value,encoding='gbk') + "\r\n"                
                self.id_dev.GetBirth(pBirth)
                id_msg_str += "出生日期:" + str(pBirth.value,encoding='gbk') + "\r\n"                
                self.id_dev.GetCertNo(pCertNo) 
                id_msg_str += "永久证号码:" + str(pCertNo.value,encoding='gbk') + "\r\n"
                self.id_dev.GetEffectDate(pEffectData)
                id_msg_str += "有效起始日期:" + str(pEffectData.value,encoding='gbk') + "\r\n"
                self.id_dev.GetExpireDate(pExpire)
                id_msg_str += "有效截止日期:" + str(pExpire.value,encoding='gbk') + "\r\n"
                
            elif idcardType == 2:
                id_msg_str = "证件类型:港澳台居民居住证" + "\r\n"
                self.id_dev.GetName(pName)
                id_msg_str += "姓名:" + str(pName.value,encoding='gbk') + "\r\n"
                self.id_dev.GetSex(pSex)
                id_msg_str += "性别:" + str(pSex.value,encoding='gbk') + "\r\n"
                self.id_dev.GetBirth(pBirth)
                id_msg_str += "出生日期:" + str(pBirth.value,encoding='gbk') + "\r\n"
                self.id_dev.GetCertNo(pCertNo)
                id_msg_str += "身份证号码:" + str(pCertNo.value,encoding='gbk') + "\r\n"
                self.id_dev.GetAddress(pAddress)
                id_msg_str += "住址:" + str(pAddress.value,encoding='gbk').strip() + "\r\n"
                self.id_dev.GetDepartemt(pDepartment)
                id_msg_str += "签发机关:" + str(pDepartment.value,encoding='gbk') + "\r\n"
                self.id_dev.GetEffectDate(pEffectData)
                id_msg_str += "有效起始日期:" + str(pEffectData.value,encoding='gbk') + "\r\n"
                self.id_dev.GetExpireDate(pExpire)
                id_msg_str += "有效截止日期:" + str(pExpire.value,encoding='gbk') + "\r\n"
                self.id_dev.GetTXZHM(pTXZHM)
                id_msg_str += "通行证号码:" + str(pTXZHM.value,encoding='gbk') + "\r\n"
                self.id_dev.GetTXZQFCS(pTXZQFCS)
                id_msg_str += "通行证签发次数:" + str(pTXZQFCS.value,encoding='gbk') + "\r\n"
                
            else:
                print("id card read failure.")
                return  (idcardType,)
               
            img = self.id_dev.GetBmpFile(pBmpFile);
            self.id_dev.EU_PosBeep(self.id_handle, 0x30);            
            return (ret,id_msg_str,img,id_image_path)
        else:
            print('PEU_Reader_ReadIDCard error.')
            return (ret,)
            
    def get_uid(self):
        uid_pstr = c_char_p(b'/0'*20)
        self.id_handle = self.id_dev.EU_Reader_Open(self.dev_name)
        ret = self.id_dev.PEU_Reader_ID_ReadUID(self.id_handle,uid_pstr)
        if ret == 0:
            self.id_dev.EU_PosBeep(self.id_handle, 0x30);
        uid_str = str(uid_pstr.value,encoding='utf-8')
        return (ret,uid_str)

        
if  __name__ == "__main__":      
    idcard = idreader()
    
    uid=idcard.get_uid()
    print('id card uid=')
    print(uid)
    
    id_msg = idcard.get_msg()
    print('id card message:')
    print(id_msg)

        

 

你可能感兴趣的:(python,人工智能,自然语言处理)