コード例 #1
0
        // OVERLOADED function to convert results to a string
        private static bool FormatResults(TOCRRESULTS Results, ref string Answer)
        {
            Answer = "";

            if (Results.Hdr.NumItems > 0)
            {
                for (int ItemNo = 0; ItemNo < Results.Hdr.NumItems; ItemNo++)
                {
                    if (Results.Item[ItemNo].OCRCha == 13)
                    {
                        Answer = Answer + Environment.NewLine;
                    }
                    else
                    {
                        Answer = Answer + Convert.ToChar(Results.Item[ItemNo].OCRCha);
                    }
                }
                return(true);
            }
            else
            {
                MessageBox.Show("No results returned", "FormatResults", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return(false);
            }
        }
コード例 #2
0
ファイル: TOCR.cs プロジェクト: tieuloitu1/NewProject
        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);
            }
        }
コード例 #3
0
ファイル: TOCR.cs プロジェクト: tieuloitu1/NewProject
        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);
        }
コード例 #4
0
ファイル: TOCR.cs プロジェクト: tieuloitu1/NewProject
        // OVERLOADED function to convert results to a string
        private bool FormatResults(TOCRRESULTS Results, ref string Msg)
        {
            if (Results.Hdr.NumItems > 0)
            {
                List <WordInfo>  listWord      = new List <WordInfo>();
                List <Character> listCharacter = new List <Character>();
                int      index = 1;
                DateTime time  = DateTime.Now;
                for (int ItemNo = 0; ItemNo < Results.Hdr.NumItems; ItemNo++)
                {
                    if (Results.Item[ItemNo].OCRCha == 13)
                    {
                        Msg = Msg + Environment.NewLine;
                    }
                    else
                    {
                        Msg = Msg + Convert.ToChar(Results.Item[ItemNo].OCRCha);
                    }


                    SetOCRRow(ref Results, ref listWord, ref listCharacter, ref index, ItemNo);
                }



                Message = Msg;

                /*Encoding UNICODE = Encoding.Unicode;
                 * Encoding ANSI = Encoding.GetEncoding(1252);
                 * byte[] ansibytes = new Byte[Results.Hdr.NumItems];
                 * byte[] unicodebytes = new Byte[Results.Hdr.NumItems];
                 *
                 * for (int ItemNo = 0; ItemNo < Results.Hdr.NumItems; ItemNo++)
                 *              {
                 *  if (Results.Item[ItemNo].OCRCha == 13)
                 *      ansibytes[ItemNo] = (byte)'\n';
                 *  else
                 *  {
                 *      ansibytes[ItemNo] = (byte)Results.Item[ItemNo].OCRCha;
                 *  }
                 *              }
                 * unicodebytes = Encoding.Convert(ANSI, UNICODE, ansibytes);
                 * Msg = UNICODE.GetString(unicodebytes);
                 */
                return(true);
            }
            else
            {
                //MessageBox.Show("No results returned", "FormatResults", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return(false);
            }
        }
コード例 #5
0
        private string GetOCRfromBMP(Bitmap bmp)
        {
            int         Status;
            int         JobNo  = 0;
            string      Answer = "";
            Bitmap      BMP;
            TOCRRESULTS Results = new TOCRRESULTS();
            TOCRJOBINFO JobInfo = new TOCRJOBINFO();

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

            IntPtr MMFhandle = IntPtr.Zero;

            //BMP = new Bitmap(mSample_BMP_file);
            BMP = bmp;

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

            if (!(MMFhandle.Equals(IntPtr.Zero)))
            {
                TOCRSetErrorMode(TOCRDEFERRORMODE, TOCRERRORMODE_MSGBOX);
                JobInfo.InputFile = "";
                JobInfo.JobType   = TOCRJOBTYPE_MMFILEHANDLE;
                Status            = TOCRInitialise(ref JobNo);
                if (Status == TOCR_OK)
                {
                    JobInfo.PageNo = MMFhandle.ToInt32();
                    if (OCRWait(JobNo, JobInfo))
                    {
                        if (GetResults(JobNo, ref Results))
                        {
                            if (FormatResults(Results, ref Answer))
                            {
                                //MessageBox.Show(Answer, "Example 3", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                    }
                    TOCRShutdown(TOCRSHUTDOWNALL);
                }
                CloseHandle(MMFhandle);
            }
            return(Answer);
        }
コード例 #6
0
ファイル: TOCR.cs プロジェクト: tieuloitu1/NewProject
        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);
        }
コード例 #7
0
ファイル: TOCR.cs プロジェクト: tieuloitu1/NewProject
        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);
        }
