ab56a9bcf7
SVN-Revision: r240
335 lines
14 KiB
C#
335 lines
14 KiB
C#
/******************************
|
|
* 说明:身份证阅读器(CVR-100)辅助类
|
|
* 创建人:龚宇超
|
|
* 创建日期:2018-05-30
|
|
* 修改人:
|
|
* 修改日期:
|
|
* 修改备注:
|
|
* 版本:1.0.0.0
|
|
******************************/
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Runtime.InteropServices;
|
|
using Lskj.Util;
|
|
using System.Windows.Forms;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
|
|
namespace Lskj.Control.Model
|
|
{
|
|
/// <summary>
|
|
/// 身份证阅读器(CVR-100)辅助类
|
|
/// </summary>
|
|
public class IDCardUtil
|
|
{
|
|
/// <summary>
|
|
/// 是否正在读取
|
|
/// </summary>
|
|
public static bool IsConnect = false;
|
|
public static bool IsTip = false;
|
|
|
|
/// <summary>
|
|
/// <para>说明:初始化身份证阅读器</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="tipMsg">返回提示信息.</param>
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
|
public static bool InitIDCard()
|
|
{
|
|
int iPort;
|
|
int iRetUSB = 0;
|
|
|
|
try
|
|
{
|
|
for (iPort = 1001; iPort <= 1016; iPort++)
|
|
{
|
|
iRetUSB = IDCardSDK.CVR_InitComm(iPort);
|
|
if (iRetUSB == 1)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
return iRetUSB == 1;
|
|
}
|
|
public static int CloseIDCard()
|
|
{
|
|
IsConnect = false;
|
|
return IDCardSDK.CVR_CloseComm();
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:是否可读身份验证</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-31 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <returns><c>true</c> if this instance is read; otherwise, <c>false</c>.</returns>
|
|
public static bool IsRead()
|
|
{
|
|
if (IDCardSDK.CVR_Authenticate() == 1)
|
|
return IDCardSDK.CVR_Read_Content(4) == 1;
|
|
return false;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:读取指定身份证内容</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="cardType">Type of the card.</param>
|
|
/// <returns>System.Object.</returns>
|
|
public static object ReadCardMessage(IDCardType cardType)
|
|
{
|
|
object returnValue = string.Empty;
|
|
|
|
switch (cardType)
|
|
{
|
|
case IDCardType.Name:
|
|
{
|
|
byte[] name = new byte[30];
|
|
int length = 30;
|
|
IDCardSDK.GetPeopleName(ref name[0], ref length);
|
|
|
|
returnValue = Encoding.GetEncoding("GB2312").GetString(name).Replace("\0", "").Trim();
|
|
}
|
|
break;
|
|
case IDCardType.Sex:
|
|
{
|
|
byte[] sex = new byte[30];
|
|
int length = 3;
|
|
IDCardSDK.GetPeopleSex(ref sex[0], ref length);
|
|
returnValue = Encoding.GetEncoding("GB2312").GetString(sex).Replace("\0", "").Trim();
|
|
}
|
|
break;
|
|
case IDCardType.Photo:
|
|
{
|
|
string path = Application.StartupPath + "\\zp.bmp";
|
|
returnValue = new Bitmap(ImageHelper.ReadImage(path));
|
|
break;
|
|
}
|
|
case IDCardType.Nation:
|
|
{
|
|
byte[] people = new byte[30];
|
|
int length = 3;
|
|
IDCardSDK.GetPeopleNation(ref people[0], ref length);
|
|
|
|
returnValue = Encoding.GetEncoding("GB2312").GetString(people).Replace("\0", "").Trim();
|
|
}
|
|
break;
|
|
case IDCardType.Birthday:
|
|
{
|
|
byte[] birthday = new byte[30];
|
|
int length = 16;
|
|
IDCardSDK.GetPeopleBirthday(ref birthday[0], ref length);
|
|
returnValue = Encoding.GetEncoding("GB2312").GetString(birthday).Replace("\0", "").Trim();
|
|
}
|
|
break;
|
|
case IDCardType.CardNo:
|
|
{
|
|
byte[] number = new byte[30];
|
|
int length = 36;
|
|
IDCardSDK.GetPeopleIDCode(ref number[0], ref length);
|
|
|
|
returnValue = Encoding.GetEncoding("GB2312").GetString(number).Replace("\0", "").Trim();
|
|
}
|
|
break;
|
|
case IDCardType.Address:
|
|
{
|
|
byte[] address = new byte[200];
|
|
int length = 200;
|
|
IDCardSDK.GetPeopleAddress(ref address[0], ref length);
|
|
returnValue = Encoding.GetEncoding("GB2312").GetString(address).Replace("\0", "").Trim();
|
|
}
|
|
break;
|
|
case IDCardType.Dept:
|
|
{
|
|
byte[] signdate = new byte[30];
|
|
int length = 30;
|
|
IDCardSDK.GetDepartment(ref signdate[0], ref length);
|
|
returnValue = Encoding.GetEncoding("GB2312").GetString(signdate).Replace("\0", "").Trim();
|
|
}
|
|
break;
|
|
case IDCardType.ValidBeginDate:
|
|
{
|
|
byte[] validtermOfStart = new byte[30];
|
|
int length = 16;
|
|
IDCardSDK.GetStartDate(ref validtermOfStart[0], ref length);
|
|
returnValue = Encoding.GetEncoding("GB2312").GetString(validtermOfStart).Replace("\0", "").Trim();
|
|
}
|
|
break;
|
|
case IDCardType.ValidEndDate:
|
|
{
|
|
byte[] validtermOfEnd = new byte[30];
|
|
int length = 16;
|
|
IDCardSDK.GetEndDate(ref validtermOfEnd[0], ref length);
|
|
returnValue = Encoding.GetEncoding("GB2312").GetString(validtermOfEnd).Replace("\0", "").Trim();
|
|
}
|
|
break;
|
|
case IDCardType.SamId:
|
|
{
|
|
byte[] samid = new byte[32];
|
|
IDCardSDK.CVR_GetSAMID(ref samid[0]);
|
|
returnValue = Encoding.GetEncoding("GB2312").GetString(samid).Replace("\0", "").Trim();
|
|
}
|
|
break;
|
|
}
|
|
return returnValue;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:读取身份证全部信息</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <returns>Dictionary<System.Int32, System.Object>.</returns>
|
|
public static Dictionary<IDCardType, object> ReadCardAllMessage()
|
|
{
|
|
try
|
|
{
|
|
if (!IsConnect) IsConnect = InitIDCard();
|
|
|
|
//if (!IsConnect && !IsTip)
|
|
//{
|
|
// MessageUtil.Show("设备未连接,请连接设备.");
|
|
// IsTip = true;
|
|
//}
|
|
//else
|
|
//{
|
|
// IsTip = false;
|
|
//}
|
|
|
|
Dictionary<IDCardType, object> idCards = new Dictionary<IDCardType, object>();
|
|
|
|
if (IsConnect && IsRead())
|
|
{
|
|
idCards.Add(IDCardType.Name, ReadCardMessage(IDCardType.Name));
|
|
idCards.Add(IDCardType.Sex, ReadCardMessage(IDCardType.Sex));
|
|
idCards.Add(IDCardType.Address, ReadCardMessage(IDCardType.Address));
|
|
idCards.Add(IDCardType.Birthday, ReadCardMessage(IDCardType.Birthday));
|
|
idCards.Add(IDCardType.CardNo, ReadCardMessage(IDCardType.CardNo));
|
|
idCards.Add(IDCardType.Dept, ReadCardMessage(IDCardType.Dept));
|
|
idCards.Add(IDCardType.Nation, ReadCardMessage(IDCardType.Nation));
|
|
idCards.Add(IDCardType.Photo, ReadCardMessage(IDCardType.Photo));
|
|
idCards.Add(IDCardType.SamId, ReadCardMessage(IDCardType.SamId));
|
|
idCards.Add(IDCardType.ValidBeginDate, ReadCardMessage(IDCardType.ValidBeginDate));
|
|
idCards.Add(IDCardType.ValidEndDate, ReadCardMessage(IDCardType.ValidEndDate));
|
|
}
|
|
return idCards;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.Instance.WriteLog(ex);
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 身份证阅读器SDK
|
|
/// </summary>
|
|
public class IDCardSDK
|
|
{
|
|
[DllImport("termb.dll", EntryPoint = "CVR_InitComm", CharSet = CharSet.Auto, SetLastError = false)]
|
|
public static extern int CVR_InitComm(int Port);//声明外部的标准动态库, 跟Win32API是一样的
|
|
[DllImport("termb.dll", EntryPoint = "CVR_Authenticate", CharSet = CharSet.Auto, SetLastError = false)]
|
|
public static extern int CVR_Authenticate();
|
|
[DllImport("termb.dll", EntryPoint = "CVR_Read_Content", CharSet = CharSet.Auto, SetLastError = false)]
|
|
public static extern int CVR_Read_Content(int Active);
|
|
[DllImport("termb.dll", EntryPoint = "CVR_CloseComm", CharSet = CharSet.Auto, SetLastError = false)]
|
|
public static extern int CVR_CloseComm();
|
|
[DllImport("termb.dll", EntryPoint = "GetPeopleName", CharSet = CharSet.Ansi, SetLastError = false)]
|
|
public static extern unsafe int GetPeopleName(ref byte strTmp, ref int strLen);
|
|
[DllImport("termb.dll", EntryPoint = "GetPeopleNation", CharSet = CharSet.Ansi, SetLastError = false)]
|
|
public static extern int GetPeopleNation(ref byte strTmp, ref int strLen);
|
|
[DllImport("termb.dll", EntryPoint = "GetPeopleBirthday", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int GetPeopleBirthday(ref byte strTmp, ref int strLen);
|
|
[DllImport("termb.dll", EntryPoint = "GetPeopleAddress", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int GetPeopleAddress(ref byte strTmp, ref int strLen);
|
|
[DllImport("termb.dll", EntryPoint = "GetPeopleIDCode", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int GetPeopleIDCode(ref byte strTmp, ref int strLen);
|
|
[DllImport("termb.dll", EntryPoint = "GetDepartment", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int GetDepartment(ref byte strTmp, ref int strLen);
|
|
[DllImport("termb.dll", EntryPoint = "GetStartDate", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int GetStartDate(ref byte strTmp, ref int strLen);
|
|
[DllImport("termb.dll", EntryPoint = "GetEndDate", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int GetEndDate(ref byte strTmp, ref int strLen);
|
|
[DllImport("termb.dll", EntryPoint = "GetPeopleSex", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int GetPeopleSex(ref byte strTmp, ref int strLen);
|
|
[DllImport("termb.dll", EntryPoint = "CVR_GetSAMID", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int CVR_GetSAMID(ref byte strTmp);
|
|
[DllImport("termb.dll", EntryPoint = "GetManuID", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
|
|
public static extern int GetManuID(ref byte strTmp);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 读取身份证类型
|
|
/// </summary>
|
|
public enum IDCardType
|
|
{
|
|
/// <summary>
|
|
/// 姓名
|
|
/// </summary>
|
|
Name = 1,
|
|
/// <summary>
|
|
/// 性别
|
|
/// </summary>
|
|
Sex = 2,
|
|
/// <summary>
|
|
/// 照片
|
|
/// </summary>
|
|
Photo = 3,
|
|
/// <summary>
|
|
/// 民族
|
|
/// </summary>
|
|
Nation = 4,
|
|
/// <summary>
|
|
/// 出生日期
|
|
/// </summary>
|
|
Birthday = 5,
|
|
/// <summary>
|
|
/// 身份证号
|
|
/// </summary>
|
|
CardNo = 6,
|
|
/// <summary>
|
|
/// 地址
|
|
/// </summary>
|
|
Address = 7,
|
|
/// <summary>
|
|
/// 签发机关
|
|
/// </summary>
|
|
Dept = 8,
|
|
/// <summary>
|
|
/// 有效期起
|
|
/// </summary>
|
|
ValidBeginDate = 9,
|
|
/// <summary>
|
|
/// 有效期止
|
|
/// </summary>
|
|
ValidEndDate = 10,
|
|
/// <summary>
|
|
/// 安全模块号
|
|
/// </summary>
|
|
SamId = 11
|
|
}
|
|
}
|