C#&.Net干货分享-构建Aocr_ImageHelper读取图片文字做解析

直接源码,就是这么干脆。。。

 

namespace Frame.Image
{
    ///


    ///
    ///

    public class AspriseOCRLanguages
    {
        // Common used languages
        /// eng (English)
        public const String LANGUAGE_ENG = "eng";

        /// spa (Spanish)
        public const String LANGUAGE_SPA = "spa";

        /// por (Portuguese)
        public const String LANGUAGE_POR = "por";

        /// deu (German)
        public const String LANGUAGE_DEU = "deu";

        /// fra (French)
        public const String LANGUAGE_FRA = "fra";
        // around 30 languages are supported - use their ISO 639 3-letter as the id
    }
    ///
    /// Represents an Asprise OCR engine.
    /// Read the developer's guide here.
    ///

    public class AspriseOCR
    {
        private const string OCR_DLL_NAME_32 = "aocr.dll";
        private const string OCR_DLL_NAME_64 = "aocr_x64.dll";

        /// Highest speed, accuracy may suffer - default option
        public const String SPEED_FASTEST = "fastest";
        /// less speed, better accuracy
        public const String SPEED_FAST = "fast";
        /// lowest speed, best accuracy
        public const String SPEED_SLOW = "slow";

        /// Recognize  text
        public const String RECOGNIZE_TYPE_TEXT = "text";
        /// Recognize barcode
        public const String RECOGNIZE_TYPE_BARCODE = "barcode";
        /// Recognize both text and barcode
        public const String RECOGNIZE_TYPE_ALL = "all";

        /// Output recognition result as plain text
        public const String OUTPUT_FORMAT_PLAINTEXT = "text";
        /// Output recognition result in XML format with additional information if coordination, confidence, runtime, etc.
        public const String OUTPUT_FORMAT_XML = "xml";
        /// Output recognition result as searchable PDF
        public const String OUTPUT_FORMAT_PDF = "pdf";
        /// Output to editable format RTF (can be edited in MS Word)
        public const String OUTPUT_FORMAT_RTF = "rtf";
        // ------------------------ dictionary properties ------------------------

        /// set to 'true' to skip using the default built in dict. Default value: 'false' - can only be used for StartEngine
        public const String START_PROP_DICT_SKIP_BUILT_IN_DEFAULT = "START_PROP_DICT_SKIP_BUILT_IN_DEFAULT";

        /// set to 'true' to skip using all built-in dicts. Default value: 'false' - can only be used for StartEngine
        public const String START_PROP_DICT_SKIP_BUILT_IN_ALL = "START_PROP_DICT_SKIP_BUILT_IN_ALL";
        /// Path to your custom dictionary (words are separated using line breaks). Default value: null. - can only be used for StartEngine
        public const String START_PROP_DICT_CUSTOM_DICT_FILE = "START_PROP_DICT_CUSTOM_DICT_FILE";
        /// Path to your custom templates (templates are separated using line breaks). Default value: null. - can only be used for StartEngine
        public const String START_PROP_DICT_CUSTOM_TEMPLATES_FILE = "START_PROP_DICT_CUSTOM_TEMPLATES_FILE";

        /// Percentage measuring the importance of the dictionary (0: not at all; 100: extremely important; default: 10)
        public const String PROP_DICT_DICT_IMPORTANCE = "PROP_DICT_DICT_IMPORTANCE";

        // ------------------------ general options ------------------------
        /// Page type
        public const String PROP_PAGE_TYPE = "PROP_PAGE_TYPE";

        /// Page type value: auto
        public const String PROP_PAGE_TYPE_AUTO_DETECT = "auto";
        /// Page type value: a single block of text
        public const String PROP_PAGE_TYPE_SINGLE_BLOCK = "single_block";
        /// Page type value: a single column of text
        public const String PROP_PAGE_TYPE_SINGLE_COLUMN = "single_column";
        /// Page type value: a single line of text
        public const String PROP_PAGE_TYPE_SINGLE_LINE = "single_line";
        /// Page type value: a single word
        public const String PROP_PAGE_TYPE_SINGLE_WORD = "single_word";
        /// Page type value: a single char
        public const String PROP_PAGE_TYPE_SINGLE_CHARACTOR = "single_char";
        /// Page type value: scattered text
        public const String PROP_PAGE_TYPE_SCATTERED = "scattered";

        /// Limit charset to a set of predefined chars
        public const String PROP_LIMIT_TO_CHARSET = "PROP_LIMIT_TO_CHARSET";

        /// Set to 'true' to set the output level as word instead of the default, line.
        public const String PROP_OUTPUT_SEPARATE_WORDS = "PROP_OUTPUT_SEPARATE_WORDS";

        /// The DPI to be used to render the PDF file; default is 300 if not specified
        public const String PROP_INPUT_PDF_DPI = "PROP_INPUT_PDF_DPI";

        // ------------------------ Image pre-processing ------------------------
        /// Image pre-processing type
        public const String PROP_IMG_PREPROCESS_TYPE = "PROP_IMG_PREPROCESS_TYPE";

        /// Use system default
        public const String PROP_IMG_PREPROCESS_TYPE_DEFAULT = "default";

        /// Default + page orientation detection
        public const String PROP_IMG_PREPROCESS_TYPE_DEFAULT_WITH_ORIENTATION_DETECTION = "default_with_orientation_detection";

