基本信息
源码名称:C# 读取身份证信息Demo(WltRS)
源码大小:0.48M
文件格式:.zip
开发语言:C#
更新时间:2018-07-25
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 5 元×
微信扫码支付:5 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
读取身份证信息
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace ReadCard
{
public class ReaderCard
{
#region API声明
[DllImport("sdtapi.dll", CallingConvention = CallingConvention.StdCall)]
static extern int SDT_StartFindIDCard(int iPort, byte[] pucManaInfo, int iIfOpen);
[DllImport("sdtapi.dll", CallingConvention = CallingConvention.StdCall)]
static extern int SDT_SelectIDCard(int iPort, byte[] pucManaMsg, int iIfOpen);
[DllImport("sdtapi.dll", CallingConvention = CallingConvention.StdCall)]
static extern int SDT_ReadBaseMsg(int iPort, byte[] pucCHMsg, ref UInt32 puiCHMsgLen, byte[] pucPHMsg, ref UInt32 puiPHMsgLen, int iIfOpen);
[DllImport("WltRS.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetBmp(string s);
#endregion
static byte[] CardPUCIIN = new byte[255];
static byte[] pucManaMsg = new byte[255];
static byte[] pucCHMsg = new byte[255];
static byte[] pucPHMsg = new byte[3024];
/// <summary>
/// 读卡
/// </summary>
/// <param name="errorMessage"></param>
/// <returns></returns>
public static ReadNewModel ReaderNowCard(out string errorMessage)
{
try
{
errorMessage = string.Empty;
Array.Clear(CardPUCIIN, 0, CardPUCIIN.Length);
Array.Clear(pucManaMsg, 0, pucManaMsg.Length);
Array.Clear(pucCHMsg, 0, pucCHMsg.Length);
Array.Clear(pucPHMsg, 0, pucPHMsg.Length);
//变量声明
//byte[] CardPUCIIN = new byte[255];
//byte[] pucManaMsg = new byte[255];
//byte[] pucCHMsg = new byte[255];
//byte[] pucPHMsg = new byte[3024];
UInt32 puiCHMsgLen = 0;
UInt32 puiPHMsgLen = 0;
int st = 0;
//读卡操作
st = SDT_StartFindIDCard(1001, CardPUCIIN, 1);
if (st != 0x9f) return null;
st = SDT_SelectIDCard(1001, pucManaMsg, 1);
if (st != 0x90) return null;
st = SDT_ReadBaseMsg(1001, pucCHMsg, ref puiCHMsgLen, pucPHMsg, ref puiPHMsgLen, 1);
if (st != 0x90) return null;
//显示结果
ReadNewModel resultModel = new ReadNewModel();
resultModel.Name = System.Text.UnicodeEncoding.Unicode.GetString(pucCHMsg, 0, 30).Trim();
resultModel.Sex_Code = System.Text.UnicodeEncoding.Unicode.GetString(pucCHMsg, 30, 2).Trim();
resultModel.NATION_Code = System.Text.UnicodeEncoding.Unicode.GetString(pucCHMsg, 32, 4).Trim();
string strBird = System.Text.UnicodeEncoding.Unicode.GetString(pucCHMsg, 36, 16).Trim();
resultModel.BIRTH =(strBird.Substring(0, 4) "-" strBird.Substring(4, 2) "-" strBird.Substring(6));
resultModel.ADDRESS = System.Text.UnicodeEncoding.Unicode.GetString(pucCHMsg, 52, 70).Trim();
resultModel.IDC = System.Text.UnicodeEncoding.Unicode.GetString(pucCHMsg, 122, 36).Trim();
resultModel.REGORG = System.Text.UnicodeEncoding.Unicode.GetString(pucCHMsg, 158, 30).Trim();
string strTem = System.Text.UnicodeEncoding.Unicode.GetString(pucCHMsg, 188, pucCHMsg.GetLength(0) - 188).Trim();
resultModel.STARTDATE = Convert.ToDateTime(strTem.Substring(0, 4) "年" strTem.Substring(4, 2) "月" strTem.Substring(6, 2) "日");
strTem = strTem.Substring(8);
if (strTem.Trim() != "长期")
{
resultModel.ENDDATE = Convert.ToDateTime(strTem.Substring(0, 4) "年" strTem.Substring(4, 2) "月" strTem.Substring(6, 2) "日");
}
else
{
resultModel.ENDDATE = DateTime.MaxValue;
}
string newCard = resultModel.IDC;
string temppath = Common.CardPath "\\" newCard ".wlt";
System.IO.FileStream f = new System.IO.FileStream(temppath, System.IO.FileMode.OpenOrCreate);
f.Write(pucPHMsg, 0, (int)puiPHMsgLen);
f.Flush();
f.Close();
f.Dispose();
try { st = GetBmp(temppath); }
catch (Exception)
{
}
string newpath = Common.CardPath "\\" newCard ".bmp";
if (File.Exists(newpath))
{
resultModel.PIC_Byte = Common.ImageToByteArray(newpath);
resultModel.PIC_Image = Common.GetImageByByte(resultModel.PIC_Byte);
File.Delete(newpath);
return resultModel;
}
else
{
errorMessage = "生成图片失败!";
return null;
}
}
catch (Exception ex) { errorMessage = ex.Message; return null; }
}
}
}