C#读卡器实现

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zhongxinhua0123/article/details/60965137

申明:该代码适用于网页形式的读卡器功能实现。
直接上代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="c_reg.aspx.cs" Inherits="zhaopin.c_reg" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">


      <object id="OCX" name="OCX" width="0" height="0" classid="CLSID:220C3AD1-5E9D-4B06-870F-E34662E2DFEA" codebase="IdrOcx.cab#version=1,0,1,2"></object>
<%--添加上面一个对象,代表引用读卡器很重要,必须申明 --% >

<%--添加下面的代码用于读取数据,身份证,姓名, --%>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="table01">

              <td height="32" align="right" class="text12" style="width: 20%">身份证:</td>
              <td width="67%" align="left">
                  <asp:TextBox ID="txt_sfz" runat="server" Width="243px" ></asp:TextBox>
                </td>
            </tr>
                   <tr>
              <td height="32" align="right" class="text12" style="width: 20%" >法人代表:</td>
              <td width="67%" align="left">
                  <asp:TextBox ID="txt_name" runat="server" Width="243px" ></asp:TextBox>
                </td>
                <td>
                            <asp:Button ID="Button1" runat="server"  Text="读卡" OnClientClick="ReadMachine()"/>
               </td>
            </tr>


      </table>
    <div class="aspNetHidden">
    <script type="text/javascript">
    var BuyerNameID = '<%=txt_name.ClientID%>';
    var BuyerIDID = '<%=txt_sfz.ClientID%>'; 
    var GT2ICROCX = document.getElementById('OCX');

    function MyGetData()//GT2ICROCX读卡成功后的回调函数
    {
        //alert("123");
        var name = document.getElementById(BuyerNameID);
        name.value = GT2ICROCX.NameL;
        var id = document.getElementById(BuyerIDID);
        id.value = GT2ICROCX.CardNo;
            if (GT2ICROCX.Sex == "1") {
                var sex = document.getElementById(ddlBuyerSexID);
                sex.value = "男";
            }
            else {
                var sex = document.getElementById(ddlBuyerSexID);
                sex.value = "女";
            }
          //  var birth = GT2ICROCX.BornL;
          //  birth = birth.replace("年", "-");
          //  birth = birth.replace("月", "-");
          //  birth = birth.replace("日", "-");
          //  F(BuyerBirthdayID).setValue(birth);
            //GT2ICROCX.Address)得到地址

    }
    function MyClearData()//GT2ICROCX读卡失败后的回调函数
    {
        alert(GT2ICROCX.ErrMsg);
    }

    function MyGetErrMsg()//GT2ICROCX读卡消息回调函数
    {
        //alert(GT2ICROCX.ErrMsg);
    }

    function ReadMachine() {
        try {

            var a = GT2ICROCX.GetState();
            if (a == -4) {
                alert("未检测到器具!");
                return;
            }

            GT2ICROCX.PhotoPath = ""
            a = GT2ICROCX.ReadCard();
            if (a == -6) {
                alert("读身份证失败!未检测到卡!");
                return;
            }

            if (a == -200) {
                alert("验证卡失败,没有找到卡或者需要将卡拿离验证区重新读卡!");
                return;
            }
        } catch (err) {
            alert("驱动未安装,或驱动不正确!");
        }
    }


</script>

<%--研腾读卡--%>
<script language="javascript" for="OCX" event="GetData">        //OCX读卡成功后的回调函数
    MyGetData();
</script>

<script language="javascript" for="OCX" event="GetErrMsg">        //OCX读卡消息回调函数
    MyGetErrMsg();
</script>

<script language="javascript" for="OCX" event="ClearData">        //OCX读卡失败后的回调函数
    MyClearData();
</script>

</div>
</asp:Content>

以上便可实现读卡功能。

展开阅读全文

没有更多推荐了,返回首页