        /// Custom, need to set PROP_IMG_PREPROCESS_CUSTOM_CMDS
        public const String PROP_IMG_PREPROCESS_TYPE_CUSTOM = "custom";

        /// Custom mage pre-processing command
        public const String PROP_IMG_PREPROCESS_CUSTOM_CMDS = "PROP_IMG_PREPROCESS_CUSTOM_CMDS";

        // ------------------------ Table detection ------------------------
        /// table will be detected by default; set this property to true to skip detection.
        public const String PROP_TABLE_SKIP_DETECTION = "PROP_TABLE_SKIP_DETECTION";

        /// default is 31 if not specified
        public const String PROP_TABLE_MIN_SIDE_LENGTH = "PROP_TABLE_MIN_SIDE_LENGTH";

        /// Save intermediate images generated for debug purpose - don't specify or empty string to skip saving
        public const String PROP_SAVE_INTERMEDIATE_IMAGES_TO_DIR = "PROP_SAVE_INTERMEDIATE_IMAGES_TO_DIR";

        // ------------------------ PDF output specific ------------------------
        /// PDF output file - required for PDF output. Valid prop value: absolute path to the target output file.
        public const String PROP_PDF_OUTPUT_FILE = "PROP_PDF_OUTPUT_FILE";
        /// The DPI of the images or '0' to auto-detect. Optional. Valid prop value: 0(default: auto-detect), 300, 200, etc.
        public const String PROP_PDF_OUTPUT_IMAGE_DPI = "PROP_PDF_OUTPUT_IMAGE_DPI";

        /// Font to be used for PDF output. Optional. Valid values: "serif" (default), "sans".
        public const String PROP_PDF_OUTPUT_FONT = "PROP_PDF_OUTPUT_FONT";

        /// Make text visible - for debugging and analysis purpose. Optional. Valid prop values false(default), true.
        public const String PROP_PDF_OUTPUT_TEXT_VISIBLE = "PROP_PDF_OUTPUT_TEXT_VISIBLE";

        /// Convert images into black/white to reduce PDF output file size. Optional. Valid prop values: false(default), true.
        public const String PROP_PDF_OUTPUT_IMAGE_FORCE_BW = "PROP_PDF_OUTPUT_IMAGE_FORCE_BW";

        /// Set to 'text' or 'xml' to return information when the output format is PDF
        public const String PROP_PDF_OUTPUT_RETURN_TEXT = "PROP_PDF_OUTPUT_RETURN_TEXT";

        /// Return text
        public const String PROP_PDF_OUTPUT_RETURN_TEXT_FORMAT_PLAINTEXT = "text";

        /// Return xml
        public const String PROP_PDF_OUTPUT_RETURN_TEXT_FORMAT_XML = "xml";

        /// Set to true to output PDF/A instead of normal PDF.
        public const String PROP_PDF_OUTPUT_PDFA = "PROP_PDF_OUTPUT_PDFA";

        /// Optionally specifies path to the custom font to be embedded in PDF/A
        public const String PROP_PDF_OUTPUT_PDFA_FONT_FILE = "PROP_PDF_OUTPUT_PDFA_FONT_FILE";

        // ------------------------ RTF specific ------------------------
        /// RTF output file - required for RTF output. Valid prop value: absolute path to the target output file.
        public const String PROP_RTF_OUTPUT_FILE = "PROP_RTF_OUTPUT_FILE";

        /// default is LETTER, may set to A4.
        public const String PROP_RTF_PAPER_SIZE = "PROP_RTF_PAPER_SIZE";

        /// Return text in 'text' or 'xml' format when the output format is set to RTF.
        public const String PROP_RTF_OUTPUT_RETURN_TEXT = "PROP_RTF_OUTPUT_RETURN_TEXT";

        /// Return text
        public const String PROP_RTF_OUTPUT_RETURN_TEXT_FORMAT_PLAINTEXT = "text";

        /// Return xml
        public const String PROP_RTF_OUTPUT_RETURN_TEXT_FORMAT_XML = "xml";

        /// Do not change unless you are told so.
        public static String CONFIG_PROP_SEPARATOR = "|";

        /// Do not change unless you are told so.
        public static String CONFIG_PROP_KEY_VALUE_SEPARATOR = "=";

        /// Recognize all pages.
        public const int PAGES_ALL = -1;

        ///
        /// Unmanaged code access (32bit).
        ///

        private static class OcrDll32
        {
            ///
            ///
            ///

            ///
            [DllImport(OCR_DLL_NAME_32, CharSet = CharSet.Ansi)]
            public static extern IntPtr com_asprise_ocr_version();

            ///
            ///
            ///

            ///
            ///
            [DllImport(OCR_DLL_NAME_32, CharSet = CharSet.Ansi)]
            public static extern int com_asprise_ocr_setup(int queryOnly);

            ///
            ///
            ///

            ///
            [DllImport(OCR_DLL_NAME_32, CharSet = CharSet.Ansi)]
            public static extern IntPtr com_asprise_ocr_list_supported_langs();

            ///
            ///
            ///

            ///
            ///
            ///
            ///
            ///
            ///
            [DllImport(OCR_DLL_NAME_32, CharSet = CharSet.Ansi)]
            public static extern IntPtr com_asprise_ocr_start(string lang, string speed, string propSpec, string propSeparator, string propKeyValueSpeparator);

            ///
            ///
            ///

            ///
            [DllImport(OCR_DLL_NAME_32, CharSet = CharSet.Ansi)]
            public static extern void com_asprise_ocr_stop(Int64 handle);

