예제 #1
0
 private static void Close()
 {
     if (ImgObj != 0)
     {
         TNSOCR.Img_Destroy(ImgObj);
     }
     if (OcrObj != 0)
     {
         TNSOCR.Ocr_Destroy(OcrObj);
     }
     if (CfgObj != 0)
     {
         TNSOCR.Cfg_Destroy(CfgObj);
     }
     TNSOCR.Engine_Uninitialize();
 }
예제 #2
0
    public async Task <object> Invoke(dynamic input)
    {
        Image     bmp;
        int       CfgObj;
        bool      Dwn = false;
        Rectangle Frame;
        Graphics  gr;
        int       ImgObj;
        bool      NoEvent;
        int       OcrObj;
        var       fileByte = (byte[])input.byte64;



        StringBuilder val;

        val = new StringBuilder(256);
        TNSOCR.Engine_GetVersion(val);
        //if you get "BadImageFormatException" error here: find and check "LIBNAME" constant in "NSOCR.cs"
        //  Console.WriteLine(" [ NSOCR version: " + val + " ] ");

        TNSOCR.Engine_SetLicenseKey(((string)input.finalKey)); //required for licensed version only

        //init engine and create ocr-related objects
        TNSOCR.Engine_Initialize();
        TNSOCR.Cfg_Create(out CfgObj);
        TNSOCR.Ocr_Create(CfgObj, out OcrObj);
        TNSOCR.Img_Create(OcrObj, out ImgObj);



        TNSOCR.Cfg_LoadOptions(CfgObj, "Config.dat");

        val = new StringBuilder(256);


        #region Language
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "English", (((bool)input.English)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Bulgarian", (((bool)input.Bulgarian)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Catalan", (((bool)input.Catalan)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Croatian", (((bool)input.Croatian)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Czech", (((bool)input.Czech)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Danish", (((bool)input.Danish)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Dutch", (((bool)input.Dutch)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Estonian", (((bool)input.Estonian)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Finnish", (((bool)input.Finnish)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "French", (((bool)input.French)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "German", (((bool)input.German)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Hungarian", (((bool)input.Hungarian)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Indonesian", (((bool)input.Indonesian)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Italian", (((bool)input.Italian)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Latvian", (((bool)input.Latvian)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Lithuanian", (((bool)input.Lithuanian)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Norwegian", (((bool)input.Norwegian)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Polish", (((bool)input.Polish)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Portuguese", (((bool)input.Portuguese)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Romanian", (((bool)input.Romanian)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Russian", (((bool)input.Russian)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Slovak", (((bool)input.Slovak)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Slovenian", (((bool)input.Slovenian)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Spanish", (((bool)input.Spanish)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Swedish", (((bool)input.Swedish)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Turkish", (((bool)input.Turkish)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Arabic", (((bool)input.Arabic)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Chinese_Simplified", (((bool)input.Chinese_Simplified)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Chinese_Traditional", (((bool)input.Chinese_Traditional)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Japanese", (((bool)input.Japanese)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/" + "Korean", (((bool)input.Korean)) ? "1" : "0");
        #endregion


        #region Configuration
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Zoning/FindBarcodes", (((bool)input.FindBarcodes)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "ImgAlizer/Inversion", (((bool)input.ImgInversion)) ? "2" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Zoning/DetectInversion", (((bool)input.ZonesInversion)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "ImgAlizer/SkewAngle", (((bool)input.DetectFixSkew)) ? "360" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "ImgAlizer/AutoRotate", (((bool)input.Rotation)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "ImgAlizer/NoiseFilter", (((bool)input.ImgNoiseFilter)) ? "1" : "0");

        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "PixLines/RemoveLines", (((bool)input.RemoveLines)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "PixLines/FindHorLines", (((bool)input.RemoveLines)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "PixLines/FindVerLines", (((bool)input.RemoveLines)) ? "1" : "0");

        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Main/GrayMode", (((bool)input.GrayMode)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Main/FastMode", (((bool)input.FastMode)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Binarizer/BinTwice", (((bool)input.BinTwice)) ? "1" : "0");


        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "WordAlizer/CorrectMixed", (((bool)input.CorrectMixed)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Dictionaries/UseDictionary", (((bool)input.Dictionary)) ? "1" : "0");
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Zoning/OneColumn", (((bool)input.OneColumn)) ? "1" : "0");


        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Main/EnabledChars", (((string)input.EnabledChars)));
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Main/DisabledChars", (((string)input.DisabledChars)));
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Binarizer/SimpleThr", (((string)input.BinarizationThreshold)));
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "WordAlizer/TextQual", (((string)input.TextQuality)));
        TNSOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Main/PdfDPI", (((string)input.PDFRenderingDPI)));



        #endregion


        IntPtr unmanagedPointer = Marshal.AllocHGlobal(fileByte.Length);
        Marshal.Copy(fileByte, 0, unmanagedPointer, fileByte.Length);
        // Call unmanaged code

        var res = TNSOCR.Img_LoadFromMemory(ImgObj, unmanagedPointer, fileByte.Length);
        Marshal.FreeHGlobal(unmanagedPointer);

        res = TNSOCR.Img_OCR(ImgObj, TNSOCR.OCRSTEP_FIRST, TNSOCR.OCRSTEP_ZONING - 1, TNSOCR.OCRFLAG_NONE);
        if (res > TNSOCR.ERROR_FIRST)
        {
            //Console.WriteLine("Img_OCR", res);

            //Console.ReadLine();
            if (ImgObj != 0)
            {
                TNSOCR.Img_Destroy(ImgObj);
            }
            if (OcrObj != 0)
            {
                TNSOCR.Ocr_Destroy(OcrObj);
            }
            if (CfgObj != 0)
            {
                TNSOCR.Cfg_Destroy(CfgObj);
            }
            TNSOCR.Engine_Uninitialize();
            return("");
        }


        res = TNSOCR.Img_OCR(ImgObj, TNSOCR.OCRSTEP_ZONING, TNSOCR.OCRSTEP_LAST, TNSOCR.OCRFLAG_NONE);
        if (res > TNSOCR.ERROR_FIRST)
        {
            //  Console.WriteLine("Img_OCR", res);
            // Console.ReadLine();
            if (ImgObj != 0)
            {
                TNSOCR.Img_Destroy(ImgObj);
            }
            if (OcrObj != 0)
            {
                TNSOCR.Ocr_Destroy(OcrObj);
            }
            if (CfgObj != 0)
            {
                TNSOCR.Cfg_Destroy(CfgObj);
            }
            TNSOCR.Engine_Uninitialize();
            return("");
        }
        var flags = TNSOCR.FMT_EDITCOPY;                          //or TNSOCR.FMT_EXACTCOPY
        var txt   = new StringBuilder(0);
        var n     = TNSOCR.Img_GetImgText(ImgObj, txt, 0, flags); //get buffer text length plus null-terminated zero
        txt = new StringBuilder(n + 1);
        TNSOCR.Img_GetImgText(ImgObj, txt, n + 1, flags);
        // Console.WriteLine(txt.ToString());

        int SvrObj;
        res = TNSOCR.Svr_Create(CfgObj, TNSOCR.SVR_FORMAT_XML, out SvrObj);
        if (res > TNSOCR.ERROR_FIRST)
        {
            // Console.WriteLine("Svr_Create", res);
            if (ImgObj != 0)
            {
                TNSOCR.Img_Destroy(ImgObj);
            }
            if (OcrObj != 0)
            {
                TNSOCR.Ocr_Destroy(OcrObj);
            }
            if (CfgObj != 0)
            {
                TNSOCR.Cfg_Destroy(CfgObj);
            }
            TNSOCR.Engine_Uninitialize();
            return("");
        }

        TNSOCR.Svr_NewDocument(SvrObj);
        res = TNSOCR.Svr_AddPage(SvrObj, ImgObj, 0);
        if (res > TNSOCR.ERROR_FIRST)
        {
            // Console.WriteLine("Svr_AddPage", res);
            TNSOCR.Svr_Destroy(SvrObj);
            if (ImgObj != 0)
            {
                TNSOCR.Img_Destroy(ImgObj);
            }
            if (OcrObj != 0)
            {
                TNSOCR.Ocr_Destroy(OcrObj);
            }
            if (CfgObj != 0)
            {
                TNSOCR.Cfg_Destroy(CfgObj);
            }
            TNSOCR.Engine_Uninitialize();
            return("");
        }
        // IntPtr demo = new IntPtr();
        //Array bytes = new byte[21126];

        res = TNSOCR.Svr_SaveToMemory(SvrObj, IntPtr.Zero, 0);
        var size = res;
        if (res > TNSOCR.ERROR_FIRST)
        {
            //  Console.WriteLine("Svr_SaveToFile", res);
            if (ImgObj != 0)
            {
                TNSOCR.Img_Destroy(ImgObj);
            }
            if (OcrObj != 0)
            {
                TNSOCR.Ocr_Destroy(OcrObj);
            }
            if (CfgObj != 0)
            {
                TNSOCR.Cfg_Destroy(CfgObj);
            }
            TNSOCR.Engine_Uninitialize();
            return("");
        }

        var demo = IntPtr.Zero;

        try
        {
            demo = Marshal.AllocHGlobal(size);
            res  = TNSOCR.Svr_SaveToMemory(SvrObj, demo, size);

            var managedArray2 = new byte[size];

            Marshal.Copy(demo, managedArray2, 0, size);
            var str5 = Encoding.Unicode.GetString(managedArray2);
            TNSOCR.Svr_Destroy(SvrObj);
            if (ImgObj != 0)
            {
                TNSOCR.Img_Destroy(ImgObj);
            }
            if (OcrObj != 0)
            {
                TNSOCR.Ocr_Destroy(OcrObj);
            }
            if (CfgObj != 0)
            {
                TNSOCR.Cfg_Destroy(CfgObj);
            }
            TNSOCR.Engine_Uninitialize();
            return(str5);
        }
        catch (Exception e)
        {
            return(e.Message);
        }
        finally
        {
            // Free unmanaged memory
            if (demo != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(demo);
            }
            if (ImgObj != 0)
            {
                TNSOCR.Img_Destroy(ImgObj);
            }
            if (OcrObj != 0)
            {
                TNSOCR.Ocr_Destroy(OcrObj);
            }
            if (CfgObj != 0)
            {
                TNSOCR.Cfg_Destroy(CfgObj);
            }
            TNSOCR.Engine_Uninitialize();
        }


        //here you can adjust configuration (scale image, select languages, etc) - refer to other C# sample projects
        //
    }