The _c# course of ID card recognition function based on C # technology

Source: Internet
Author: User
Tags function definition pack

Recently studied C # related ORC technology, image recognition General C and C + + this low-level language to do more, C # mainly rely on some packaged components to invoke, here introduce three identification methods.

One: Call the big company API interface, Baidu, Cloud Pulse, Wen Tong technology have relevant API introduction.

Second: Call the image Processing Class library, EMGUCV is a cross-platform. NET package for OpenCV, which can also be compiled onto the mono platform and allowed to run on multiple platforms such as Windows, Mac OS, Android, IPhone, ipad, etc.

Three: Calling the Office2007 component

Document Identification API Interface

Take the API interface in aggregate data for example, because the official API does not provide C # 's invocation, the URL is as follows: Identification interface

<summary>///upload pictures///</summary>///<returns></returns> public static string Cardupload () { try {string appkey = "The site's own application key";//Configure your application for Appkey httppostedfile file = httpcontext.current.request.files[0]; string ur
L = "Http://api2.juheapi.com/cardrecon/upload";
var parameters = new dictionary<string, string> (); Parameters.
ADD ("Key", Appkey); Parameters.
ADD ("Cardtype", "2"); string result = Httppostdata (URL, 60000, "pic", file.
InputStream, parameters); Jobject info = jobject.parse (jobject.parse (result) [' result '].
ToString ());
var cardinfo = new {name = info["Name", card = info["citizenship number"]};
return Cardinfo.tojson (); catch (Exception ex) {return ex.
ToString (); }///<summary>///post invoke API///</summary>///<param name= "url" >api address </param>///<param n Ame= "TimeOut" > Access timeout </param>///<param name= "filekeyname" > File parameter name </param>///<param name= " File > Files Flow </param>///<param name= "Stringdict" &Gt; parameter list </param>///<returns> result set </returns> private static string Httppostdata (string url, int timeOut, String filekeyname, Stream file, dictionary<string, string> stringdict) {string responsecontent; var memstream = NE
W MemoryStream ();
var webRequest = (HttpWebRequest) webrequest.create (URL);
The boundary character var boundary = "---------------" + DateTime.Now.Ticks.ToString ("x");
The boundary character var beginboundary = Encoding.ASCII.GetBytes ("--" + boundary + "\ r \ n");
The last terminator var endboundary = Encoding.ASCII.GetBytes ("--" + Boundary + "--\r\n");
Set Property Webrequest.method = "POST";
Webrequest.timeout = Timeout; Webrequest.contenttype = "Multipart/form-data;
boundary= "+ boundary;
Write start boundary character Memstream.write (beginboundary, 0, beginboundary.length); Write file const string filepartheader = "Content-disposition:form-data;" Name=\ "{0}\";
Filename=\ "{1}\" \ r \ n "+" content-type:application/octet-stream\r\n\r\n "; The var header = string.
Format (Filepartheader, Filekeyname, "card.jpg"); var headerbytes = Encoding.UTF8.GetBytes (header); Memstream.write (headerbytes, 0, Headerbytes.
Length); File.
CopyTo (Memstream); Write String key var Stringkeyheader = "\r\n--" + boundary + "\r\ncontent-disposition:form-data;"
Name=\ "{0}\" "+" "\r\n\r\n{1}\r\n"; foreach (byte[] formitembytes in from string key in Stringdict.keys select String. Format (Stringkeyheader, Key, Stringdict[key]) into Formitem select Encoding.UTF8.GetBytes (Formitem)) {Memstream.write (formitembytes, 0, Formitembytes.)
Length);
//write the final end border character Memstream.write (endboundary, 0, endboundary.length);
Webrequest.contentlength = Memstream.length;
Construction completed, execute post method var requeststream = Webrequest.getrequeststream ();
memstream.position = 0;
var tempbuffer = new Byte[memstream.length];
Memstream.read (tempbuffer, 0, tempbuffer.length);
Memstream.close ();
requestStream.Write (tempbuffer, 0, tempbuffer.length);
Requeststream.close ();
var HttpWebResponse = (HttpWebResponse) webrequest.getresponse (); using (var httpstreamreader = new StreamReader (httpwebreSponse.
GetResponseStream (), encoding.getencoding ("Utf-8"))) {responsecontent = Httpstreamreader.readtoend ();}
Httpwebresponse.close ();
Webrequest.abort ();
return responsecontent; }

Second, EMGUCV class library calls

Environment construction

Download Address: EMGUCV official website