            ///
            ///
            ///

            ///
            ///
            ///
            ///
            ///
            ///
            ///
            ///
            ///
            ///
            ///
            ///
            ///
            [DllImport(OCR_DLL_NAME_32, EntryPoint = "com_asprise_ocr_recognize")]
            public static extern IntPtr com_asprise_ocr_recognize(Int64 handle, string imgFiles, int pageIndex, int startX, int startY, int width, int height, string recognizeType, string outputFormat, string propSpec, string propSeparator, string propKeyValueSpeparator);

            ///
            ///
            ///

            ///
            ///
            [DllImport(OCR_DLL_NAME_32, CharSet = CharSet.Ansi)]
            public static extern void com_asprise_ocr_input_license(string licenseeName, string licenseCode);

            ///
            ///
            ///

            ///
            ///
            [DllImport(OCR_DLL_NAME_32, CharSet = CharSet.Ansi)]
            public static extern void com_asprise_ocr_util_delete(Int64 handle, bool isArray);

            ///
            ///
            ///

            ///
            ///
            ///
            ///
            ///
            [DllImport(OCR_DLL_NAME_32, CharSet = CharSet.Ansi)]
            public static extern IntPtr com_asprise_image_new(int width, int height, int depth, bool indexed);

            ///
            ///
            ///

            ///
            ///
            ///
            ///
            ///
            ///
            [DllImport(OCR_DLL_NAME_32, CharSet = CharSet.Ansi)]
            public static extern bool com_asprise_image_add_color_to_map(Int64 imgPtr, int r, int g, int b, int a);

            ///
            ///
            ///

            ///
            ///
            [DllImport(OCR_DLL_NAME_32, CharSet = CharSet.Ansi)]
            public static extern IntPtr com_asprise_image_get_data(Int64 imgPtr);

            ///
            ///
            ///

            ///
            ///
            ///
            [DllImport(OCR_DLL_NAME_32, CharSet = CharSet.Ansi)]
            public static extern bool com_asprise_image_save(Int64 imgPtr, string file);

            ///
            ///
            ///

            ///
            ///
            [DllImport(OCR_DLL_NAME_32, CharSet = CharSet.Ansi)]
            public static extern bool com_asprise_image_destory(Int64 imgPtr);
        }

        ///
        /// Unmanaged code access (64bit).
        ///

        private static class OcrDll64
        {
            [DllImport(OCR_DLL_NAME_64, CharSet = CharSet.Ansi)]
            public static extern IntPtr com_asprise_ocr_version();

            [DllImport(OCR_DLL_NAME_64, CharSet = CharSet.Ansi)]
            public static extern int com_asprise_ocr_setup(int queryOnly);

            [DllImport(OCR_DLL_NAME_64, CharSet = CharSet.Ansi)]
            public static extern IntPtr com_asprise_ocr_list_supported_langs();

            [DllImport(OCR_DLL_NAME_64, CharSet = CharSet.Ansi)]
            public static extern IntPtr com_asprise_ocr_start(string lang, string speed, string propSpec, string propSeparator, string propKeyValueSpeparator);

            [DllImport(OCR_DLL_NAME_64, CharSet = CharSet.Ansi)]
            public static extern void com_asprise_ocr_stop(Int64 handle);

            [DllImport(OCR_DLL_NAME_64, CharSet = CharSet.Ansi)]
            public static extern IntPtr com_asprise_ocr_recognize(Int64 handle, string imgFiles, int pageIndex, int startX, int startY, int width, int height, string recognizeType, string outputFormat, string propSpec, string propSeparator, string propKeyValueSpeparator);

            [DllImport(OCR_DLL_NAME_64, CharSet = CharSet.Ansi)]
            public static extern void com_asprise_ocr_input_license(string licenseeName, string licenseCode);

            [DllImport(OCR_DLL_NAME_64, CharSet = CharSet.Ansi)]
            public static extern void com_asprise_ocr_util_delete(Int64 handle, bool isArray);

            [DllImport(OCR_DLL_NAME_64, CharSet = CharSet.Ansi)]
            public static extern IntPtr com_asprise_image_new(int width, int height, int depth, bool indexed);

            [DllImport(OCR_DLL_NAME_64, CharSet = CharSet.Ansi)]
            public static extern bool com_asprise_image_add_color_to_map(Int64 imgPtr, int r, int g, int b, int a);

            [DllImport(OCR_DLL_NAME_64, CharSet = CharSet.Ansi)]
            public static extern IntPtr com_asprise_image_get_data(Int64 imgPtr);

            [DllImport(OCR_DLL_NAME_64, CharSet = CharSet.Ansi)]
            public static extern bool com_asprise_image_save(Int64 imgPtr, string file);

            [DllImport(OCR_DLL_NAME_64, CharSet = CharSet.Ansi)]
            public static extern bool com_asprise_image_destory(Int64 imgPtr);
        }

        private IntPtr _handle = new IntPtr(0);

        ///
        /// Whether the OCR engine is currently running.
        ///

        public bool IsEngineRunning
        {
            get { return _handle.ToInt64() > 0; }
        }

        ///
        /// Starts the OCR engine; does nothing if the engine has already been started.
        ///

