Ejemplo n.º 1
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 TOcrResultStructures.TOcrResults Results, ref int error)
        {
            int ResultsInf = 0; // number of bytes needed for results

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

            Results.Hdr.NumItems = 0;
            if (TOCRGetJobResults(JobNo, ref ResultsInf, IntPtr.Zero) == 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 (TOCRGetJobResults(JobNo, ref ResultsInf, BytesGC.AddrOfPinnedObject()) == TOCR_OK)
                    {
                        Results.Hdr = ((TOcrResultStructures.TOcrResultsHeader)(Marshal.PtrToStructure(BytesGC.AddrOfPinnedObject(), typeof(TOcrResultStructures.TOcrResultsHeader))));
                        if (Results.Hdr.NumItems > 0)
                        {
                            Results.Item = new TOcrResultStructures.TOcrResultsItem[Results.Hdr.NumItems];
                            Offset       = Marshal.SizeOf(typeof(TOcrResultStructures.TOcrResultsHeader));
                            for (int ItemNo = 0; ItemNo <= Results.Hdr.NumItems - 1; ItemNo++)
                            {
                                AddrOfItemBytes      = Marshal.UnsafeAddrOfPinnedArrayElement(Bytes, Offset);
                                Results.Item[ItemNo] = ((TOcrResultStructures.TOcrResultsItem)(Marshal.PtrToStructure(AddrOfItemBytes, typeof(TOcrResultStructures.TOcrResultsItem))));
                                Offset = Offset + Marshal.SizeOf(typeof(TOcrResultStructures.TOcrResultsItem));
                            }
                        }

                        RetStatus = true;

                        // Double check results
                        if (Results.Hdr.StructId == 0)
                        {
                            if (Results.Hdr.NumItems > 0)
                            {
                                if (Results.Item[0].StructId != 0)
                                {
                                    error = -1;//ResultStructureError;
                                    Results.Hdr.NumItems = 0;
                                    RetStatus            = false;
                                }
                            }
                        }
                        else
                        {
                            error = -2;//ResultHeaderStructureError;
                            Results.Hdr.NumItems = 0;
                            RetStatus            = false;
                        }
                    }
                    BytesGC.Free();
                }
            }
            return(RetStatus);
        }
Ejemplo n.º 2
0
        public TOcrResultStructures.TOcrResults Ocr(string imgFile)
        {
            int error = 0;

            TOcrResultStructures.TOcrResults Results = new TOcrResultStructures.TOcrResults();

            JobInfo2.InputFile = imgFile;
            JobInfo2.JobType   = TOCRJOBTYPE_TIFFFILE;

            if (!OCRWait(JobNo, JobInfo2, ref error))
            {
                throw new TransymException("Unknown Transym Error. Engine did not respond. Transym error code: " + error);
            }

            if (!GetResults(JobNo, ref Results, ref error))
            {
                throw new TransymException("Transym Error. Could not get result. Transym error code: " + error);
            }

            if (error != 0)
            {
                throw new TransymException("Could not ocr stream. Transym error code: " + error);
            }

            return(Results);
        }
Ejemplo n.º 3
0
        // Demonstrates how to OCR an image using a memory mapped file created here
        public TOcrResultStructures.TOcrResults OcrByStream(Stream imgStream)
        {
            var status = TOCRInitialise(ref JobNo);

            if (status != TOCR_OK)
            {
                throw new TransymException("Transym could not be started. Transym error code: " + status);
            }

            try
            {
                int    error = 0;
                Bitmap BMP;
                TOcrResultStructures.TOcrResults Results = new TOcrResultStructures.TOcrResults();
                JobInfo2.JobType = TOCRJOBTYPE_MMFILEHANDLE;

                IntPtr MMFhandle = IntPtr.Zero;

                imgStream.Seek(0, SeekOrigin.Begin);
                BMP = new Bitmap(imgStream);

                MMFhandle = ConvertBitmapToMMF(BMP, true, true);

                if (MMFhandle.Equals(IntPtr.Zero))
                {
                    return(Results);
                }

                try
                {
                    TOCRSetConfig(TOCRCONFIG_DEFAULTJOB, TOCRCONFIG_DLL_ERRORMODE, TOCRERRORMODE_MSGBOX);

                    JobInfo2.hMMF = MMFhandle;

                    if (!OCRWait(JobNo, JobInfo2, ref error))
                    {
                        throw new TransymException("Unknown Transym Error. Engine did not respond. Transym error code: " + error);
                    }

                    if (!GetResults(JobNo, ref Results, ref error))
                    {
                        throw new TransymException("Transym Error. Could not get result. Transym error code: " + error);
                    }

                    if (error != 0)
                    {
                        throw new TransymException("Could not ocr stream. Transym error code: " + error);
                    }

                    return(Results);
                }
                finally
                {
                    CloseHandle(MMFhandle);
                }
            }
            catch (TransymException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new TransymException("Unknown Transym Error", e);
            }
            finally
            {
                Stop();
                var task = Task.Factory.StartNew(() => { Stop(); });
                if (!task.Wait(5000))
                {
                    throw new TransymException("Timedout trying to process shutdown transym");
                }
            }
        } // Example4()