Пример #1
0
        public void Run(string imageFile, int JobNo = 0)
        {
            string          Msg = "";
            Bitmap          BMP;
            TOCRRESULTS     Results  = new TOCRRESULTS();
            TOCRJOBINFO2    JobInfo2 = new TOCRJOBINFO2();
            ImageProcessing image    = new ImageProcessing();

            JobInfo2.ProcessOptions.DisableCharacter = new short[256];

            IntPtr MMFhandle = IntPtr.Zero;

            using (FileStream stream = new FileStream(imageFile, FileMode.Open, FileAccess.Read))
            {
                BMP = new Bitmap(stream);
            }
            if (BMP.Width > 10000 || BMP.Height > 10000)
            {
                BMP = image.Resize(BMP, 5000, 5000, 100);
            }


            Width  = BMP.Width;
            Height = BMP.Height;
            OCR(ref JobNo, ref Msg, BMP, ref Results, ref JobInfo2, ref MMFhandle);
        }
Пример #2
0
        public TransymAccess(string logFile)
        {
            JobInfo2 = new TOCRJOBINFO2
            {
                ProcessOptions =
                {
                    DisableCharacter = new short[256],
                    StructId         =              1,
                    DeshadeOff       = 1
                }
            };

            var fileInfo = new FileInfo(logFile);

            if (!fileInfo.Exists)
            {
                if (fileInfo.Directory != null && !fileInfo.Directory.Exists)
                {
                    Directory.CreateDirectory(fileInfo.Directory.FullName);
                }
                using (var str = File.Create(logFile))
                {
                    str.Close();
                }
            }

            //TOCRSetConfig(TOCRCONFIG_DEFAULTJOB, TOCRCONFIG_SRV_THREADPRIORITY, ThreadPriority.Highest));
            //var i1= TOCRSetConfig(TOCRCONFIG_DEFAULTJOB, TOCRCONFIG_DLL_MUTEXWAIT, 5000);
            //var i2 = TOCRSetConfig(TOCRCONFIG_DEFAULTJOB, TOCRCONFIG_DLL_EVENTWAIT, 5000);
            //var i3 = TOCRSetConfig(TOCRCONFIG_DEFAULTJOB, TOCRCONFIG_SRV_MUTEXWAIT, 5000);
            var i4 = TOCRSetConfig(TOCRCONFIG_DEFAULTJOB, TOCRCONFIG_DLL_ERRORMODE, TOCRERRORMODE_LOG);
            var i5 = TOCRSetConfigStr(TOCRCONFIG_DEFAULTJOB, TOCRCONFIG_LOGFILE, logFile);

            //InitTOCR();
        }