        /// e.g., "eng"
        /// e.g., "fastest"
        /// property specifications, can be a single Dictionary object or inline specification in pairs. Valid property names are defined in this class, e.g., START_PROP_DICT_CUSTOM_DICT_FILE, etc.
        public void StartEngine(string lang, string speed = SPEED_FASTEST, params object[] startProperties)
        {
            Dictionary dict = ReadProperties(startProperties);

            if (IsEngineRunning)
            {
                return;
            }
            if (lang == null || speed == null || lang.Trim().Length == 0 || speed.Trim().Length == 0)
            {
                throw new Exception("Invalid arguments.");
            }

            _handle = Is64BitProcess ?
                OcrDll64.com_asprise_ocr_start(lang, speed, DictToString(dict), CONFIG_PROP_SEPARATOR, CONFIG_PROP_KEY_VALUE_SEPARATOR) :
                OcrDll32.com_asprise_ocr_start(lang, speed, DictToString(dict), CONFIG_PROP_SEPARATOR, CONFIG_PROP_KEY_VALUE_SEPARATOR);

            if (_handle.ToInt64() == 0)
            {
                throw new Exception("Failed to start engine. Error code: " + _handle.ToInt64());
            }
        }

        ///
        /// Stops the OCR engine; does nothing if it has already been stopped.
        ///

        public void StopEngine()
        {
            if (!IsEngineRunning)
            {
                return;
            }
            if (Is64BitProcess)
            {
                OcrDll64.com_asprise_ocr_stop(_handle.ToInt64());
            }
            else
            {
                OcrDll32.com_asprise_ocr_stop(_handle.ToInt64());
            }
        }

        private Thread threadDoingOCR;

        ///
        /// Performs OCR on the given input bitmaps.
        ///

        /// List of bitmaps to perform OCR on
        /// -1 for whole page or the starting x coordinate of the specified region
        /// -1 for whole page or the starting y coordinate of the specified region
        /// -1 for whole page or the width of the specified region
        /// -1 for whole page or the height of the specified region
        /// valid values: RECOGNIZE_TYPE_TEXT, RECOGNIZE_TYPE_BARCODE or RECOGNIZE_TYPE_ALL.
        /// valid values: OUTPUT_FORMAT_PLAINTEXT, OUTPUT_FORMAT_XML, OUTPUT_FORMAT_PDF or OUTPUT_FORMAT_RTF.
        /// additional properties, can be a single Dictionary object or inline specification in pairs. Valid property names are defined in this class, e.g., PROP_INCLUDE_EMPTY_BLOCK, etc.
        /// text (plain text, xml) recognized for OUTPUT_FORMAT_PLAINTEXT, OUTPUT_FORMAT_XML
        public string Recognize(IList bitmaps, int startX, int startY, int width, int height, string recognizeType, string outputFormat, params object[] additionalProperties)
        {
            IList imgHandles = new List();
            string imgNames = "";
            try
            {
                foreach (Bitmap bitmap in bitmaps)
                {
                    if (bitmap == null)
                    {
                        continue;
                    }
                    Int64 imgPtr = AspriseOCR.ImageFrom(bitmap);
                    if (imgPtr == 0)
                    {
                        continue;
                    }
                    imgHandles.Add(imgPtr);
                    if (imgNames.Length > 0)
                    {
                        imgNames += ',';
                    }
                    imgNames += "image://" + imgPtr;
                }
                if (imgHandles.Count == 0 || imgNames.Length == 0)
                {
                    return null;
                }

                return Recognize(imgNames, -1, startX, startY, width, height, recognizeType, outputFormat, additionalProperties);
            }
            finally
            {
                foreach (Int64 handle in imgHandles)
                {
                    ImageDestory(handle);
                }
            }
        }

        ///
        /// Performs OCR on the given input files.
        ///

        /// comma ',' separated image file path (JPEG, BMP, PNG, TIFF)
        /// -1 for all pages or the specified page (first page is 1) for multi-page image format like TIFF
        /// -1 for whole page or the starting x coordinate of the specified region
        /// -1 for whole page or the starting y coordinate of the specified region
        /// -1 for whole page or the width of the specified region
        /// -1 for whole page or the height of the specified region
        /// valid values: RECOGNIZE_TYPE_TEXT, RECOGNIZE_TYPE_BARCODE or RECOGNIZE_TYPE_ALL.
        /// valid values: OUTPUT_FORMAT_PLAINTEXT, OUTPUT_FORMAT_XML, OUTPUT_FORMAT_PDF or OUTPUT_FORMAT_RTF.
        /// additional properties, can be a single Dictionary object or inline specification in pairs. Valid property names are defined in this class, e.g., PROP_INCLUDE_EMPTY_BLOCK, etc.
        /// text (plain text, xml) recognized for OUTPUT_FORMAT_PLAINTEXT, OUTPUT_FORMAT_XML
        public string Recognize(string files, int pageIndex, int startX, int startY, int width, int height, string recognizeType, string outputFormat, params object[] additionalProperties)
        {
            if (threadDoingOCR != null)
            {
                throw new Exception("Currently " + threadDoingOCR + " is using this OCR engine. Please create multiple OCR engine instances for multi-threading. ");
            }

            Dictionary dict = ReadProperties(additionalProperties);
            if (outputFormat.Equals(OUTPUT_FORMAT_PDF))
            {
                string pdfOutputFile = dict.ContainsKey(PROP_PDF_OUTPUT_FILE) ? dict[PROP_PDF_OUTPUT_FILE] : null;
                if (pdfOutputFile == null)
                {
                    throw new Exception("You must specify PDF output through property named: " + PROP_PDF_OUTPUT_FILE);
                }

                if (!dict.ContainsKey(PROP_OUTPUT_SEPARATE_WORDS))
                {
                    dict[PROP_OUTPUT_SEPARATE_WORDS] = "true"; // default as separate
                }
            }
            if (outputFormat.Equals(OUTPUT_FORMAT_RTF))
            {
                string rtfOutputFile = dict.ContainsKey(PROP_RTF_OUTPUT_FILE) ? dict[PROP_RTF_OUTPUT_FILE] : null;
                if (rtfOutputFile == null)
                {
                    throw new Exception("You must specify RTF output through property named: " + PROP_RTF_OUTPUT_FILE);
                }
            }

            try
            {
                threadDoingOCR = Thread.CurrentThread;

                IntPtr ptr = (Is64BitProcess ?
                    OcrDll64.com_asprise_ocr_recognize(_handle.ToInt64(), files, pageIndex, startX, startY, width, height, recognizeType, outputFormat, DictToString(dict), CONFIG_PROP_SEPARATOR, CONFIG_PROP_KEY_VALUE_SEPARATOR) :
                    OcrDll32.com_asprise_ocr_recognize(_handle.ToInt64(), files, pageIndex, startX, startY, width, height, recognizeType, outputFormat, DictToString(dict), CONFIG_PROP_SEPARATOR, CONFIG_PROP_KEY_VALUE_SEPARATOR)
                );

                string s = Marshal.PtrToStringAnsi(ptr);
                string sInUnicode = null;
                if (s != null && s.Length > 0 && ptr.ToInt64() > 0)
                {
                    sInUnicode = Utf8ToUnicode(s);
                    // clean up
                    DeleteC(ptr, true);
                }
                return sInUnicode;
            }
            finally
            {
                threadDoingOCR = null;
            }

        }