コード例 #8
0
        // OVERLOADED function to retrieve the results from the service process and load into 'Results'
        // Remember the character numbers returned refer to the Windows character set.
        private static bool GetResults(int JobNo, ref TOCRRESULTS Results)
        {
            int ResultsInf = 0; // number of bytes needed for results

            byte[]   Bytes;
            int      Offset;
            bool     RetStatus = false;
            GCHandle BytesGC;

            System.IntPtr AddrOfItemBytes;

            Results.Hdr.NumItems = 0;
            if (TOCRGetJobResults(JobNo, ref ResultsInf, 0) == TOCR_OK)
            {
                if (ResultsInf > 0)
                {
                    Bytes = new Byte[ResultsInf - 1];
                    // pin the Bytes array so that TOCRGetJobResults can write to it
                    BytesGC = GCHandle.Alloc(Bytes, GCHandleType.Pinned);
                    if (TOCRdeclares.TOCRGetJobResults(JobNo, ref ResultsInf, ref Bytes[0]) == TOCR_OK)
                    {
                        Results.Hdr = ((TOCRRESULTSHEADER)(Marshal.PtrToStructure(BytesGC.AddrOfPinnedObject(), typeof(TOCRRESULTSHEADER))));
                        if (Results.Hdr.NumItems > 0)
                        {
                            Results.Item = new TOCRRESULTSITEM[Results.Hdr.NumItems];
                            Offset       = Marshal.SizeOf(typeof(TOCRRESULTSHEADER));
                            for (int ItemNo = 0; ItemNo <= Results.Hdr.NumItems - 1; ItemNo++)
                            {
                                AddrOfItemBytes      = Marshal.UnsafeAddrOfPinnedArrayElement(Bytes, Offset);
                                Results.Item[ItemNo] = ((TOCRRESULTSITEM)(Marshal.PtrToStructure(AddrOfItemBytes, typeof(TOCRRESULTSITEM))));
                                Offset = Offset + Marshal.SizeOf(typeof(TOCRRESULTSITEM));
                            }
                        }

                        RetStatus = true;

                        // Double check results
                        if (Results.Hdr.StructId == 0)
                        {
                            if (Results.Hdr.NumItems > 0)
                            {
                                if (Results.Item[0].StructId != 0)
                                {
                                    MessageBox.Show("Wrong results item structure Id " + Results.Item[0].StructId.ToString(), "GetResults", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                                    Results.Hdr.NumItems = 0;
                                    RetStatus            = false;
                                }
                            }
                        }
                        else
                        {
                            MessageBox.Show("Wrong results header structure Id " + Results.Item[0].StructId.ToString(), "GetResults", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            Results.Hdr.NumItems = 0;
                            RetStatus            = false;
                        }
                    }
                    BytesGC.Free();
                }
            }
            return(RetStatus);
        }
コード例 #9
0
ファイル: TOCR.cs プロジェクト: tieuloitu1/NewProject
        private void SetOCRRow(ref TOCRRESULTS Results, ref List <WordInfo> listWord, ref List <Character> listCharacter, ref int index, int ItemNo)
        {
            int wordIndex = 1;

            if (Convert.ToChar(Results.Item[ItemNo].OCRCha) != 13)
            {
                if (Convert.ToChar(Results.Item[ItemNo].OCRCha) != ' ')
                {
                    Character character2 = new Character()
                    {
                        Value      = Convert.ToChar(Results.Item[ItemNo].OCRCha).ToString(),
                        Confidence = Results.Item[ItemNo].Confidence,
                        XDim       = Results.Item[ItemNo].XDim,
                        XPos       = Results.Item[ItemNo].XPos,
                        YDim       = Results.Item[ItemNo].YDim,
                        YPos       = Results.Item[ItemNo].YPos,
                    };
                    listCharacter.Add(character2);
                }
                if (Convert.ToChar(Results.Item[ItemNo].OCRCha) == ' ')
                {
                    WordInfo word = new WordInfo()
                    {
                        //ListCharacter = listCharacter
                        Word          = string.Join("", listCharacter.Select(x => x.Value)),
                        ListCharacter = listCharacter,
                        Height        = listCharacter.Max(x => x.YDim),
                        Width         = listCharacter.Count == 1 ? listCharacter.First().XDim : listCharacter.Last().XPos - listCharacter.First().XPos + listCharacter.Last().XDim,
                        XPos          = listCharacter.First().XPos,
                        YPos          = listCharacter.First().YPos,
                        Confidence    = listCharacter.Sum(x => x.Confidence) / listCharacter.Count * 100,
                        Position      = wordIndex,
                        CorrectWord   = string.Empty,
                    };
                    listCharacter = new List <Character>();
                    listWord.Add(word);
                    wordIndex++;
                }
            }
            else
            {
                WordInfo word = new WordInfo()
                {
                    //ListCharacter = listCharacter
                    Word          = string.Join("", listCharacter.Select(x => x.Value)),
                    ListCharacter = listCharacter,
                    Height        = listCharacter.Max(x => x.YDim),
                    Width         = listCharacter.Count == 1 ? listCharacter.First().XDim : listCharacter.Last().XPos - listCharacter.First().XPos + listCharacter.Last().XDim,
                    XPos          = listCharacter.First().XPos,
                    YPos          = listCharacter.First().YPos,
                    Confidence    = listCharacter.Sum(x => x.Confidence) / listCharacter.Count * 100,
                    CorrectWord   = string.Empty,
                    Position      = wordIndex,
                };
                listCharacter = new List <Character>();
                listWord.Add(word);
                wordIndex++;
                OCRRow info = new OCRRow()
                {
                    ListWord   = listWord,
                    Row        = string.Join(" ", listWord.Select(x => x.Word)),
                    Position   = index,
                    CorrectRow = string.Empty,
                    Confidence = listWord.Sum(x => x.Confidence) / listWord.Count,
                };
                listOCRRow.Add(info);

                listWord = new List <WordInfo>();
                index++;
            }
            if (ItemNo == Results.Hdr.NumItems - 1)
            {
                WordInfo word = new WordInfo()
                {
                    //ListCharacter = listCharacter
                    Word          = string.Join("", listCharacter.Select(x => x.Value)),
                    ListCharacter = listCharacter,
                    Height        = listCharacter.Max(x => x.YDim),
                    Width         = listCharacter.Count == 1 ? listCharacter.First().XDim : listCharacter.Last().XPos - listCharacter.First().XPos + listCharacter.Last().XDim,
                    XPos          = listCharacter.First().XPos,
                    YPos          = listCharacter.First().YPos,
                    Confidence    = listCharacter.Sum(x => x.Confidence) / listCharacter.Count * 100,
                    CorrectWord   = string.Empty,
                    Position      = wordIndex,
                };
                listCharacter = new List <Character>();
                listWord.Add(word);
                wordIndex++;
                OCRRow info = new OCRRow()
                {
                    ListWord   = listWord,
                    Row        = string.Join(" ", listWord.Select(x => x.Word)),
                    Position   = index,
                    CorrectRow = string.Empty,
                    Confidence = listWord.Sum(x => x.Confidence) / listWord.Count,
                };
                listOCRRow.Add(info);

                listWord = new List <WordInfo>();
                index++;
            }
        }