Пример #3
0
        // Wait for the engine to complete by polling
        private bool OCRPoll(int JobNo, TOCRJOBINFO2 JobInfo2)
        {
            int   Status;
            int   JobStatus       = 0;
            int   ErrorMode       = 0;
            float Progress        = 0;
            int   AutoOrientation = 0;

            Status = TOCRDoJob2(JobNo, ref JobInfo2);
            if (Status == TOCR_OK)
            {
                do
                {
                    //Status = TOCRGetJobStatus(JobNo, JobStatus)
                    Status = TOCRGetJobStatusEx(JobNo, ref JobStatus, ref Progress, ref AutoOrientation);

                    // Do something whilst the OCR engine runs
                    //Application.DoEvents(); System.Threading.Thread.Sleep(100); Application.DoEvents();
                    Console.WriteLine("Progress" + Convert.ToString(Convert.ToInt32(Progress * 100)) + "%");
                } while (Status == TOCR_OK & JobStatus == TOCRJOBSTATUS_BUSY);
            }

            if (Status == TOCR_OK & JobStatus == TOCRJOBSTATUS_DONE)
            {
                return(true);
            }
            else
            {
                // If something has gone wrong display a message
                // (Check that the OCR engine hasn't already displayed a message)
                TOCRGetConfig(JobNo, TOCRCONFIG_DLL_ERRORMODE, ref ErrorMode);
                if (ErrorMode == TOCRERRORMODE_NONE)
                {
                    StringBuilder Msg = new StringBuilder(TOCRJOBMSGLENGTH);
                    TOCRGetJobStatusMsg(JobNo, Msg);
                    //MessageBox.Show(Msg.ToString(), "OCRPoll", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                return(false);
            }
        }
Пример #4
0
        // Wait for the engine to complete
        private bool OCRWait(int JobNo, TOCRJOBINFO2 JobInfo2, ref int error)
        {
            int Status    = 0;
            int JobStatus = 0;
            int ErrorMode = 0;

            Status = TOCRDoJob2(JobNo, ref JobInfo2);


            if (Status != TOCR_OK) // 9=connection broken
            {
                RestartTOCR();
                Status = TOCRDoJob2(JobNo, ref JobInfo2);
            }

            if (Status == TOCR_OK)
            {
                Status = TOCRWaitForJob(JobNo, ref JobStatus);
            }
            else
            {
                throw new TransymException("Transym broken. Transym status: '" + Status + "'");
            }

            if (Status == TOCR_OK && JobStatus == TOCRJOBSTATUS_DONE)
            {
                return(true);
            }
            else
            {
                // If something has gone wrong display a message
                // (Check that the OCR engine hasn't already displayed a message)
                TOCRGetConfig(JobNo, TOCRCONFIG_DLL_ERRORMODE, ref ErrorMode);
                if (ErrorMode == TOCRERRORMODE_NONE)
                {
                    TOCRGetJobStatus(JobNo, ref error);
                }
                return(false);
            }
        }
Пример #5
0
        public void Run(Bitmap bmp, int JobNo = 0)
        {
            string          Msg      = "";
            TOCRRESULTS     Results  = new TOCRRESULTS();
            TOCRJOBINFO2    JobInfo2 = new TOCRJOBINFO2();
            ImageProcessing image    = new ImageProcessing();

            JobInfo2.ProcessOptions.DisableCharacter = new short[256];

            IntPtr MMFhandle = IntPtr.Zero;



            Rectangle zone    = new Rectangle(0, 0, bmp.Width, bmp.Height);
            Bitmap    bmpZone = image.Crop(bmp, zone);

            //bmpZone.Save(imageFile + "zone_" + Guid.NewGuid() + ".jpg");

            Width  = bmp.Width;
            Height = bmp.Height;
            OCR(ref JobNo, ref Msg, bmpZone, ref Results, ref JobInfo2, ref MMFhandle);
        }
Пример #6
0
        public void Run(string imageFile, int x, int y, int width, int height, string zoneName = "", int JobNo = 0)
        {
            string          Msg      = "";
            TOCRRESULTS     Results  = new TOCRRESULTS();
            TOCRJOBINFO2    JobInfo2 = new TOCRJOBINFO2();
            ImageProcessing image    = new ImageProcessing();

            JobInfo2.ProcessOptions.DisableCharacter = new short[256];

            IntPtr MMFhandle = IntPtr.Zero;
            Bitmap bmp       = image.LoadImageFromFile(imageFile);

            //crop image
            Rectangle zone    = new Rectangle(x, y, width, height);
            Bitmap    bmpZone = image.Crop(bmp, zone);

            bmpZone.Save(imageFile + "zone_" + zoneName + "_" + Guid.NewGuid() + ".jpg");
            //bmpZone.Save(DateTime.Now.ToString("yyyyMMddhhmmssfff") + "zone_"+ zoneName + "_"+ Guid.NewGuid() + ".jpg");
            Width  = bmp.Width;
            Height = bmp.Height;
            OCR(ref JobNo, ref Msg, bmpZone, ref Results, ref JobInfo2, ref MMFhandle);
        }
Пример #7
0
        private void OCR(ref int JobNo, ref string Msg, Bitmap BMP, ref TOCRRESULTS Results, ref TOCRJOBINFO2 JobInfo2, ref IntPtr MMFhandle)
        {
            int Status;

            MMFhandle = ConvertBitmapToMMF(BMP);
            //MMFhandle = ConvertBitmapToMMF2(BMP);

            if (!(MMFhandle.Equals(IntPtr.Zero)))
            {
                TOCRSetConfig(TOCRCONFIG_DEFAULTJOB, TOCRCONFIG_DLL_ERRORMODE, TOCRERRORMODE_LOG);
                JobInfo2.JobType = TOCRJOBTYPE_MMFILEHANDLE;
                Status           = TOCRInitialise(ref JobNo);


                JobInfo2.ProcessOptions.StructId = 3;

                if (Status == TOCR_OK)
                {
                    JobInfo2.hMMF = MMFhandle;
                    if (OCRWait(JobNo, JobInfo2))
                    {
                        if (GetResults(JobNo, ref Results))
                        {
                            if (FormatResults(Results, ref Msg))
                            {
                                ////MessageBox.Show(Msg, "Example 4", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                    }
                    TOCRShutdown(JobNo);
                }
                CloseHandle(MMFhandle);
            }
        }
Пример #8
0
        // Wait for the engine to complete
        private bool OCRWait(int JobNo, TOCRJOBINFO2 JobInfo2)
        {
            int   Status;
            int   JobStatus   = 0;
            int   ErrorMode   = 0;
            int   Orientation = 0;
            float Progress    = 0;
            //Status = TOCRDoJob2(JobNo, ref JobInfo2);

            DateTime time = DateTime.Now;

            if (TOCRDoJob2(JobNo, ref JobInfo2) == TOCR_OK)
            {
                do
                {
                    Status = TOCRGetJobStatusEx(JobNo, ref JobStatus, ref Progress, ref Orientation);

                    if (Status == TOCR_OK)
                    {
                        Status = TOCRWaitForJob(JobNo, ref JobStatus);
                        break;
                    }
                    if ((DateTime.Now - time).Minutes == 5)
                    {
                        break;
                    }
                } while (Status == TOCR_OK & JobStatus == TOCRJOBSTATUS_BUSY);

                if (Status == TOCR_OK && JobStatus == TOCRJOBSTATUS_DONE)
                {
                    Status = TOCRGetJobStatusEx(JobNo, ref JobStatus, ref Progress, ref Orientation);

                    //Convert Orientation to degrees
                    switch (Orientation)
                    {
                    case TOCRJOBORIENT_90: Rotate = 90;
                        break;

                    case TOCRJOBORIENT_180: Rotate = 180;
                        break;

                    case TOCRJOBORIENT_270: Rotate = 270;
                        break;

                    default:
                        break;
                    }
                    return(true);
                }
                else
                {
                    // If something has gone wrong display a message
                    // (Check that the OCR engine hasn't already displayed a message)
                    TOCRGetConfig(JobNo, TOCRCONFIG_DLL_ERRORMODE, ref ErrorMode);
                    if (ErrorMode == TOCRERRORMODE_NONE)
                    {
                        StringBuilder Msg = new StringBuilder(TOCRJOBMSGLENGTH);
                        TOCRGetJobStatusMsg(JobNo, Msg);
                    }
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Пример #9
0
 public static extern int TOCRDoJob2(int JobNo, ref TOCRJOBINFO2 JobInfo2);