Csharp: Optical Character Recognition

Source: Internet
Author: User

Using System; using System. collections. generic; using System. linq; using System. text; using System. data; using System. drawing; using System. IO; using System. drawing. imaging; using MODI; // Microsoft Office Document Imaging // first, use the office to install the disk. By default, this component is not installed when the office is installed. You only need to add this component to complete the installation instructions: http://support.microsoft.com/kb/982760// Microsoft Office Document Imaging 12.0 Type Library (office2007) // or Microsoft Office Document Imaging 11.0 Type Library (office2003) // Chinese Simplified OCR engine http://www.microsoft.com/downloads/thankyou.aspx?familyId=dd172063-9517-41d8-82af-29c38f7437b6&displayLang=zh-hk Namespace ToText {// <summary> // Optical Character Recognition // 20140507 Geovin Du // tu juwen /// </summary> public static class OCRGetstring {// <summary> // language type // </summary> /// <returns> </returns> public static DataTable getmediaagelist () {DataTable dt = new DataTable (); dt. columns. add ("ID", typeof (int); dt. columns. add ("LanguageName", typeof (string); dt. columns. add ("LanguageLCID", typeof (string); // dt. rows. add (1, "", 1); dt. rows. add (1, "English simplified", "2052"); dt. rows. add (2, "Traditional Chinese", "1028"); dt. rows. add (3, "English", "9"); dt. rows. add (4, "Czech", "5"); dt. rows. add (5, "Danish", "6"); dt. rows. add (6, "German", "7"); dt. rows. add (7, "Greek", "8"); dt. rows. add (8, "Spanish", "10"); dt. rows. add (9, "Finnish", "11"); dt. rows. add (10, "French", "12"); dt. rows. add (11, "Hungary", "14"); dt. rows. add (12, "Italian", "16"); dt. rows. add (13, "Japanese", "17"); dt. rows. add (14, "Korean", "18"); dt. rows. add (15, "Dutch", "19"); dt. rows. add (16, "Norwegian", "20"); dt. rows. add (17, "polish", "21"); dt. rows. add (18, "Portuguese", "22"); dt. rows. add (19, "Russian", "25"); dt. rows. add (20, "Swedish", "29"); dt. rows. add (21, "Turkish", "31"); return dt ;} /// <summary> ///// </summary> /// <param name = "sValue"> </param> /// <returns> </returns> private static MODI. miLANGUAGES GetLanuageType (string sValue) {switch (sValue) {case "2052": return MODI. miLANGUAGES. miLANG_CHINESE_SIMPLIFIED; case "5": return MODI. miLANGUAGES. miLANG_CZECH; case "6": return MODI. miLANGUAGES. miLANG_DANISH; case "7": return MODI. miLANGUAGES. miLANG_GERMAN; case "8": return MODI. miLANGUAGES. miLANG_GREEK; case "9": return MODI. miLANGUAGES. miLANG_ENGLISH; case "10": return MODI. miLANGUAGES. miLANG_SPANISH; case "11": return MODI. miLANGUAGES. miLANG_FINNISH; case "12": return MODI. miLANGUAGES. miLANG_FRENCH; case "14": return MODI. miLANGUAGES. miLANG_HUNGARIAN; case "16": return MODI. miLANGUAGES. miLANG_ITALIAN; case "17": return MODI. miLANGUAGES. miLANG_JAPANESE; case "18": return MODI. miLANGUAGES. miLANG_KOREAN; case "19": return MODI. miLANGUAGES. miLANG_DUTCH; case "20": return MODI. miLANGUAGES. miLANG_NORWEGIAN; case "21": return MODI. miLANGUAGES. miLANG_POLISH; case "22": return MODI. miLANGUAGES. miLANG_PORTUGUESE; case "25": return MODI. miLANGUAGES. miLANG_RUSSIAN; case "29": return MODI. miLANGUAGES. miLANG_SWEDISH; case "31": return MODI. miLANGUAGES. miLANG_TURKISH; case "1028": return MODI. miLANGUAGES. miLANG_CHINESE_TRADITIONAL; default: return MODI. miLANGUAGES. miLANG_ENGLISH ;}} /// <summary> /// Images plain text /// </summary> /// <param name = "image"> Image </param> // <param name = "language"> language type </param> // <returns> </returns> public static string ExtractText (this System. drawing. image image, string language) {var tmpFile = Path. getTempFileName (); StringBuilder sb = new StringBuilder (); // string text; try {var bmp = new Bitmap (Math. max (image. width, 1024), Math. max (image. height (768); var gfxResize = Graphics. fromImage (bmp); gfxResize. drawImage (image, new Rectangle (0, 0, image. width, image. height); bmp. save (tmpFile + ". bmp ", ImageFormat. bmp); var doc = new MODI. document (); doc. create (tmpFile + ". bmp "); // doc. OCR (MODI. miLANGUAGES. miLANG_ENGLISH, true, true); doc. OCR (GetLanuageType (language), true, true); // identify the text type var img = (MODI. image) doc. images [0]; var layout = img. layout; sb. append (layout. text); // text = sb. toString (); // layout. text;} finally {File. delete (tmpFile); File. delete (tmpFile + ". bmp ");} return sb. toString (); // text ;} /// <summary> /// source Image file Upload plain text /// </summary> /// <param name = "fileToOCR"> file </param>/ // <param name = "language"> language type </param> // <returns> </returns> public static string getFileToOCR (string fileToOCR, string language) {StringBuilder sb = new StringBuilder (); if (File. exists (fileToOCR) {MODI. document md = new MODI. document (); md. create (fileToOCR); md. OCR (GetLanuageType (language), true, true); MODI. image img; MODI. layout layout; for (int I = 0; I <md. images. count; I ++) {img = (MODI. image) md. images [I]; layout = img. layout; sb. append (layout. text);} md. close (false);} else {sb. append ("");} return sb. toString ();}}}

 

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.