问题一
解决方法:
通常我把所有的dll复制到system32文件夹,64位系统复制到sysWOW64文件夹,而且编译选项CPU要选择X86
问题二
解决方法:
图片路径默认是C:\,对C:\没有写的权限,所以提示此错误。改变此路径到别的盘符就可以解决。
以下是我编写的代码
1 public class IDCardHelper 2 { 3 #region 数据类型和导入接口 4 5 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] 6 public struct IDCardData 7 { 8 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] 9 public string Name; //姓名 10 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)] 11 public string Sex; //性别 12 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)] 13 public string Nation; //名族 14 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 18)] 15 public string Born; //出生日期 16 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 72)] 17 public string Address; //住址 18 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 38)] 19 public string IDCardNo; //身份证号 20 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] 21 public string GrantDept; //发证机关 22 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 18)] 23 public string UserLifeBegin; // 有效开始日期 24 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 18)] 25 public string UserLifeEnd; // 有效截止日期 26 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 38)] 27 public string reserved; // 保留 28 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)] 29 public string PhotoFileName; // 照片路径 30 } 31 /************************端口类API *************************/ 32 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetCOMBaud", CharSet = CharSet.Ansi)] 33 public static extern int Syn_GetCOMBaud(int iPort, ref uint puiBaudRate); 34 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetCOMBaud", CharSet = CharSet.Ansi)] 35 public static extern int Syn_SetCOMBaud(int iPort, uint uiCurrBaud, uint uiSetBaud); 36 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_OpenPort", CharSet = CharSet.Ansi)] 37 public static extern int Syn_OpenPort(int iPort); 38 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ClosePort", CharSet = CharSet.Ansi)] 39 public static extern int Syn_ClosePort(int iPort); 40 /**************************SAM类函数 **************************/ 41 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetMaxRFByte", CharSet = CharSet.Ansi)] 42 public static extern int Syn_SetMaxRFByte(int iPort, byte ucByte, int iIfOpen); 43 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ResetSAM", CharSet = CharSet.Ansi)] 44 public static extern int Syn_ResetSAM(int iPort, int iIfOpen); 45 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetSAMStatus", CharSet = CharSet.Ansi)] 46 public static extern int Syn_GetSAMStatus(int iPort, int iIfOpen); 47 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetSAMID", CharSet = CharSet.Ansi)] 48 public static extern int Syn_GetSAMID(int iPort, ref byte pucSAMID, int iIfOpen); 49 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetSAMIDToStr", CharSet = CharSet.Ansi)] 50 public static extern int Syn_GetSAMIDToStr(int iPort, ref byte pcSAMID, int iIfOpen); 51 /*************************身份证卡类函数 ***************************/ 52 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_StartFindIDCard", CharSet = CharSet.Ansi)] 53 public static extern int Syn_StartFindIDCard(int iPort, ref byte pucIIN, int iIfOpen); 54 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SelectIDCard", CharSet = CharSet.Ansi)] 55 public static extern int Syn_SelectIDCard(int iPort, ref byte pucSN, int iIfOpen); 56 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadBaseMsg", CharSet = CharSet.Ansi)] 57 public static extern int Syn_ReadBaseMsg(int iPort, ref byte pucCHMsg, ref uint puiCHMsgLen, ref byte pucPHMsg, ref uint puiPHMsgLen, int iIfOpen); 58 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadBaseMsgToFile", CharSet = CharSet.Ansi)] 59 public static extern int Syn_ReadBaseMsgToFile(int iPort, ref byte pcCHMsgFileName, ref uint puiCHMsgFileLen, ref byte pcPHMsgFileName, ref uint puiPHMsgFileLen, int iIfOpen); 60 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadBaseFPMsg", CharSet = CharSet.Ansi)] 61 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); 62 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadBaseFPMsgToFile", CharSet = CharSet.Ansi)] 63 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); 64 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadNewAppMsg", CharSet = CharSet.Ansi)] 65 public static extern int Syn_ReadNewAppMsg(int iPort, ref byte pucAppMsg, ref uint puiAppMsgLen, int iIfOpen); 66 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetBmp", CharSet = CharSet.Ansi)] 67 public static extern int Syn_GetBmp(int iPort, ref byte Wlt_File); 68 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadMsg", CharSet = CharSet.Ansi)] 69 public static extern int Syn_ReadMsg(int iPortID, int iIfOpen, ref IDCardData pIDCardData); 70 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadFPMsg", CharSet = CharSet.Ansi)] 71 public static extern int Syn_ReadFPMsg(int iPortID, int iIfOpen, ref IDCardData pIDCardData, ref byte cFPhotoname); 72 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_FindReader", CharSet = CharSet.Ansi)] 73 public static extern int Syn_FindReader(); 74 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_FindUSBReader", CharSet = CharSet.Ansi)] 75 public static extern int Syn_FindUSBReader(); 76 /***********************设置附加功能函数 ************************/ 77 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetPhotoPath", CharSet = CharSet.Ansi)] 78 public static extern int Syn_SetPhotoPath(int iOption, ref byte cPhotoPath); 79 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetPhotoType", CharSet = CharSet.Ansi)] 80 public static extern int Syn_SetPhotoType(int iType); 81 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetPhotoName", CharSet = CharSet.Ansi)] 82 public static extern int Syn_SetPhotoName(int iType); 83 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetSexType", CharSet = CharSet.Ansi)] 84 public static extern int Syn_SetSexType(int iType); 85 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetNationType", CharSet = CharSet.Ansi)] 86 public static extern int Syn_SetNationType(int iType); 87 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetBornType", CharSet = CharSet.Ansi)] 88 public static extern int Syn_SetBornType(int iType); 89 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetUserLifeBType", CharSet = CharSet.Ansi)] 90 public static extern int Syn_SetUserLifeBType(int iType); 91 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetUserLifeEType", CharSet = CharSet.Ansi)] 92 public static extern int Syn_SetUserLifeEType(int iType, int iOption); 93 /***********************M1卡操作函数 (A16D-HF) ********************/ 94 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_USBHIDM1Reset", CharSet = CharSet.Ansi)] 95 public static extern int Syn_USBHIDM1Reset(int iPort, ref uint pdwCardSN, ref byte pbSize); 96 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_USBHIDM1AuthenKey", CharSet = CharSet.Ansi)] 97 public static extern int Syn_USBHIDM1AuthenKey(int iPort, byte KeyType, byte BlockNo, ref byte pKey); 98 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_USBHIDM1ReadBlock", CharSet = CharSet.Ansi)] 99 public static extern int Syn_USBHIDM1ReadBlock(int iPort, byte BlockNo, ref byte pBlock); 100 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_USBHIDM1WriteBlock", CharSet = CharSet.Ansi)] 101 public static extern int Syn_USBHIDM1WriteBlock(int iPort, byte BlockNo, ref byte pBlock); 102 [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_USBHIDM1Halt", CharSet = CharSet.Ansi)] 103 public static extern int Syn_USBHIDM1Halt(int iPort); 104 105 #endregion 106 107 static int nPort = 0;//端口 108 109 /// <summary> 110 /// 获取身份证信息 111 /// </summary> 112 /// <param name="CardMsg"></param> 113 /// <param name="errStr"></param> 114 /// <returns></returns> 115 public static bool ReadMsg(out IDCardData CardMsg, out string errStr) 116 { 117 CardMsg = new IDCardData(); 118 errStr = ""; 119 int nRet, iPhotoType; 120 string stmp; 121 byte[] cPath = new byte[255]; 122 byte[] pucIIN = new byte[4]; 123 byte[] pucSN = new byte[8]; 124 125 #region 获取读卡器 126 if (nPort == 0) 127 { 128 if (!FindDrivers(out errStr, out nPort)) 129 { 130 if (string.IsNullOrEmpty(errStr)) 131 errStr = "没有找到读卡器"; 132 return false; 133 } 134 } 135 #endregion 136 137 stmp = "D:\\"; 138 cPath = System.Text.Encoding.Default.GetBytes(stmp); 139 140 Syn_SetPhotoPath(2, ref cPath[0]); //设置照片路径 iOption 路径选项 0=C: 1=当前路径 2=指定路径 141 //cPhotoPath 绝对路径,仅在iOption=2时有效 142 iPhotoType = 0; 143 Syn_SetPhotoType(0); //0 = bmp ,1 = jpg , 2 = base64 , 3 = WLT ,4 = 不生成 144 Syn_SetPhotoName(2); // 生成照片文件名 0=tmp 1=姓名 2=身份证号 3=姓名_身份证号 145 146 Syn_SetSexType(1); // 0=卡中存储的数据 1=解释之后的数据,男、女、未知 147 Syn_SetNationType(1);// 0=卡中存储的数据 1=解释之后的数据 2=解释之后加"族" 148 Syn_SetBornType(3); // 0=YYYYMMDD,1=YYYY年MM月DD日,2=YYYY.MM.DD,3=YYYY-MM-DD,4=YYYY/MM/DD 149 Syn_SetUserLifeBType(1); // 0=YYYYMMDD,1=YYYY年MM月DD日,2=YYYY.MM.DD,3=YYYY-MM-DD,4=YYYY/MM/DD 150 Syn_SetUserLifeEType(1, 1); // 0=YYYYMMDD(不转换),1=YYYY年MM月DD日,2=YYYY.MM.DD,3=YYYY-MM-DD,4=YYYY/MM/DD, 151 // 0=长期 不转换, 1=长期转换为 有效期开始+50年 152 if (Syn_OpenPort(nPort) == 0) 153 { 154 if (Syn_SetMaxRFByte(nPort, 80, 0) == 0) 155 { 156 nRet = Syn_StartFindIDCard(nPort, ref pucIIN[0], 0); 157 nRet = Syn_SelectIDCard(nPort, ref pucSN[0], 0); 158 nRet = Syn_ReadMsg(nPort, 0, ref CardMsg); 159 if (nRet == 0) 160 { 161 return true; 162 } 163 else 164 { 165 errStr = " A 读取身份证信息错误"; 166 return false; 167 } 168 } 169 } 170 else 171 { 172 errStr = " A 打开端口失败"; 173 return false; 174 } 175 Syn_ClosePort(nPort); 176 return true; 177 } 178 179 /// <summary> 180 /// 获取读卡器 181 /// </summary> 182 /// <param name="error"></param> 183 /// <param name="nPort"></param> 184 /// <returns></returns> 185 public static bool FindDrivers(out string error, out int nPort) 186 { 187 int i = 0; 188 error = ""; 189 nPort = 0; 190 try 191 { 192 i = Syn_FindReader(); 193 } 194 catch (Exception e) 195 { 196 error = e.ToString(); 197 return false; 198 } 199 if (i > 0) 200 { 201 nPort = i; 202 } 203 else 204 { 205 nPort = 0; 206 return false; 207 } 208 return true; 209 } 210 }
本人用ASP.NET开发的,图片会保存到本地,需要上传到服务器端才能在网页中查看,以下是上传图片的方法
1 public bool UploadPhoto(string photoPath) 2 { 3 FileStream stream = new FileStream(photoPath, FileMode.Open, FileAccess.Read); 4 int size = Convert.ToInt32(stream.Length); 5 Byte[] MeaningFile = new Byte[size]; 6 stream.Read(MeaningFile, 0, size); 7 stream.Close(); 8 FileStream photoStream = null; 9 10 string fileName = photoPath.Substring(photoPath.LastIndexOf("\\") + 1); 11 string fileExt = photoPath.Substring(photoPath.LastIndexOf(".") + 1); 12 if (fileExt.ToLower() == "bmp")//必须是bmp文件 13 { 14 try 15 { 16 string filePath = Server.MapPath("~/IDCardPhoto/"); 17 if (!Directory.Exists(filePath)) 18 { 19 Directory.CreateDirectory(filePath); 20 } 21 if (File.Exists(Server.MapPath("~/IDCardPhoto/" + fileName)) == true) //如果存在文件 22 { 23 24 } 25 else 26 { 27 //fileName = "newFileName" + fileExt; 28 photoStream = new FileStream(filePath + fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); 29 photoStream.Write(MeaningFile, 0, MeaningFile.Length); 30 photoStream.Close(); 31 } 32 } 33 catch 34 { 35 return false; 36 } 37 finally 38 { 39 if (photoStream != null) 40 { 41 try 42 { 43 photoStream.Close(); 44 } 45 catch 46 { 47 } 48 } 49 } 50 } 51 else 52 { 53 54 } 55 return true; 56 }