1、思路:在使用浏览器的电脑上装一个websocket服务端。通过读取浏览器的操作通知给服务端,服务端触发读取身份证机制。
2、服务端处理完通知给浏览器,这样就实现了任何浏览器都可以读取身份证。
具体代码如下:
浏览器端:
服务端代码如下:
1、因为要做windows服务需要安装Topshelf;
2、因为要做websocket需要nuget包Fleck,为啥选择该包,因为该websocket遇到错误可以自己重新启动。
3、控制台程序Program.cs代码如下
using Fleck;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Topshelf;
namespace IDCardReader
{
class Program
{
private static NLog.Logger logger = NLog.LogManager.GetLogger("websocketserver");
static void Main(string[] args)
{
try
{
var rc = HostFactory.Run(x =>
{
x.Service(s =>
{
s.ConstructUsing(name => new Program());
s.WhenStarted(p => p.Start());
s.WhenStopped(p => p.Stop());
});
x.RunAsLocalSystem();
x.SetServiceName("MyShenIDCardServices");
x.SetDisplayName("身份证读卡器插件");
});
var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode());
Environment.ExitCode = exitCode;
}
catch (Exception ex)
{
logger.Error(ex.Message);
}
}
///
/// 服务启动
///
void Start()
{
try
{
Task.Run(() =>
{
var allSockets = new List();
var server = new WebSocketServer("ws://0.0.0.0:" + 8888");
//出错后进行重启
server.RestartAfterListenError = true;
server.Start(socket =>
{
socket.OnOpen = () =>
{
string IpAddress = socket.ConnectionInfo.ClientIpAddress;
int Port = socket.ConnectionInfo.ClientPort;
allSockets.Add(socket);
Console.WriteLine("\r\n" + System.DateTime.Now + " [客户端\"" + IpAddress + ":" + Port + "\"建立连接成功! 客户端数量:" + allSockets.Count + "]");
};
socket.OnClose = () =>
{
string IpAddress = socket.ConnectionInfo.ClientIpAddress;
int Port = socket.ConnectionInfo.ClientPort;
allSockets.Remove(socket);
Console.WriteLine("\r\n" + System.DateTime.Now + " [客户端\"" + IpAddress + ":" + Port + "\"已经中断连接! 客户端数量:" + allSockets.Count + "]");
};
socket.OnMessage = message =>
{
string IpAddress = socket.ConnectionInfo.ClientIpAddress;
int Port = socket.ConnectionInfo.ClientPort;
Console.WriteLine("\r\n" + System.DateTime.Now + " [客户端\"" + IpAddress + ":" + Port + " 发送信息: " + message);
switch (message)
{
//case "SDT_ClearZP#":
// allSockets.ToList().ForEach(s =>
// {
// if (s == socket)
// {
// s.Send("123");
// }
// });
// break;
//case "SDT_OpenReader#": //打开连接
// HuaShiIDCardReader.OpenReader();
// allSockets.ToList().ForEach(s =>
// {
// if (s == socket)
// {
// s.Send("1");
// }
// });
// break;
case "SDT_ReadCard#": //读卡
allSockets.ToList().ForEach(
s =>
{
if (s == socket)
{
s.Send(HuaShiIDCardReader.ReadCard());
}
});
string path = System.AppDomain.CurrentDomain.BaseDirectory;
File.Delete(path + "wz.txt");
File.Delete(path + "zp.bmp");
break;
default:
break;
}
};
socket.OnError = error =>
{
string IpAddress = socket.ConnectionInfo.ClientIpAddress;
int Port = socket.ConnectionInfo.ClientPort;
allSockets.Remove(socket);
Console.WriteLine("\r\n" + System.DateTime.Now + " [客户端\"" + IpAddress + ":" + Port + "\"已经中断连接! 客户端数量:" + allSockets.Count + "]");
};
});
});
}
catch (Exception ex)
{
logger.Error(ex.Message);
}
}
///
/// 服务停止
///
void Stop()
{
logger.Error("停止");
}
}
}
4、对接的是华视读卡器HuaShiIDCardReader.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace IDCardReader
{
public class HuaShiIDCardReader
{
//初始化
static int iRetUSB = 0;
public static void OpenReader()
{
try
{
if(iRetUSB!=1)
{
int iPort = 0;
for (iPort = 1001; iPort <= 1016; iPort++)
{
iRetUSB = CVRSDK.CVR_InitComm(iPort);
if (iRetUSB == 1)
{
break;
}
}
}
}
catch (Exception)
{
}
}
//public static string ReadCard()
//{
// try
// {
// if (iRetUSB == 1)
// {
// this.labelOpResult.Text = "初始化成功!";
// }
// else
// {
// this.labelOpResult.Text = "初始化失败!";
// }
// int authenticate = CVRSDK.CVR_Authenticate();
// if (authenticate == 1)
// {
// int readContent = CVRSDK.CVR_Read_FPContent();
// if (readContent == 1)
// {
// this.labelOpResult.Text = "读卡操作成功!";
// FillData();
// }
// else
// {
// this.labelOpResult.Text = "读卡操作失败!";
// }
// }
// else
// {
// MessageBox.Show("未放卡或卡片放置不正确");
// }
// CVRSDK.CVR_CloseComm();
// }
// catch (Exception ex)
// {
// MessageBox.Show(ex.ToString());
// }
// string msg = "";
// try
// {
// if (iRetUSB == 1)
// {
// msg = getUserInfo().ToString();//读取身份证
// }
// else
// {
// msg = "5";// "初始化失败!";
// }
// }
// catch (Exception ex)
// {
// }
// return msg;
//}
public static string ReadCard()
{
string name = "";
try
{
int iPort, iRetUSB = 0;
for (iPort = 1001; iPort <= 1016; iPort++)
{
iRetUSB = CVRSDK.CVR_InitComm(iPort);
if (iRetUSB == 1)
{
break;
}
}
if (iRetUSB != 1)
{
return "3";
}
int authenticate = CVRSDK.CVR_Authenticate();
if (authenticate == 1)
{
int readContent = CVRSDK.CVR_Read_FPContent();
if (readContent == 1)
{
byte[] Sname = new byte[30];
int length = 30;
CVRSDK.GetPeopleName(ref Sname[0], ref length);
//MessageBox.Show();
byte[] number = new byte[36];
length = 36;
CVRSDK.GetPeopleIDCode(ref number[0], ref length);
name = System.Text.Encoding.GetEncoding("GB2312").GetString(Sname).Replace("\0", "").Trim() + "|" +
System.Text.Encoding.GetEncoding("GB2312").GetString(number).Replace("\0", "").Trim();
}
else
{
name = "5";//"读卡操作失败!";
}
}
else
{
name = "4";//name="未放卡或卡片放置不正确";
}
}
catch (Exception ex)
{
name = "0";
}
finally
{
CloseCard();
}
return name;
}
private static void CloseCard()
{
int C = CVRSDK.CVR_CloseComm();
}
}
}
4、读取引入dll类文件,CVRSDK.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Runtime.InteropServices;//这是用到DllImport时候要引入的包
namespace IDCardReader
{
///
/// 身份证阅读类
///
class CVRSDK
{
[DllImport("Termb.dll", EntryPoint = "CVR_InitComm", CharSet = CharSet.Ansi, SetLastError = false)]
public static extern int CVR_InitComm(int Port);//声明外部的标准动态库, 跟Win32API是一样的
[DllImport("Termb.dll", EntryPoint = "CVR_Authenticate", CharSet = CharSet.Ansi, SetLastError = false)]
public static extern int CVR_Authenticate();
[DllImport("Termb.dll", EntryPoint = "CVR_Read_Content", CharSet = CharSet.Ansi, SetLastError = false)]
public static extern int CVR_Read_Content(int Active);
[DllImport("Termb.dll", EntryPoint = "CVR_Read_FPContent", CharSet = CharSet.Ansi, SetLastError = false)]
public static extern int CVR_Read_FPContent();
[DllImport("Termb.dll", EntryPoint = "CVR_FindCard", CharSet = CharSet.Ansi, SetLastError = false)]
public static extern int CVR_FindCard();
[DllImport("Termb.dll", EntryPoint = "CVR_SelectCard", CharSet = CharSet.Ansi, SetLastError = false)]
public static extern int CVR_SelectCard();
[DllImport("Termb.dll", EntryPoint = "CVR_CloseComm", CharSet = CharSet.Ansi, SetLastError = false)]
public static extern int CVR_CloseComm();
[DllImport("Termb.dll", EntryPoint = "GetCertType", CharSet = CharSet.Ansi, SetLastError = false)]
public static extern unsafe int GetCertType(ref byte strTmp, ref int strLen);
[DllImport("Termb.dll", EntryPoint = "GetPeopleName", CharSet = CharSet.Ansi, SetLastError = false)]
public static extern unsafe int GetPeopleName(ref byte strTmp, ref int strLen);
[DllImport("Termb.dll", EntryPoint = "GetPeopleChineseName", CharSet = CharSet.Ansi, SetLastError = false)]
public static extern unsafe int GetPeopleChineseName(ref byte strTmp, ref int strLen);
[DllImport("Termb.dll", EntryPoint = "GetPeopleNation", CharSet = CharSet.Ansi, SetLastError = false)]
public static extern int GetPeopleNation(ref byte strTmp, ref int strLen);
[DllImport("Termb.dll", EntryPoint = "GetNationCode", CharSet = CharSet.Ansi, SetLastError = false)]
public static extern int GetNationCode(ref byte strTmp, ref int strLen);
[DllImport("Termb.dll", EntryPoint = "GetPeopleBirthday", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
public static extern int GetPeopleBirthday(ref byte strTmp, ref int strLen);
[DllImport("Termb.dll", EntryPoint = "GetPeopleAddress", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
public static extern int GetPeopleAddress(ref byte strTmp, ref int strLen);
[DllImport("Termb.dll", EntryPoint = "GetPeopleIDCode", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
public static extern int GetPeopleIDCode(ref byte strTmp, ref int strLen);
[DllImport("Termb.dll", EntryPoint = "GetDepartment", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
public static extern int GetDepartment(ref byte strTmp, ref int strLen);
[DllImport("Termb.dll", EntryPoint = "GetStartDate", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
public static extern int GetStartDate(ref byte strTmp, ref int strLen);
[DllImport("Termb.dll", EntryPoint = "GetEndDate", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
public static extern int GetEndDate(ref byte strTmp, ref int strLen);
[DllImport("Termb.dll", EntryPoint = "GetPeopleSex", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
public static extern int GetPeopleSex(ref byte strTmp, ref int strLen);
[DllImport("Termb.dll", EntryPoint = "CVR_GetSAMID", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
public static extern int CVR_GetSAMID(ref byte strTmp);
[DllImport("Termb.dll", EntryPoint = "GetBMPData", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
public static extern int GetBMPData(ref byte btBmp, ref int nLen);
[DllImport("Termb.dll", EntryPoint = "GetJpgData", CharSet = CharSet.Ansi, SetLastError = false, CallingConvention = CallingConvention.StdCall)]
public static extern int GetJpgData(ref byte btBmp, ref int nLen);
}
}
源码地址:https://download.csdn.net/download/IT_ziliang/12477569