Download this exe under the file category, install it, install it in the directory and find the appropriate components, and some examples of applications.

C # to identify, need to do a picture of two value processing and OCR call related DLLs can be downloaded at my address: 360 Cloud Disk Extraction code: 89F4
DLL folder in the C # project, x64,x86,tessdata corresponding to OCR recognized class libraries and language libraries, I tessdata have added the Chinese language pack, put these three folders into the Program execution folder.

Demo

I do a small demo as shown: ID picture is downloaded from Baidu

The relevant code is as follows:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using EMGU.CV;
Using Emgu.CV.OCR;
Using Emgu.CV.Structure;
Using System.IO; namespace Imagemanage {public partial class Form1:form {image<gray, byte> imagethreshold; public Form1 () {Initi
Alizecomponent (); } private void Btn_convert_click (object sender, EventArgs e) {//The first parameter is the address of the language Pack folder and does not write defaults under the execution folder tesseract _OCR = new Tesserac
T ("", "Chi_sim", ocrenginemode.tesseractonly); _OCR.
recognize (imagethreshold); String Text = _OCR.
GetText ();
This.textBox1.Text = Text; private void PictureBox1_Click (object sender, EventArgs e) {OpenFileDialog of = new OpenFileDialog ();
Title = "Please select Picture"; if (of. ShowDialog () = = DialogResult.OK) {string file = of.
FileName;
Image img = image.fromfile (file); 
pictureBox1.Image = img;
} Bitmap Bitmap = (Bitmap) this.pictureBox1.Image; IMAGE&LT;BGR, byte> ImageSource =New IMAGE&LT;BGR, byte> (bitmap);
Image<gray, byte> Imagegrayscale = Imagesource.convert<gray, byte> ();
Imagegrayscale = Randon (Imagegrayscale);
Imagethreshold = Imagegrayscale.thresholdbinary (New Gray (), New Gray (255));
This.pictureBox2.Image = Imagethreshold.tobitmap (); ///<summary>///rotation correction///</summary>///<param name= "Imageinput" ></param>///<returns> </returns> private Image<gray, byte> Randon (Image<gray, byte> imageinput)//Image projection Rotation method Tilt Correction sub function definition {int
nwidth = Imageinput.width;
int nheight = Imageinput.height;
int sum;
int Sumofcha;
int sumofchatemp = 0;
int[] Sumhang = new Int[nheight];
Image<gray, byte> resultimage = imageinput;
Image<gray, byte> imrotaimage; Adjustment for 20 degrees (int ang = -20; ang < Ang = ang + 1) {imrotaimage = Imageinput.rotate (Ang, New Gray (1)); for (int i = 0; i < nheight; i++) {sum = 0; for (int j = 0; J < nwidth; J +) {sum + = Imrotaimage.data[i, j, 0];} sumhang[i] = Sum
} sumofcha = 0; for (int k = 0; k < nheight-1; k++) {Sumofcha = Sumofcha + (Math.Abs (sumhang[k)-sumhang[k + 1]);} if (Sumofcha & Gt
sumofchatemp) {resultimage = Imrotaimage;
Sumofchatemp = Sumofcha;
} return resultimage; }
}
}

Three, Office 2007 components

This component is free and has a high degree of recognition.

Environment construction

Office 2007 Component Modi, you need to install Ofiice2007, and because the compatibility needs to install the patch, SP1 or SP2 are all right, the patch download address is as follows:

SP1 Download address SP2 download address

After installation Control Panel--> Uninstall or update--> select office2007--> Select Change--> select Add or Repair feature--> eject the following interface and run the corresponding component.

Run the Office tool-->microsoft the tools under Office Document Imaging

Referencing a COM component in a C # project can:

If the Office component application needs to be deployed on IIS instead of a local program, you will also need to set the application pool permissions for the application as shown in the following illustration: Program application pool--> Advanced Settings--> identity

Demo

StringBuilder sb = new StringBuilder ();
MODI. Document doc = new MODI. Document ();
Doc. Create (fullfilename);
MODI. Image Image;
MODI. Layout Layout;
Doc. OCR (MODI.MiLANGUAGES.miLANG_CHINESE_SIMPLIFIED, True, true); Recognition literal type for
(int i = 0; i < Doc. Images.count; i++)
{
image = (MODI. Image) Doc. Images[i];
Layout = image. Layout;
Sb. Append (layout. Text);
}

The above is some C # to identify the identification method, according to the different needs of their own projects to choose.

Related Article

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.