// HOW-TO: How to pass images between processes or to and from FineReader Engine
        public static void How_to_pass_images_between_processes_or_to_and_from_FineReader_Engine(IEngine engine)
        {
            trace("Prepare an image in FlexiCapture Engine format...");
            IDocument      document          = PrepareNewRecognizedDocument(engine);
            IImageDocument pageImageDocument = document.Pages[0].ReadOnlyImage;

            trace("Serialize the image document in memory...");
            // You are responsible for freeing the allocated memory when it is no longer needed
            int hGlobal = pageImageDocument.SaveImageDocToMemory();

            try {
                // The handle to the serialized image can be used in the same process (for example for
                // interoperability with FineReader Engine) or passed (marshalled) to a different process if required

                trace("Deserialize the image document from memory...");
                // To deserialize the image in the target context use LoadImageDocFromMemory method.
                // An identical method is present in FineReader Engine which can be used to convert
                // images to FREngine format
                IImageDocument documentCopy = engine.LoadImageDocFromMemory(hGlobal);
            } finally {
                // Do not forget to free the memory. The memory can be freed either in the same process where
                // it was allocated or in the process where the image is consumed
                trace("Free the memory...");
                Kernel32.GlobalFree((IntPtr)hGlobal);
            }
        }
        // HOW-TO: Use HBITMAPs returned by FlexiCapture Engine objects
        public static void How_to_use_HBITMAPs_returned_by_FlexiCapture_Engine_objects(IEngine engine)
        {
            trace("Prepare an image in FlexiCapture Engine format...");
            IDocument      document          = PrepareNewRecognizedDocument(engine);
            IImageDocument pageImageDocument = document.Pages[0].ReadOnlyImage;
            IImage         pageBWImage       = pageImageDocument.BlackWhiteImage;

            trace("Request the bitmap for a region in the image...");
            // The method returns HBITMAP handle to a GDI bitmap object. You take ownership of
            // the object which means that you MUST free the object when it is no longer needed
            // (see DeleteObject below). It is recommended that the bitmap is freed
            // as soon as possible. It is also worth noting that in this sample between retrieving
            // the bitmap and freeing it there is no code that can normally throw exceptions.
            // If it were not the case, it would be necessary to use try...finally to guarantee
            // freeing the bitmap if an exception was thrown.
            IntPtr hBitmap = new IntPtr(pageBWImage.GetPicture(null, 0));

            trace("Create an Image from the bitmap...");
            // The FromHbitmap method makes a COPY of the GDI bitmap. It does not take ownership of
            // the original GDI bitmap and you are still responsible for freeing it!
            using (System.Drawing.Image image = System.Drawing.Image.FromHbitmap(hBitmap)) {
                trace("Explicitly DELETE the bitmap or there will be a resource leak!");
                // The Gdi32 class (see definition) contains the required WinAPI function (imported from Gdi32.dll)
                Gdi32.DeleteObject(hBitmap);

                // It is highly recommended that you correctly manage the image disposal and not leave
                // it to garbage collector. If the image is used in a visual control, it should be
                // disposed of in the Dispose method of the control. If the image is used in some
                // procedure, you should enclose its usage in "using" statement as in this example.

                // ...
            }
        }
        // HOW-TO: Save images for document fields
        public static void How_to_save_images_for_document_fields(IEngine engine)
        {
            trace("Prepare a sample document...");
            IDocument document = PrepareNewRecognizedDocument(engine);

            trace("Select an arbitrary field...");
            IField field = document.Sections[0].Children[0];

            assert(field.Name == "InvoiceNumber");

            trace("Get the region and the page index for the field...");
            // Generally a field might correspond to several regions (blocks) on the same page or on
            // different pages. Simple fields (simple picture or single line text)usually have one region
            assert(field.Blocks.Count == 1);
            IBlock  firstBlock = field.Blocks.Item(0);
            int     pageIndex  = firstBlock.Page.Index;
            IRegion region     = firstBlock.Region;

            // Knowing the field's page index and geometry we can "scroll" to the field image
            // in a custom document view, or extract the region from the page image and use it
            // as required. In this example we will use this information to save the field image
            // to a file

            trace("Get the page image...");
            IPage page = document.Pages[pageIndex];
            // Get the page image in .NET Framework format
            IImageDocument pageImageDocument = page.ReadOnlyImage;
            IImage         bwImage           = pageImageDocument.BlackWhiteImage;
            IntPtr         hBitmap           = new IntPtr(bwImage.GetPicture(null, GetPictureFlags.GP_ScaleToGray));

            using (System.Drawing.Image pageImage = System.Drawing.Image.FromHbitmap(hBitmap)) {
                Gdi32.DeleteObject(hBitmap);

                trace("Extract the field image...");
                // Extract a region from the page image using standard .NET Framework tools
                assert(region.Count == 1);                   // The region contains one rectangle

                int left   = region.get_Left(0);
                int right  = region.get_Right(0);
                int top    = region.get_Top(0);
                int bottom = region.get_Bottom(0);
                int width  = right - left;
                int height = bottom - top;

                using (Bitmap bmp = new Bitmap(width, height)) {
                    using (Graphics canvas = Graphics.FromImage(bmp)) {
                        canvas.DrawImage(pageImage,
                                         new Rectangle(0, 0, width, height),
                                         new Rectangle(left, top, width, height),
                                         GraphicsUnit.Pixel);
                    }
                    bmp.Save(SamplesFolder + "\\FCEExport\\InvoiceNumber.bmp");
                }
            }
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            JsonCreator creatorJson = new JsonCreator();
            PngCreator  creatorPng  = new PngCreator();
            TxtCreator  creatorTxt  = new TxtCreator();

            Console.WriteLine("Создание файлов с помощью фабрик: ");
            IDocument txt_1 = creatorTxt.CreateDocument();

            IDocument json_1 = creatorTxt.CreateDocument();

            IImageDocument png_1 = creatorPng.CreateImageDocument();

            Console.ReadKey();
        }