        ///
        ///
        ///

        ///
        ///
        internal static Dictionary ReadProperties(Object[] propSpec)
        {
            Dictionary dict = new Dictionary();

            if (propSpec == null || propSpec.Length == 0 || (propSpec.Length == 1 && propSpec[0] == null))
            {
                // nothing to do.
            }
            else if (propSpec.Length == 1 && (propSpec[0] as String != null))
            {
                // parse properties
                dict = StringToDict((String)propSpec[0]);
            }
            else if (propSpec != null && propSpec.Length > 0 &&
              (propSpec[0] as Dictionary != null))
            {
                foreach (KeyValuePair pair in (Dictionary)propSpec[0])
                {
                    if (pair.Key != null)
                    {
                        dict[pair.Key.ToString()] = pair.Value == null ? null : pair.Value.ToString();
                    }
                }
            }
            else if (propSpec != null && propSpec.Length > 0)
            {
                if (propSpec.Length % 2 == 1)
                {
                    throw new Exception("You must specify additional properties in key/value pair. Current length: " + propSpec.Length);
                }
                for (var p = 0; p < propSpec.Length; p += 2)
                {
                    string key = (string)propSpec[p];
                    object val = propSpec[p + 1];
                    if (key != null)
                    {
                        dict[key] = val == null ? "" : val.ToString();
                    }
                }
            }

            // validation
            foreach (KeyValuePair pair in dict)
            {
                if (pair.Key.Contains(CONFIG_PROP_KEY_VALUE_SEPARATOR))
                {
                    throw new Exception("Please change CONFIG_PROP_KEY_VALUE_SEPARATOR to a different value as \"" +
                        pair.Key + "\" contains \"" + CONFIG_PROP_KEY_VALUE_SEPARATOR + "\"");
                }
                if (pair.Value.Contains(CONFIG_PROP_SEPARATOR))
                {
                    throw new Exception("Please change CONFIG_PROP_SEPARATOR to a different value as \"" +
                            pair.Value + "\" contains \"" + CONFIG_PROP_SEPARATOR + "\"");
                }
            }
            return dict;
        }

        ///
        ///
        ///

        ///
        ///
        internal static Dictionary StringToDict(String s)
        {
            Dictionary dict = new Dictionary();
            if (s == null || s.Trim().Length == 0)
            {
                return dict;
            }
            string[] props = s.Split(new string[] { CONFIG_PROP_SEPARATOR }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string prop in props)
            {
                string[] parts = prop.Split(new string[] { CONFIG_PROP_KEY_VALUE_SEPARATOR }, StringSplitOptions.RemoveEmptyEntries);
                if (parts.Length >= 2)
                {
                    dict[parts[0]] = parts[1];
                }
            }
            return dict;
        }

        ///
        /// The library version.
        ///

        /// The library version.
        public static string GetLibraryVersion()
        {
            return Marshal.PtrToStringAnsi(Is64BitProcess ?
                OcrDll64.com_asprise_ocr_version() :
                OcrDll32.com_asprise_ocr_version());
        }

        ///
        /// Performs one-time setup; does nothing if setup has already been done.
        ///

        public static void SetUp()
        {
            string dllPath = LoadDll();
            if (dllPath == null)
            {
                throw new SystemException("OCR dll not found. Please download the latest evaluation kit from asprise.com");
            }
            if (Is64BitProcess)
            {
                OcrDll64.com_asprise_ocr_setup(0);
            }
            else
            {
                OcrDll32.com_asprise_ocr_setup(0);
            }
        }

        ///
        /// Call this after setup is done; returns list of langs separated by ','
        ///

