问题一
解决方法:
通常我把所有的dll复制到system32文件夹,64位系统复制到sysWOW64文件夹,而且编译选项CPU要选择X86
问题二
解决方法:
图片路径默认是C:\,对C:\没有写的权限,所以提示此错误。改变此路径到别的盘符就可以解决。
以下是我编写的代码
public class IDCardHelper
{
#region 数据类型和导入接口 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = )]
public struct IDCardData
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
public string Name; //姓名
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
public string Sex; //性别
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
public string Nation; //名族
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
public string Born; //出生日期
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
public string Address; //住址
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
public string IDCardNo; //身份证号
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
public string GrantDept; //发证机关
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
public string UserLifeBegin; // 有效开始日期
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
public string UserLifeEnd; // 有效截止日期
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
public string reserved; // 保留
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
public string PhotoFileName; // 照片路径
}
/************************端口类API *************************/
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetCOMBaud", CharSet = CharSet.Ansi)]
public static extern int Syn_GetCOMBaud(int iPort, ref uint puiBaudRate);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetCOMBaud", CharSet = CharSet.Ansi)]
public static extern int Syn_SetCOMBaud(int iPort, uint uiCurrBaud, uint uiSetBaud);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_OpenPort", CharSet = CharSet.Ansi)]
public static extern int Syn_OpenPort(int iPort);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ClosePort", CharSet = CharSet.Ansi)]
public static extern int Syn_ClosePort(int iPort);
/**************************SAM类函数 **************************/
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetMaxRFByte", CharSet = CharSet.Ansi)]
public static extern int Syn_SetMaxRFByte(int iPort, byte ucByte, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ResetSAM", CharSet = CharSet.Ansi)]
public static extern int Syn_ResetSAM(int iPort, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetSAMStatus", CharSet = CharSet.Ansi)]
public static extern int Syn_GetSAMStatus(int iPort, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetSAMID", CharSet = CharSet.Ansi)]
public static extern int Syn_GetSAMID(int iPort, ref byte pucSAMID, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetSAMIDToStr", CharSet = CharSet.Ansi)]
public static extern int Syn_GetSAMIDToStr(int iPort, ref byte pcSAMID, int iIfOpen);
/*************************身份证卡类函数 ***************************/
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_StartFindIDCard", CharSet = CharSet.Ansi)]
public static extern int Syn_StartFindIDCard(int iPort, ref byte pucIIN, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SelectIDCard", CharSet = CharSet.Ansi)]
public static extern int Syn_SelectIDCard(int iPort, ref byte pucSN, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadBaseMsg", CharSet = CharSet.Ansi)]
public static extern int Syn_ReadBaseMsg(int iPort, ref byte pucCHMsg, ref uint puiCHMsgLen, ref byte pucPHMsg, ref uint puiPHMsgLen, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadBaseMsgToFile", CharSet = CharSet.Ansi)]
public static extern int Syn_ReadBaseMsgToFile(int iPort, ref byte pcCHMsgFileName, ref uint puiCHMsgFileLen, ref byte pcPHMsgFileName, ref uint puiPHMsgFileLen, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadBaseFPMsg", CharSet = CharSet.Ansi)]
public static extern int Syn_ReadBaseFPMsg(int iPort, ref byte pucCHMsg, ref uint puiCHMsgLen, ref byte pucPHMsg, ref uint puiPHMsgLen, ref byte pucFPMsg, ref uint puiFPMsgLen, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadBaseFPMsgToFile", CharSet = CharSet.Ansi)]
public static extern int Syn_ReadBaseFPMsgToFile(int iPort, ref byte pcCHMsgFileName, ref uint puiCHMsgFileLen, ref byte pcPHMsgFileName, ref uint puiPHMsgFileLen, ref byte pcFPMsgFileName, ref uint puiFPMsgFileLen, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadNewAppMsg", CharSet = CharSet.Ansi)]
public static extern int Syn_ReadNewAppMsg(int iPort, ref byte pucAppMsg, ref uint puiAppMsgLen, int iIfOpen);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetBmp", CharSet = CharSet.Ansi)]
public static extern int Syn_GetBmp(int iPort, ref byte Wlt_File);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadMsg", CharSet = CharSet.Ansi)]
public static extern int Syn_ReadMsg(int iPortID, int iIfOpen, ref IDCardData pIDCardData);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadFPMsg", CharSet = CharSet.Ansi)]
public static extern int Syn_ReadFPMsg(int iPortID, int iIfOpen, ref IDCardData pIDCardData, ref byte cFPhotoname);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_FindReader", CharSet = CharSet.Ansi)]
public static extern int Syn_FindReader();
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_FindUSBReader", CharSet = CharSet.Ansi)]
public static extern int Syn_FindUSBReader();
/***********************设置附加功能函数 ************************/
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetPhotoPath", CharSet = CharSet.Ansi)]
public static extern int Syn_SetPhotoPath(int iOption, ref byte cPhotoPath);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetPhotoType", CharSet = CharSet.Ansi)]
public static extern int Syn_SetPhotoType(int iType);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetPhotoName", CharSet = CharSet.Ansi)]
public static extern int Syn_SetPhotoName(int iType);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetSexType", CharSet = CharSet.Ansi)]
public static extern int Syn_SetSexType(int iType);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetNationType", CharSet = CharSet.Ansi)]
public static extern int Syn_SetNationType(int iType);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetBornType", CharSet = CharSet.Ansi)]
public static extern int Syn_SetBornType(int iType);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetUserLifeBType", CharSet = CharSet.Ansi)]
public static extern int Syn_SetUserLifeBType(int iType);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetUserLifeEType", CharSet = CharSet.Ansi)]
public static extern int Syn_SetUserLifeEType(int iType, int iOption);
/***********************M1卡操作函数 (A16D-HF) ********************/
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_USBHIDM1Reset", CharSet = CharSet.Ansi)]
public static extern int Syn_USBHIDM1Reset(int iPort, ref uint pdwCardSN, ref byte pbSize);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_USBHIDM1AuthenKey", CharSet = CharSet.Ansi)]
public static extern int Syn_USBHIDM1AuthenKey(int iPort, byte KeyType, byte BlockNo, ref byte pKey);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_USBHIDM1ReadBlock", CharSet = CharSet.Ansi)]
public static extern int Syn_USBHIDM1ReadBlock(int iPort, byte BlockNo, ref byte pBlock);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_USBHIDM1WriteBlock", CharSet = CharSet.Ansi)]
public static extern int Syn_USBHIDM1WriteBlock(int iPort, byte BlockNo, ref byte pBlock);
[DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_USBHIDM1Halt", CharSet = CharSet.Ansi)]
public static extern int Syn_USBHIDM1Halt(int iPort); #endregion static int nPort = ;//端口 /// <summary>
/// 获取身份证信息
/// </summary>
/// <param name="CardMsg"></param>
/// <param name="errStr"></param>
/// <returns></returns>
public static bool ReadMsg(out IDCardData CardMsg, out string errStr)
{
CardMsg = new IDCardData();
errStr = "";
int nRet, iPhotoType;
string stmp;
byte[] cPath = new byte[];
byte[] pucIIN = new byte[];
byte[] pucSN = new byte[]; #region 获取读卡器
if (nPort == )
{
if (!FindDrivers(out errStr, out nPort))
{
if (string.IsNullOrEmpty(errStr))
errStr = "没有找到读卡器";
return false;
}
}
#endregion stmp = "D:\\";
cPath = System.Text.Encoding.Default.GetBytes(stmp); Syn_SetPhotoPath(, ref cPath[]); //设置照片路径 iOption 路径选项 0=C: 1=当前路径 2=指定路径
//cPhotoPath 绝对路径,仅在iOption=2时有效
iPhotoType = ;
Syn_SetPhotoType(); //0 = bmp ,1 = jpg , 2 = base64 , 3 = WLT ,4 = 不生成
Syn_SetPhotoName(); // 生成照片文件名 0=tmp 1=姓名 2=身份证号 3=姓名_身份证号 Syn_SetSexType(); // 0=卡中存储的数据 1=解释之后的数据,男、女、未知
Syn_SetNationType();// 0=卡中存储的数据 1=解释之后的数据 2=解释之后加"族"
Syn_SetBornType(); // 0=YYYYMMDD,1=YYYY年MM月DD日,2=YYYY.MM.DD,3=YYYY-MM-DD,4=YYYY/MM/DD
Syn_SetUserLifeBType(); // 0=YYYYMMDD,1=YYYY年MM月DD日,2=YYYY.MM.DD,3=YYYY-MM-DD,4=YYYY/MM/DD
Syn_SetUserLifeEType(, ); // 0=YYYYMMDD(不转换),1=YYYY年MM月DD日,2=YYYY.MM.DD,3=YYYY-MM-DD,4=YYYY/MM/DD,
// 0=长期 不转换, 1=长期转换为 有效期开始+50年
if (Syn_OpenPort(nPort) == )
{
if (Syn_SetMaxRFByte(nPort, , ) == )
{
nRet = Syn_StartFindIDCard(nPort, ref pucIIN[], );
nRet = Syn_SelectIDCard(nPort, ref pucSN[], );
nRet = Syn_ReadMsg(nPort, , ref CardMsg);
if (nRet == )
{
return true;
}
else
{
errStr = " A 读取身份证信息错误";
return false;
}
}
}
else
{
errStr = " A 打开端口失败";
return false;
}
Syn_ClosePort(nPort);
return true;
} /// <summary>
/// 获取读卡器
/// </summary>
/// <param name="error"></param>
/// <param name="nPort"></param>
/// <returns></returns>
public static bool FindDrivers(out string error, out int nPort)
{
int i = ;
error = "";
nPort = ;
try
{
i = Syn_FindReader();
}
catch (Exception e)
{
error = e.ToString();
return false;
}
if (i > )
{
nPort = i;
}
else
{
nPort = ;
return false;
}
return true;
}
}
本人用ASP.NET开发的,图片会保存到本地,需要上传到服务器端才能在网页中查看,以下是上传图片的方法
public bool UploadPhoto(string photoPath)
{
FileStream stream = new FileStream(photoPath, FileMode.Open, FileAccess.Read);
int size = Convert.ToInt32(stream.Length);
Byte[] MeaningFile = new Byte[size];
stream.Read(MeaningFile, , size);
stream.Close();
FileStream photoStream = null; string fileName = photoPath.Substring(photoPath.LastIndexOf("\\") + );
string fileExt = photoPath.Substring(photoPath.LastIndexOf(".") + );
if (fileExt.ToLower() == "bmp")//必须是bmp文件
{
try
{
string filePath = Server.MapPath("~/IDCardPhoto/");
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
if (File.Exists(Server.MapPath("~/IDCardPhoto/" + fileName)) == true) //如果存在文件
{ }
else
{
//fileName = "newFileName" + fileExt;
photoStream = new FileStream(filePath + fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
photoStream.Write(MeaningFile, , MeaningFile.Length);
photoStream.Close();
}
}
catch
{
return false;
}
finally
{
if (photoStream != null)
{
try
{
photoStream.Close();
}
catch
{
}
}
}
}
else
{ }
return true;
}