Home Backend Development C#.Net Tutorial Detailed explanation of graphic and text code for realizing ID card recognition function in C#

Detailed explanation of graphic and text code for realizing ID card recognition function in C#

Jul 17, 2017 am 11:02 AM
.net ID card

This article mainly introduces the detailed explanation of C# ID card recognition related technologies, which has certain reference value. Interested friends can refer to

Recent research on C# related OCR technology, image recognition is generally C Compared with low-level languages ​​​​such as C++, C# mainly relies on some encapsulated components to make calls. Here is a method of ID card recognition.

Environment setup

Download address: EmguCV official website

Download this EXE under the File category and proceedInstallation After installation, you can find the corresponding components in the directory, as well as some application cases.

The dll in the dll folder is referenced to the C# project. x64, x86, and tessdata correspond to the class library and language library recognized by OCR. I have added a Chinese language package to my tessdata. The three folders are placed in the program execution folder.

Demo

The small Demo I made is as shown below: ID cardPicture is downloaded from Baidu

I have to say that the only drawback of this class library is that the text recognition rate is too low, and the image recognition effect is not very good

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.OCR;
using Emgu.CV.Structure;
using System.IO;

namespace EmguCV
{
 public partial class Form1 : Form
 {
  Image<Gray, Byte> imageThreshold;
  public Form1()
  {
   InitializeComponent();
   pictureBox1.Enabled = false;
  }

  private void Form1_Load(object sender, EventArgs e)
  {



  }

  private void button1_Click(object sender, EventArgs e)
  {
   //第一个参数是语言包文件夹的地址,不写默认在执行文件夹下
   Tesseract _ocr = new Tesseract(@"", "chi_sim", OcrEngineMode.TesseractOnly);
   _ocr.SetImage(imageThreshold);
   _ocr.Recognize();
   String text = _ocr.GetUTF8Text();
   this.textBox1.Text = text;
  }

  private void pictureBox2_Click(object sender, EventArgs e)
  {
   OpenFileDialog of = new OpenFileDialog();
   of.Title = "请选择图片";
   if (of.ShowDialog() == DialogResult.OK)
   {
    string file = of.FileName;
    Image img = Image.FromFile(file);
    pictureBox1.Image = img;
   }
   Bitmap bitmap = (Bitmap)this.pictureBox1.Image;
   Image<Bgr, Byte> imageSource = new Image<Bgr, byte>(bitmap);
   Image<Gray, Byte> imageGrayscale = imageSource.Convert<Gray, Byte>();
   imageGrayscale = randon(imageGrayscale);
   imageThreshold = imageGrayscale.ThresholdBinary(new Gray(100), new Gray(255));
   this.pictureBox2.Image = imageThreshold.ToBitmap();
  }
  /// <summary>
  /// 旋转校正
  /// </summary>
  /// <param name="imageInput"></param>
  /// <returns></returns>
  private Image<Gray, Byte> randon(Image<Gray, Byte> imageInput)//图像投影旋转法倾斜校正子函数定义
  {
   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;
   //20度范围内的调整
   for (int ang = -20; ang < 20; 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 > SumOfChatemp)
    {
     resultImage = ImrotaImage;
     SumOfChatemp = SumOfCha;
    }
   }
   return resultImage;
  }

  private void pictureBox1_Click(object sender, EventArgs e)
  {

  }
 }
}
Copy after login

The above is the detailed content of Detailed explanation of graphic and text code for realizing ID card recognition function in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

One ID card can open several Taobao stores One ID card can open several Taobao stores Oct 10, 2023 pm 01:51 PM

An ID card can only open one Taobao store. Sellers should abide by the regulations of the Taobao platform and do not try to use other people's ID cards to open multiple stores. Sellers can expand their business and increase sales by merging stores and opening branches. However, it should be noted that each branch needs to use an independent ID card for real-name authentication.

What are the employment prospects of C#? What are the employment prospects of C#? Oct 19, 2023 am 11:02 AM

Whether you are a beginner or an experienced professional, mastering C# will pave the way for your career.

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

The development of artificial intelligence (AI) technologies is in full swing today, and they have shown great potential and influence in various fields. Today Dayao will share with you 4 .NET open source AI model LLM related project frameworks, hoping to provide you with some reference. https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.mdSemanticKernelSemanticKernel is an open source software development kit (SDK) designed to integrate large language models (LLM) such as OpenAI, Azure

.NET performance optimization technology for developers .NET performance optimization technology for developers Sep 12, 2023 am 10:43 AM

If you are a .NET developer, you must be aware of the importance of optimizing functionality and performance in delivering high-quality software. By making expert use of the provided resources and reducing website load times, you not only create a pleasant experience for your users but also reduce infrastructure costs.

Will the mobile phone and ID card be demagnetized together? Will the mobile phone and ID card be demagnetized together? Aug 14, 2023 pm 02:33 PM

Mobile phones and ID cards will not be demagnetized together. The reasons are: 1. There are coils and chips integrated inside the ID card. Unlike traditional tapes, hard drives and other storage media, there are no magnetized substances; 2. The electromagnetic radiation of mobile phones is high. Frequency electromagnetic waves, with frequencies as high as hundreds of thousands to tens of millions of Hertz, have no ability to magnetize ID cards. The magnets in the mobile phone speakers will not have a demagnetizing effect on the ID card. We can safely put the ID card and mobile phone together, and pay attention to avoid damage to the ID card and protect its normal use so that it can serve us better.

Performance differences between Java framework and .NET framework Performance differences between Java framework and .NET framework Jun 03, 2024 am 09:19 AM

In terms of high-concurrency request processing, .NETASP.NETCoreWebAPI performs better than JavaSpringMVC. The reasons include: AOT early compilation, which reduces startup time; more refined memory management, where developers are responsible for allocating and releasing object memory.

Can nfc recognize ID card? Can nfc recognize ID card? Nov 03, 2022 pm 02:30 PM

NFC can recognize ID cards; as early as 2015, China Mobile announced that its own brand N1 has opened the function of NFC to recognize ID cards. Through a special APP, you can use your mobile phone to read ID card information for real-name authentication; once mobile phones become popular for identity recognition It has the function of ID card or ID card, and uses digital password or fingerprint password to improve the security and anti-counterfeiting of its use to a certain extent.

A Practical Guide to Matching ID Numbers with PHP Regular Expressions A Practical Guide to Matching ID Numbers with PHP Regular Expressions Mar 05, 2024 pm 02:12 PM

PHP regular expression is a powerful tool that helps developers process all kinds of text data. In actual development, the verification and extraction of ID numbers are often involved. This article will introduce how to use PHP regular expressions to match ID numbers and provide specific code examples. The ID number is an important piece of personal identification information, usually containing 18 digits and a check code. A valid ID number should comply with certain formats and rules, such as restrictions on date of birth, area code, gender code, etc. Below is one

See all articles