Ejemplo n.º 5
0
        private static Tuple <Dictionary <string, object>, List <object> > ReadDocData(IDocument document)
        {
            //ImgDatas imgDatas = new ImgDatas();


            Dictionary <string, object> imgDatas = new Dictionary <string, object>();
            List <object> listData = new List <object>();


            IImageDocument pageImageDocument = document.Pages[0].ReadOnlyImage;
            IImage         pageColorImage    = pageImageDocument.ColorImage;
            IHandle        hBitmap           = pageColorImage.GetPicture(null, 0);

            processedImage = System.Drawing.Image.FromHbitmap(hBitmap.Handle);

            IField firstSection = document.Sections[0];

            addDocumentNodeChildren(imgDatas, listData, firstSection.Children);

            return(new Tuple <Dictionary <string, object>, List <object> >(imgDatas, listData));
        }
        // HOW-TO: Save images for document fields
        public static void How_to_save_images_for_document_fields_LPAREN2RPAREN(IEngine engine)
        {
            // If your development tool does not have rich functionality for working with
            // images you can try using WriteToFile method of Image object as follows

            trace("Prepare a sample document...");
            IDocument document = PrepareNewRecognizedDocument(engine);

            trace("Select an arbitrary field...");
            IField field = document.Sections[0].Children[0];

            assert(field.Name == "InvoiceNumber");

            trace("Get the region and the page index for the field...");
            // Generally a field might correspond to several regions (blocks) on the same page or on
            // different pages. Simple fields (simple picture or single line text)usually have one region
            assert(field.Blocks.Count == 1);
            IBlock  firstBlock = field.Blocks.Item(0);
            int     pageIndex  = firstBlock.Page.Index;
            IRegion region     = firstBlock.Region;

            // Knowing the field's page index and geometry we can "scroll" to the field image
            // in a custom document view, or extract the region from the page image and use it
            // as required. In this example we will use this information to save the field image
            // to a file

            trace("Get the page image...");
            IPage page = document.Pages[pageIndex];
            // Get the page image in .NET Framework format
            IImageDocument pageImageDocument = page.ReadOnlyImage;
            IImage         bwImage           = pageImageDocument.BlackWhiteImage;

            trace("Extract the field image...");
            IImageModification modification = engine.CreateImageModification();

            modification.ClipRegion = region;

            bwImage.WriteToFile(SamplesFolder + "\\FCEExport\\InvoiceNumber.tif",
                                ImageFileFormatEnum.IFF_Tif, modification, ImageCompressionTypeEnum.ICT_CcittGroup4, null);
        }