You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

148 lines
5.9 KiB

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ShenTun.IcCard.Common;
namespace ShenTun.IcCard.Instance
{
/// <summary>
/// 德卡(T10-D)
/// </summary>
public class ReadCardByDCT10D : IReadCard
{
Response IReadCard.GetReadCard()
{
int iPort = 1001;
int baud = 115200;
string sexId = string.Empty;
string nationId = string.Empty;
string birthTmp = string.Empty;
Response res = new Response()
{
code = -101
};
int handle = -1;
IDCardInfo idCard = new IDCardInfo();
try
{
handle = DCTD10.DCSDT_Open(iPort, baud);
if (handle < 0)
{
res.code = -101;
res.message = "打开读卡设备失败,请重新插拨读卡器!";
return res;
}
byte[] management_number = new byte[9];
byte[] serial_number = new byte[17];
var find = DCTD10.DCSDT_SearchIdCard(handle, management_number, serial_number);
if (find < 0)//巡卡失败
{
res.code = -102;
res.message = "巡卡失败,请拿开身份证重新放置!";
return res;
}
var type = DCTD10.DCSDT_GetIdCardType(handle);
if (type < 0)
{
res.code = -103;
res.message = "获取卡类型失败,请拿开身份证重新放置!";
return res;
}
else
{
if (type == 0)
{
var result = DCTD10.DCSDT_IdCardRead(handle, 1);
if (result < 0)
{
res.code = -104;
res.message = "获取ID卡失败,请拿开身份证重新放置!";
return res;
}
byte[] kName = new byte[64];
byte[] bSex = new byte[8];
byte[] bNation = new byte[12];
byte[] bBirth = new byte[36];
byte[] kAddress = new byte[144];
byte[] kIdNumber = new byte[76];
byte[] kPhoto = new byte[4096];
var bname = DCTD10.DCSDT_IdCardContent(handle, 1, kName);
var bsex = DCTD10.DCSDT_IdCardContent(handle, 2, bSex);
var bnation = DCTD10.DCSDT_IdCardContent(handle, 3, bNation);
var bbirth = DCTD10.DCSDT_IdCardContent(handle, 4, bBirth);
var bAddress = DCTD10.DCSDT_IdCardContent(handle, 5, kAddress);
var bIdNumber = DCTD10.DCSDT_IdCardContent(handle, 6, kIdNumber);
var bPhoto = DCTD10.DCSDT_IdCardContent(handle, 10, kPhoto);
idCard.Name = System.Text.Encoding.Default.GetString(kName).TrimEnd('\0');
sexId = System.Text.Encoding.Default.GetString(bSex).TrimEnd('\0');
var sex= BaseData.sList.Where(p => p.Id.Equals(sexId)).FirstOrDefault();
idCard.Sex = sex.Name;
idCard.IDCode = System.Text.Encoding.Default.GetString(kIdNumber).TrimEnd('\0');
birthTmp = System.Text.Encoding.Default.GetString(bBirth).TrimEnd('\0');
idCard.Birthday = string.Format("{0}-{1}-{2}", birthTmp.Substring(0, 4), birthTmp.Substring(4, 2), birthTmp.Substring(6, 2));
idCard.Address = System.Text.Encoding.Default.GetString(kAddress).TrimEnd('\0');
nationId = System.Text.Encoding.Default.GetString(bNation).TrimEnd('\0');
var nation = BaseData.nList.Where(p => p.Id.Equals(nationId)).FirstOrDefault();
idCard.Nation = nation.FullName;
if (bPhoto == 0)
{
string path_current = System.IO.Directory.GetCurrentDirectory();
string img = "zp.bmp";
string file = string.Format("{0}\\{1}", path_current, img);
byte[] byteArray = System.Text.Encoding.Default.GetBytes(file);
var photo = DCTD10.DCSDT_PhotoToBmpFile(kPhoto, byteArray);
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] imageByte = new byte[file.Length];
BinaryReader br = new BinaryReader(fs);
imageByte = br.ReadBytes(Convert.ToInt32(fs.Length));
MemoryStream ms = new MemoryStream(imageByte);
Bitmap bm = new Bitmap(ms);
string base64 = ImageHelper.ToBase64(bm, ImageFormat.Bmp);
fs.Dispose();
ms.Dispose();
idCard.Photo = base64;
}
res.code = 1;
res.data = idCard;
res.message = "Success";
}
}
}
catch (Exception ex)
{
throw new Exception(string.Format("读卡器接口操作失败:{0}", ex.Message));
}
finally
{
if(handle>0)
DCTD10.DCSDT_Close(handle);
}
return res;
}
}
}