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 ?

asked 31 Mar '15, 11:08

maol's gravatar image

maol
2911


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();
    }
}
link

answered 01 Apr '15, 20:21

Natalia%20Karaseva's gravatar image

Natalia Kara...
3214

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)?

link

answered 31 Mar '15, 17:37

Natalia%20Karaseva's gravatar image

Natalia Kara...
3214

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.

link

answered 31 Mar '15, 18:07

maol's gravatar image

maol
2911

edited 31 Mar '15, 18:10

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

link

answered 02 Apr '15, 14:58

maol's gravatar image

maol
2911

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×35
×7
×3
×1
×1

Asked: 31 Mar '15, 11:08

Seen: 1,709 times

Last updated: 02 Apr '15, 14:58

© 2016 ABBYY. All rights Reserved. www.ABBYY.com | Privacy Policy | Legal