c# 身份证读取 神思第二代居民身份证验证机具接口函数说明__版本 V2.6.2 2017-12-20
/// <summary>
/// 神思第二代居民身份证验证机具接口函数说明__版本 V2.6.2 2017-12-20
/// </summary>
public class S4IdCardServer
{
public const string dllPath1 = @"Static\Dll\S4\RdCard.dll";
[DllImport(dllPath1, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
public static extern int UCommand1(string pcmd, ref int arg0, ref int arg1, byte[] arg3);
#region 功能区
static int arg0 = 0;
static int arg1 = 8811;
static byte[] arg2 = { 0x02, 0x27, 0x00, 0x00 };
/// <summary>
/// 初始化端口
/// </summary>
/// <returns></returns>
public static int Init() {
string pcmd = Char.ConvertFromUtf32(0x41);
//初始化端口
int flg = S4IdCardServer.UCommand1(pcmd, ref arg0, ref arg1, arg2);
return flg;
}
/// <summary>
/// 验证卡
/// </summary>
/// <returns></returns>
public static int Verification()
{
//验证卡
string pcmd = Char.ConvertFromUtf32(0x43);
int flg = S4IdCardServer.UCommand1(pcmd, ref arg0, ref arg1, arg2);
return flg;
}
/// <summary>
/// 读卡
/// </summary>
/// <returns></returns>
public static int ReadCard()
{
//读卡
string pcmd = Char.ConvertFromUtf32(0x49);
//BaseConfig.result 程序运行的绝对路径
int flg = S4IdCardServer.UCommand1(pcmd, ref arg0, ref arg1, Encoding.GetEncoding("GBK").GetBytes(BaseConfig.result));
return flg;
}
/// <summary>
/// 关闭
/// </summary>
/// <returns></returns>
public static int Close()
{
string pcmd = Char.ConvertFromUtf32(0x42);
int flg = S4IdCardServer.UCommand1(pcmd, ref arg0, ref arg1, arg2);
return flg;
}
/// <summary>
/// 读取生成文件内容
/// </summary>
/// <returns></returns>
public static IdentityCardInfo GetCardInfo()
{
//程序运行的绝对路径
string url = BaseConfig.result;
IdentityCardInfo model = new IdentityCardInfo();
FileStream FSinfo = null;
string infoPatn = url + "\\wz.txt";
//string NewAddressPath = Application.StartupPath + "\\NewAdd.txt";
string binpath = url + "\\fp.bin";
string wxpath = url + "\\wx.txt";
try
{
FSinfo = new FileStream(infoPatn, FileMode.Open, FileAccess.Read);
// Thread.Sleep(500);
int infoLength = (int)FSinfo.Length;
byte[] infoBytes = new byte[infoLength];
FSinfo.Read(infoBytes, 0, infoLength);
string strs = Encoding.Unicode.GetString(infoBytes);
model.Name = Encoding.Unicode.GetString(infoBytes, 0, 30).Trim();//姓名
model.SexName = Encoding.Unicode.GetString(infoBytes, 30, 2).Trim() == "1" ? "男" : "女";//性别
int nationid = Convert.ToInt32(Encoding.Unicode.GetString(infoBytes, 32, 4).Trim());//民族
model.NationName = GetNation(nationid);
string birth = Encoding.Unicode.GetString(infoBytes, 36, 16).Trim();//出生日期
model.Birthday = DateTime.ParseExact(birth, "yyyyMMdd", null);
model.Address = Encoding.Unicode.GetString(infoBytes, 52, 70).Trim();//住址
model.CardId = Encoding.Unicode.GetString(infoBytes, 122, 36).Trim();//身份证号码
model.ISSUE_AUTH = Encoding.Unicode.GetString(infoBytes, 158, 30).Trim();//签发机关
string began = Encoding.Unicode.GetString(infoBytes, 188, 16).Trim();//有效期开始
model.StartDate = DateTime.ParseExact(began, "yyyyMMdd", null);
string end = Encoding.Unicode.GetString(infoBytes, 204, 16).Trim();//有效期结束
if (end != "长期")
{
model.EndDate = DateTime.ParseExact(end, "yyyyMMdd", null);
}
else
{
model.EndDate = DateTime.ParseExact("2100-01-01", "yyyyMMdd", null);
}
//model.HEADPORTRAIT = (Bitmap)IMAGE.GetImage(path);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
if (FSinfo != null) FSinfo.Close();
if (File.Exists(infoPatn))
{
File.Delete(infoPatn);
}
if (File.Exists(binpath))
{
File.Delete(binpath);
}
if (File.Exists(wxpath))
{
File.Delete(wxpath);
}
}
return model;
}
/// <summary>
/// 获得民族名称
/// </summary>
/// <param name="NationID">民族编号</param>
/// <returns></returns>
private static string GetNation(int NationID)
{
string nation = "";
switch (NationID)
{
case 1:
nation = "汉族";
break;
case 2:
nation = "蒙古族";
break;
case 3:
nation = "回族";
break;
case 4:
nation = "藏族";
break;
case 5:
nation = "维吾尔族";
break;
case 6:
nation = "苗族";
break;
case 7:
nation = "彝族";
break;
case 8:
nation = "壮族";
break;
case 9:
nation = "布依族";
break;
case 10:
nation = "朝鲜族";
break;
case 11:
nation = "满族";
break;
case 12:
nation = "侗族";
break;
case 13:
nation = "瑶族";
break;
case 14:
nation = "白族";
break;
case 15:
nation = "土家族";
break;
case 16:
nation = "哈尼族";
break;
case 17:
nation = "哈萨克族";
break;
case 18:
nation = "傣族";
break;
case 19:
nation = "黎族";
break;
case 20:
nation = "傈僳族";
break;
case 21:
nation = "佤族";
break;
case 22:
nation = "畲族";
break;
case 23:
nation = "高山族";
break;
case 24:
nation = "拉祜族";
break;
case 25:
nation = "水族";
break;
case 26:
nation = "东乡族";
break;
case 27:
nation = "纳西族";
break;
case 28:
nation = "景颇族";
break;
case 29:
nation = "柯尔克孜族";
break;
case 30:
nation = "土族";
break;
case 31:
nation = "达斡尔族";
break;
case 32:
nation = "仫佬族";
break;
case 33:
nation = "羌族";
break;
case 34:
nation = "布朗族";
break;
case 35:
nation = "撒拉族";
break;
case 36:
nation = "毛南族";
break;
case 37:
nation = "仡佬族";
break;
case 38:
nation = "锡伯族";
break;
case 39:
nation = "阿昌族";
break;
case 40:
nation = "普米族";
break;
case 41:
nation = "塔吉克族";
break;
case 42:
nation = "怒族";
break;
case 43:
nation = "乌孜别克族";
break;
case 44:
nation = "俄罗斯族";
break;
case 45:
nation = "鄂温克族";
break;
case 46:
nation = "德昂族";
break;
case 47:
nation = "保安族";
break;
case 48:
nation = "裕固族";
break;
case 49:
nation = "京族";
break;
case 50:
nation = "塔塔尔族";
break;
case 51:
nation = "独龙族";
break;
case 52:
nation = "鄂伦春族";
break;
case 53:
nation = "赫哲族";
break;
case 54:
nation = "门巴族";
break;
case 55:
nation = "珞巴族";
break;
case 56:
nation = "基诺族";
break;
case 57:
nation = "其他";
break;
default:
nation = "外国血统";
break;
}
return nation;
}
#endregion
}