        /// The list of langs separated by ','
        public static string ListSupportedLangs()
        {
            return Marshal.PtrToStringAnsi(Is64BitProcess ? OcrDll64.com_asprise_ocr_list_supported_langs() : OcrDll32.com_asprise_ocr_list_supported_langs());
        }

        /// Input the license code
        /// Licensee name
        /// License code
        public static void InputLicense(string licenseeName, string licenseCode)
        {
            if (Is64BitProcess)
            {
                OcrDll64.com_asprise_ocr_input_license(licenseeName, licenseCode);
            }
            else
            {
                OcrDll32.com_asprise_ocr_input_license(licenseeName, licenseCode);
            }
        }

        /// Finds the OCR dll in system path or from bundle and return the path to the dll.
        public static string LoadDll()
        {
            // 1. Search path.
            string dllFilePath = AspriseOCR.GetOcrDllPath();
            if (dllFilePath == null)
            {
                // 2. Then parent folders
                string parentFolder = AspriseOCR.DetectOcrDllInParentFolders();
                if (parentFolder != null)
                {
                    AspriseOCR.AddToSystemPath(parentFolder);
                    // log("Folder containing ocr dll detected: " + parentFolder);
                }
            }
            dllFilePath = GetOcrDllPath();
            if (dllFilePath != null)
            {
                return dllFilePath;
            }

            // 3. from DLL bundle
            string fromBundle = AspriseOCR.ExtractDllFromBundleTo(Directory.GetCurrentDirectory(), true);
            if (fromBundle != null)
            {
                // log("Using OCR dll from bundle: " + fromBundle);
            }

            return GetOcrDllPath();
        }

        ///
        /// Search PATH and return the location of the ocr dll.
        ///

        ///
        protected static string GetOcrDllPath()
        {
            return SearchFileInPath(GetOcrDllName());
        }

        ///
        /// The simple name of the ocr dll file.
        ///

        ///
        public static string GetOcrDllName()
        {
            return Is64BitProcess ? OCR_DLL_NAME_64 : OCR_DLL_NAME_32;
        }

        ///
        /// Search the ancester directories and return the directory that contains ocr dll or null if not found.
        ///

        ///
        private static string DetectOcrDllInParentFolders()
        {
            string folder = AppDomain.CurrentDomain.BaseDirectory;
            while (true)
            {
                if (File.Exists(Path.Combine(folder, GetOcrDllName())))
                {
                    return folder;
                }
                else
                {
                    folder = Path.GetDirectoryName(folder);
                    if (folder == null)
                    {
                        break;
                    }
                }
            }
            return null;
        }

        ///
        ///
        ///

        ///
        ///
        ///
        private static string ExtractDllFromBundleTo(string dir, bool overwrite)
        {



            string bundleName = Is64BitProcess ? "asprise-ocr-dll-bundle-64" : "asprise-ocr-dll-bundle-32";
            Assembly bundleAssemly = null;
            foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
            {
                string name = asm.GetName().Name;
                if (asm.GetName().Name == bundleName)
                {
                    bundleAssemly = asm;
                    break;
                }
            }

            if (bundleAssemly == null)
            {
                Console.Error.WriteLine("Assembly not loaded: " + bundleName);
                return null;
            }

            string resourceClassName = bundleName.Replace('-', '_') + ".Properties.Resources";
            Type typeResources = bundleAssemly.GetType(resourceClassName, false);
            if (typeResources == null)
            {
                Console.Error.WriteLine("OCR dll bundle not referenced: " + bundleName);
                return null;
            }
            MethodInfo[] methods = typeResources.GetMethods();
            MethodInfo methodAocrRes = null;
            string dllMd5 = null;
            for (int i = 0; methods != null && i < methods.Length; i++)
            {
                MethodInfo m = methods[i];
                if (m.Name.StartsWith("get_aocr"))
                {
                    methodAocrRes = m;
                    dllMd5 = m.Name.Substring(m.Name.LastIndexOf('_') + 1);
                    break;
                }
            }

            if (methodAocrRes == null)
            {
                Console.Error.WriteLine("OCR dll bundle not referenced, but unable to find resource in " + typeResources);
                return null;
            }

            string path = dir +
                ((dir.EndsWith("/") || dir.EndsWith("\\")) ? "" : "\\") +
              GetOcrDllName();
            if (!overwrite && File.Exists(path))
            {
                return null;
            }

            File.WriteAllBytes(path, (byte[])methodAocrRes.Invoke(null, new object[0]));
            return bundleName;
        }

        ///
        /// Returns the absolute path of the first occurrence
        ///

        ///
        ///
        protected static string SearchFileInPath(string fileSimpleName)
        {
            string path = GetSystemPath();
            string[] folders = path.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            // insert current dir to folders
            string[] extended = new string[folders.Length + 1];
            extended[0] = AppDomain.CurrentDomain.BaseDirectory + "bin\\";
            Array.Copy(folders, 0, extended, 1, folders.Length);
            folders = extended;
            for (int i = 0; i < folders.Length; i++)
            {
                string folder = folders[i];
                folder = folder.Replace('/', '\\');
                if (!folder.EndsWith("\\"))
                {
                    folder += "\\";
                }
                string file = folder + fileSimpleName;
                if (File.Exists(file))
                {
                    return file;
                }
            }
            return null;
        }

        ///
        /// Running in 64bit mode?
        ///

        protected static bool Is64BitProcess
        {
            get { return IntPtr.Size == 8; }
        }

