Community

[FR Engine 11 SDK] How to reuse ImageDocument object ? Answered

Hello,

I have an image on which I have to perform two processes:

1) Convert it to PDF.

2) Perform a text extraction to a txt file.

So I create a single ImageDocument.

I then create a FRDocument adding the ImageDocument to it and perform the PDF conversion (DocumentConversion_Accuracy): everything works well.

I then create another FRDocument adding my ImageDocument to perform the text extraction (TextExtraction_Accuracy) -> I get an exception:

com.abbyy.FREngine.EngineException: Invalid pointer at com.abbyy.FREngine.IFRDocument.AddImageDocument(Native Method)

I resolved this by creating two distinct ImageDocument objects.

This is not really great as I have to call image preprocessing methods on both objects (crop, enhance local contrast, ....) while they are exactly the same.

Is there a way to reuse ImageDocument objects to avoid this code duplication ?

Was this article helpful?

0 out of 0 found this helpful

Comments

4 comments

  • Avatar
    Natalia Karaseva

    Hello!

    I've just tried to implement the described behavior in Hello sample. It works correctly. Please, see below my code snippet in C#:

    // Create ImageDocument
    FREngine.DocumentInfo docInfo = engineLoader.Engine.CreateDocumentInfo();
    FREngine.ImageDocument imageDocument = engineLoader.Engine.OpenImageFile(imagePath, null, null, docInfo);
    
    // Process document #1 with DocumentConversion_Accuracy profile
    engineLoader.Engine.LoadPredefinedProfile("DocumentConversion_Accuracy");
    FREngine.FRDocument document1 = engineLoader.Engine.CreateFRDocument();
    document1.AddImageDocument(imageDocument);
    
    // Recognize document #1 and save results
    document1.Process( null );
    document1.Export( to PDF );
    
    // Process document #2 with TextExtraction_Accuracy profile
    engineLoader.Engine.LoadPredefinedProfile("TextExtraction_Accuracy");
    FREngine.FRDocument document2 = engineLoader.Engine.CreateFRDocument();
    document2.AddImageDocument(imageDocument);
    
    // Recognize document #2 and save results
    document2.Process(null);
    document2.Export(to TXT);
    

    Could you please specify the issue in details? What methods do you call? Where does the error message occur (code snippet will be useful)?

    0
  • Avatar
    maol

    Hello,

    After some testing it seems the problem happens only when loading the image file from memory by using the OpenImageFileFromMemory() method. The error occurs when calling document.AddImageDocument(imageDocument) for the second time

    Here is the code to reproduce the error (it's in Java) :

    public void test() throws Exception {
    
        IFRDocument document = null;
    
        try {
            //Load engine
            engine = Engine.Load(DLL_FOLDER, DEV_SERIAL_NUMBER);
    
            //Create ImageDocument from file in memory 
            //--> This produce the Invalid pointer exception
            byte[] fileContent = Files.readAllBytes(Paths.get("src/img/laca.jpg"));
            IImageDocument imageDocument = 
                    engine.OpenImageFileFromMemory(fileContent, null, null, null);
    
            //Create ImageDocument from file --> This is ok
            //IImageDocument imageDocument = 
                engine.OpenImageFile("src/img/laca.jpg", null, null, null);
    
            //Process document 1 with DocumentConversion_Accuracy profile
            engine.LoadPredefinedProfile("DocumentConversion_Accuracy");
    
            document = engine.CreateFRDocument();
            document.AddImageDocument(imageDocument);
    
            //Recognize document 1 and save results to PDF
            document.Process(null);
    
            IPDFExportParams pdfExportParams = engine.CreatePDFExportParams();
            pdfExportParams.setScenario( PDFExportScenarioEnum.PES_Balanced );
            document.Export("src/img/laca.pdf", FileExportFormatEnum.FEF_PDF, pdfExportParams);
    
            document.Close();
    
            //Process document 2 with TextExtraction_Accuracy profile
            engine.LoadPredefinedProfile("TextExtraction_Accuracy");
    
            document = engine.CreateFRDocument();
            document.AddImageDocument(imageDocument);
    
            //Recognize document 2 and save results to TXT
            document.Process(null);
            document.Export("src/img/laca.txt", FileExportFormatEnum.FEF_TextUnicodeDefaults, null);
        }
        finally {
            if(document != null) {
                document.Close();
            }
    
            if(engine != null) {
                // Unload ABBYY FineReader Engine
                engine = null;
                System.gc();
                System.runFinalization();
                Engine.Unload();
            }
        }
    }
    

    Note that for my project I have no choice than loading it from memory.

    0
  • Avatar
    Natalia Karaseva

    Thanks to the code snippet! The reason is that you call

    document.Close();
    

    before you finish FRDocument processing. Remove it.

    Also, you don't need to call CreateFRDocument and AddImageDocument the second time. The ImageDocument has been already added into the FRDocument, so you just change the profile to TextExtraction_Accuracy and Process the doc again. Please, explore the code snippet below:

    // Create the only one FRDocument
    IFRDocument document = engine.CreateFRDocument();
    try {       
        //Create ImageDocument from file in memory 
        byte[] fileContent = Files.readAllBytes(Paths.get(imagePath));
        IImageDocument imageDocument = engine.OpenImageFileFromMemory(fileContent, null, null, null);
    
        // Add image file to the document for only one time.
        document.AddImageDocument(imageDocument);
    
        //Recognize document and save results to PDF
        document.Process(null);
        document.Export(imagePath + ".pdf", FileExportFormatEnum.FEF_PDF, null);
    
        // Change the profile
        engine.LoadPredefinedProfile("TextExtraction_Accuracy");
    
        //Recognize document with new settings and save results to TXT
        document.Process(null);
        document.Export(imagePath + ".txt", FileExportFormatEnum.FEF_TextUnicodeDefaults, null);
    
        // Close document
        document.Close();
    } finally {
        // Close document
        if(document != null) {
            document.Close();
        }
    }
    
    1
  • Avatar
    maol

    Ok, I did not know that I could process a document multiple times with different profiles. It solved my problem, thanks :)

    0

Please sign in to leave a comment.