Share the. Net program to read the second-generation ID Card (with source code)

Source: Internet
Author: User

Generally, the winform application is easy to solve this problem, and the Web application is a little more troublesome.

Here I will talk about my solution:

1. You need to have a resident ID card reader (with drive disc) for the connected model. Here I use the idr200 of jinglun Electronics Company and provide the development interface description for this reader.

2. Create a winform Control Project readcardcontrol and add a primary class readcard

1. Declare the DLL entry

[Dllimport ("sdtapi. dll")]

Private Static extern int initcomm (INT iport); // Initialization

[Dllimport ("sdtapi. dll")]

Private Static extern int authenticate (); // card authentication

[Dllimport ("sdtapi. dll")]

Private Static extern int readbaseinfos (stringbuilder name, stringbuilder gender, stringbuilder folk, stringbuilder birthday, stringbuilder code, stringbuilder address, stringbuilder agency, stringbuilder expirestart, stringbuilder interval); // read data, recommended

[Dllimport ("sdtapi. dll")]

Private Static extern int closecomm (); // close the port

[Dllimport ("sdtapi. dll")]

Private Static extern int readbasemsg (byte [] PMSG, ref int Len); // read data, not required here

[Dllimport ("sdtapi. dll")]

Private Static extern int readbasemsgw (byte [] PMSG, ref int Len); // read data, not needed here

[Dllimport ("kernel32.dll")]

Private Static extern int BEEP (INT dwfreq, int dwduration); // used to scream

2. card reading Method

Private string [] arrys = NULL; // The array used to save the ID card information

Public void ontimer ()

{

Stringbuilder name = new stringbuilder (31 );

Stringbuilder gender = new stringbuilder (3 );

Stringbuilder folk = new stringbuilder (10 );

Stringbuilder Birthday = new stringbuilder (9 );

Stringbuilder code = new stringbuilder (19 );

Stringbuilder address = new stringbuilder (71 );

Stringbuilder agency = new stringbuilder (31 );

Stringbuilder expirestart = new stringbuilder (9 );

Stringbuilder expireend = new stringbuilder (9 );

// Int Len = 0;

// String [] temp;

Char [] Param = {'\ 0 '};

Byte [] PMSG = new byte [1, 256];

String [] baseinfo = new string [9];

 

// Open the port

Int bytes penret = initcomm (1001 );

If (else penret! = 1)

{

// Settext ("the reading machine is not connected", lblmsg );

Return;

}

// Card authentication

Int intreadret = authenticate ();

If (intreadret! = 1)

{

// Settext ("failed card authentication", lblmsg );

Closecomm ();

Return;

}

// Readbaseinfos (recommended)

Int intreadbaseinfosret = readbaseinfos (name, gender, folk, birthday, code, address, agency, expirestart, expireend );

If (intreadbaseinfosret! = 1)

{

// Settext ("failed to read the card", lblmsg );

Closecomm ();

Return;

}

Beep (2047,200 );

Arrys = new string [10];

Arrys [0] = code. tostring (). Trim ();

Arrys [1] = Name. tostring (). Trim ();

Arrys [2] = gender. tostring (). Trim ();

Arrys [3] = folk. tostring (). Trim ();

Arrys [4] = birthday. tostring (). Trim ();

Arrys [5] = address. tostring (). Trim ();

Arrys [6] = agency. tostring (). Trim ();

Arrys [7] = expirestart. tostring (). Trim ();

Arrys [8] = expireend. tostring (). Trim ();

Arrys [9] = system. Io. Directory. getcurrentdirectory () + "\ photo.bmp ";

// Settext ("card reading succeeded", lblmsg );

// Settext ("document number:" + code. tostring (), label1 );

// Settext ("name:" + name. tostring (), label2 );

// Settext ("Gender:" + gender. tostring (), label3 );

// Settext ("Nationality:" + folk. tostring (), label4 );

// Settext ("Date of Birth:" + birthday. tostring (), label5 );

// Settext ("Address:" + address. tostring (), label6 );

// Settext ("Issuing Authority:" + agency. tostring (), label7 );

// Settext ("issue time:" + expirestart. tostring (), label8 );

// Settext ("effective end time:" + expireend. tostring (), label9 );

Setimage ("photo.bmp", picturebox1 );

Closecomm ();

}

2. Declare the method test called by a client (arrays are not directly used here)

Public String test ()

{

String STR = "";

Foreach (string s in arrys)

{

STR + = S + ",";

}

STR = Str. trimend (',');

Return STR;

}

3. encapsulate the control into an ActiveX component

Add the header in the readcard class

[GUID ("B9BB196C-5008-4156-840F-9FE37BA81502")]

To access server data from a client, you must implement the iobjectsafe interface.

The interface declaration is as follows:

[Comimport, guidattribute ("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]

[Interfacetypeattribute (cominterfacetype. interfaceisiunknown)]

Public interface iobjectsafe

{

[Preservesig]

Void getinterfacesafeoptions (ref guid ID, [financialas (unmanagedtype. U4)] ref int pdwsupportedoptions, [financialas (unmanagedtype. U4)] ref int pdwenabledoptions );

 

[Preservesig ()]

Void setinterfacesafeoptions (ref guid ID, [financialas (unmanagedtype. U4)] int dwoptionsetmask, [externalas (unmanagedtype. U4)] int dwenabledoptions );

}

 

Uidesign

/// <Summary> /// required designer variables. /// </Summary> private system. componentmodel. icontainer components = NULL;
/// <Summary> /// clear all resources in use. /// </Summary> /// <Param name = "disposing"> true if the managed resource should be released; otherwise, false. </Param> protected override void dispose (bool disposing) {If (disposing & (components! = NULL) {components. Dispose ();} base. Dispose (disposing );}
# Code generated by the region component designer
/// <Summary> /// the designer supports the required methods-do not // use the code editor to modify the content of this method. /// </Summary> private void initializecomponent () {This. picturebox1 = new system. windows. forms. picturebox (); (system. componentmodel. isupportinitialize) (this. picturebox1 )). begininit (); this. suspendlayout (); // picturebox1 // This. picturebox1.location = new system. drawing. point (3, 3); this. picturebox1.name = "picturebox1"; this. picturebox1.size = new system. drawing. size (100,130); this. picturebox1.tabindex = 12; this. picturebox1.tabstop = false; // readcard // This. autoscaledimensions = new system. drawing. sizef (6f, 12f); this. autoscalemode = system. windows. forms. autoscalemode. font; this. controls. add (this. picturebox1); this. name = "readcard"; this. size = new system. drawing. size (107,135); (system. componentmodel. isupportinitialize) (this. picturebox1 )). endinit (); this. resumelayout (false );
}
# Endregion
Private system. Windows. Forms. picturebox picturebox1;

 

4. Create a web application project

1. interface:

<Table Style = "width: 100%; "Border =" 1 "width =" 100% "bordercolordark =" # ffffff "cellspacing =" 0 "cellpadding =" 2 "bordercolor =" #000000 ">

<Tr>

<TD style = "width: 33.3%; text-align: right;"> credential number: </TD>

<TD style = "width: 33.3%" id = "TD1"> & nbsp; </TD>

<TD style = "width: 33.3%; text-align: Left;" rowspan = "6">

<Object ID = "csharpactivex" name = "csharpactivex" classid = "CLSID: B9BB196C-5008-4156-840F-9FE37BA81502"> </Object> </TD>

</Tr>

<Tr>

<TD style = "text-align: Right"> name: </TD>

<TD id = "td2"> & nbsp; </TD>

</Tr>

<Tr>

<TD style = "text-align: Right"> Gender: </TD>

<TD id = "td3"> & nbsp; </TD>

</Tr>

<Tr>

<TD style = "text-align: Right"> Nationality: </TD>

<TD id = "td4"> & nbsp; </TD>

</Tr>

<Tr>

<TD style = "text-align: Right"> Date of Birth: </TD>

<TD id = "td5"> & nbsp; </TD>

</Tr>

<Tr>

<TD style = "text-align: Right"> address: </TD>

<TD id = "td6"> & nbsp; </TD>

</Tr>

<Tr>

<TD style = "text-align: Right"> issuing authority: </TD>

<TD id = "td7"> & nbsp; </TD>

<TD> & nbsp; </TD>

</Tr>

<Tr>

<TD style = "text-align: Right"> Issue Date: </TD>

<TD id = "td8"> & nbsp; </TD>

<TD> & nbsp; </TD>

</Tr>

<Tr>

<TD style = "text-align: Right"> validity deadline: </TD>

<TD id = "td9"> & nbsp; </TD>

<TD> & nbsp; </TD>

</Tr>

 

</Table>

 

2. JavaScript code:

Function demo (){

Try {

VaR x = Document. getelementbyid ("csharpactivex ");

X. ontimer (); // read the image

VaR v = x. Test (); // read other ID card information

VaR arrys = V. Split (',');

If (arrys. length> 9 ){

Document. getelementbyid ("TD1"). innerhtml = arrys [0];

Document. getelementbyid ("td2"). innerhtml = arrys [1];

Document. getelementbyid ("td3"). innerhtml = arrys [2];

Document. getelementbyid ("td4"). innerhtml = arrys [3];

Document. getelementbyid ("td5"). innerhtml = arrys [4];

Document. getelementbyid ("td6"). innerhtml = arrys [5];

Document. getelementbyid ("td7"). innerhtml = arrys [6];

Document. getelementbyid ("td8"). innerhtml = arrys [7];

Document. getelementbyid ("td9"). innerhtml = arrys [8];

}

}

Catch (e ){

// Alert (E. Message)

}

}

Setinterval (demo, 1000); // you can specify to read data every 1 second.

Success...Download source code

Cut:

 

Make a little progress every day...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.