        ///
        /// Performs native C/C++ delete
        ///

        /// pointer
        /// whether delete []
        protected static void DeleteC(IntPtr ptr, bool isArray)
        {
            if (Is64BitProcess)
            {
                OcrDll64.com_asprise_ocr_util_delete(ptr.ToInt64(), isArray);
            }
            else
            {
                OcrDll32.com_asprise_ocr_util_delete(ptr.ToInt64(), isArray);
            }
        }

        ///
        /// Returns the system path
        ///

        /// System path
        protected static string GetSystemPath()
        {
            return Environment.GetEnvironmentVariable("PATH");
        }

        ///
        /// Adds the given directory to the PATH variable.
        ///

        /// The folder to be added to PATH
        public static void AddToSystemPath(string dir)
        {
            Environment.SetEnvironmentVariable("PATH", dir + ";" + Environment.GetEnvironmentVariable("PATH"));
        }

        ///
        ///
        ///

        ///
        ///
        public static string DictToString(Dictionary dict)
        {
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair pair in dict)
            {
                if (sb.Length > 0)
                {
                    sb.Append(CONFIG_PROP_SEPARATOR);
                }
                sb.Append(pair.Key);
                sb.Append(CONFIG_PROP_KEY_VALUE_SEPARATOR);
                sb.Append(pair.Value);
            }
            return sb.ToString();
        }

        ///
        /// Returns the ToString() for non-null object or ""/"null" for null.
        ///

        /// target object
        /// true to return "" for null; false "null"
        ///
        protected static string ObjectToString(object obj, bool nullAsEmpty = true)
        {
            if (obj == null)
            {
                return nullAsEmpty ? "" : "null";
            }
            return obj.ToString();
        }

        ///
        /// Returns the first non-null object or null if all arguments are null.
        ///

        ///
        ///
        ///
        protected static object FirstNonNull(object o, params object[] others)
        {
            if (o != null)
            {
                return o;
            }

            for (var i = 0; others != null && i < others.Length; i++)
            {
                if (others[i] != null)
                {
                    return others[i];
                }
            }

            return null;
        }

        ///
        /// Converts utf8 encoded string to unicode
        ///

        ///
        ///
        protected static string Utf8ToUnicode(string utf8String)
        {
            Encoding ansiEncoding = Encoding.GetEncoding(1252);

            byte[] utf8Bytes = new byte[utf8String.Length];
            for (int i = 0; i < utf8String.Length; ++i)
            {
                utf8Bytes[i] = ansiEncoding.GetBytes(utf8String.Substring(i, 1))[0];
            }

            return Encoding.UTF8.GetString(utf8Bytes, 0, utf8Bytes.Length);
        }

        /// save the aocr.xsl to the specified directory
        public static bool SaveAocrXslTo(string dir, bool overwrite)
        {
            string path = dir +
                ((dir.EndsWith("/") || dir.EndsWith("\\")) ? "" : "\\") +
                "aocr.xsl";
            if (!overwrite && File.Exists(path))
            {
                return false;
            }
            return true;
        }

        private static Int64 ImageCreate(int width, int height, int depth, bool indexed)
        {
            return Is64BitProcess ? OcrDll64.com_asprise_image_new(width, height, depth, indexed).ToInt64() :
                OcrDll32.com_asprise_image_new(width, height, depth, indexed).ToInt64();
        }

        private static bool ImageAddIndexedColor(Int64 imgPtr, int r, int g, int b, int a)
        {
            return Is64BitProcess ? OcrDll64.com_asprise_image_add_color_to_map(imgPtr, r, g, b, a) : OcrDll32.com_asprise_image_add_color_to_map(imgPtr, r, g, b, a);
        }

        private static Int64 ImageGetData(Int64 imgPtr)
        {
            return Is64BitProcess ? OcrDll64.com_asprise_image_get_data(imgPtr).ToInt64() : OcrDll32.com_asprise_image_get_data(imgPtr).ToInt64();
        }

        private static bool ImageSave(Int64 imgPtr, string file)
        {
            return Is64BitProcess ? OcrDll64.com_asprise_image_save(imgPtr, file) : OcrDll32.com_asprise_image_save(imgPtr, file);
        }

        private static bool ImageDestory(Int64 imgPtr)
        {
            return Is64BitProcess ? OcrDll64.com_asprise_image_destory(imgPtr) : OcrDll32.com_asprise_image_destory(imgPtr);
        }

        private unsafe static Int64 ImageFrom(Bitmap bitmap)
        {
            int depth = 0;
            if (bitmap.PixelFormat == PixelFormat.Format1bppIndexed)
            {
                depth = 1;
            }
            else if (bitmap.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                depth = 8;
            }
            else if (bitmap.PixelFormat == PixelFormat.Format32bppArgb || bitmap.PixelFormat == PixelFormat.Format24bppRgb)
            {
                depth = 32;
            }
            if (depth == 0)
            {
                throw new Exception("Unsupported bitmap pixel format: " + bitmap.PixelFormat);
            }

            int width = bitmap.Width; int height = bitmap.Height;
            bool indexed = (bitmap.PixelFormat & PixelFormat.Indexed) == PixelFormat.Indexed;
            Int64 imgPtr = ImageCreate(width, height, depth, indexed);
            if (imgPtr == 0)
            {
                throw new Exception("Failed to get image object from bitmap");
            }
            if (indexed)
            {
                ColorPalette palette = bitmap.Palette;
                for (int i = 0; i < palette.Entries.Length; i++)
                //for (int i = palette.Entries.Length - 1; i >= 0; i--)
                {
                    Color color = palette.Entries[i];
                    if (!ImageAddIndexedColor(imgPtr, color.R, color.G, color.B, color.A))
                    {
                        throw new Exception("Failed to add index color to image");
                    }
                }
            }

            Int64 imgDataPtr = ImageGetData(imgPtr);
            BitmapData bitmapData = null;
            try
            {
                bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
                int imgRowWidthWords = (width * depth + 31) / 32;
                if (bitmap.PixelFormat == PixelFormat.Format1bppIndexed)
                {
                    for (int r = 0; r < height; r++)
                    {
                        byte* bitmapRow = (byte*)bitmapData.Scan0 + (r * bitmapData.Stride);
                        uint* imgRow = (uint*)(imgDataPtr) + (r * imgRowWidthWords);
                        for (int c = 0; c < (width + 7) / 8; c++) /////////////////
                        {
                            //*(imgRow + c) = *(bitmapRow + c);
                            if (Is64BitProcess)
                            {
                                *(byte*)((ulong)((byte*)imgRow + c) ^ 3) = (byte)(255 - (byte)(*(bitmapRow + c)));
                            }
                            else
                            {
                                *(byte*)((uint)((byte*)imgRow + c) ^ 3) = (byte)(255 - (byte)(*(bitmapRow + c)));
                            }
                        }
                    }
                }
                else if (bitmap.PixelFormat == PixelFormat.Format8bppIndexed)
                {
                    for (int r = 0; r < height; r++)
                    {
                        byte* bitmapRow = (byte*)bitmapData.Scan0 + (r * bitmapData.Stride);
                        uint* imgRow = (uint*)(imgDataPtr) + (r * imgRowWidthWords);
                        for (int c = 0; c < width; c++)
                        {
                            if (Is64BitProcess)
                            {
                                *(byte*)((ulong)((byte*)imgRow + c) ^ 3) = (byte)(*(bitmapRow + c));
                            }
                            else
                            {
                                *(byte*)((uint)((byte*)imgRow + c) ^ 3) = (byte)(*(bitmapRow + c));
                            }
                        }
                    }
                }
                else if (bitmap.PixelFormat == PixelFormat.Format24bppRgb)
                {
                    for (int r = 0; r < height; r++)
                    {
                        byte* bitmapRow = (byte*)bitmapData.Scan0 + (r * bitmapData.Stride);
                        uint* imgRow = (uint*)(imgDataPtr) + (r * imgRowWidthWords);
                        for (int c = 0; c < width; c++)
                        {
                            byte* colorPtr = bitmapRow + (c * 3);
                            byte blue = colorPtr[0], green = colorPtr[1], red = colorPtr[2];
                            *(imgRow + c) = (uint)((red << 24) | (green << 16) | (blue << 8) | 255); ;
                        }
                    }
                }
                else if (bitmap.PixelFormat == PixelFormat.Format32bppArgb)
                {
                    for (int r = 0; r < height; r++)
                    {
                        byte* bitmapRow = (byte*)bitmapData.Scan0 + (r * bitmapData.Stride);
                        uint* imgRow = (uint*)(imgDataPtr) + (r * imgRowWidthWords);
                        for (int c = 0; c < width; c++)
                        {
                            byte* colorPtr = bitmapRow + (c * 4);
                            byte blue = colorPtr[0], green = colorPtr[1], red = colorPtr[2], alpha = colorPtr[3];
                            *(imgRow + c) = (uint)((red << 24) | (green << 16) | (blue << 8) | alpha);
                        }
                    }
                }
                return imgPtr;
            }
            catch (Exception)
            {
                ImageDestory(imgPtr);
                throw;
            }
            finally
            {
                if (bitmapData != null)
                {
                    bitmap.UnlockBits(bitmapData);
                }
            }
        }
    }

    ///
    ///
    ///

    public static class Aocr_ImageHelper
    {
        ///
        ///
        ///

        ///
        ///
        ///
        public static string ReadImageText(string imageFilepath, string lang = AspriseOCRLanguages.LANGUAGE_ENG)
        {
            AspriseOCR.SetUp(); // one-time setup
            AspriseOCR aspriseOCR = new AspriseOCR();
            aspriseOCR.StartEngine(lang);
            string imageText = aspriseOCR.Recognize(imageFilepath, -1, -1, -1, -1, -1, AspriseOCR.RECOGNIZE_TYPE_ALL, AspriseOCR.OUTPUT_FORMAT_PLAINTEXT);
            aspriseOCR.StopEngine();
            return imageText;
        }

        ///
        ///
        ///

        ///
        ///
        ///
        public static string ReadBitmapText(List bitmaps, string lang = AspriseOCRLanguages.LANGUAGE_ENG)
        {
            AspriseOCR.SetUp(); // one-time setup
            AspriseOCR aspriseOCR = new AspriseOCR();
            aspriseOCR.StartEngine(lang);
            string imageText = aspriseOCR.Recognize(bitmaps, -1, -1, -1, -1, AspriseOCR.RECOGNIZE_TYPE_ALL, AspriseOCR.OUTPUT_FORMAT_PLAINTEXT);
            aspriseOCR.StopEngine();
            return imageText;
        }
    }
}

你可能感兴趣的:(C#&.Net干货分享-构建Aocr_ImageHelper读取图片文字做解析)