|
| 1 | +// |
| 2 | +// Copyright (c) 2017-2025, Datalogics, Inc. All rights reserved. |
| 3 | +// |
| 4 | +// |
| 5 | +// The OCRImage sample demonstrates how the Library works to OCR an image. |
| 6 | +// |
| 7 | +// Command-line: <output-file> (Optional) |
| 8 | +// |
| 9 | + |
| 10 | +#include <iostream> |
| 11 | + |
| 12 | +#include "ASExtraCalls.h" |
| 13 | +#include "DLExtrasCalls.h" |
| 14 | +#include "PSFCalls.h" |
| 15 | +#include "PERCalls.h" |
| 16 | +#include "PEWCalls.h" |
| 17 | +#include "PagePDECntCalls.h" |
| 18 | +#include "PagePDECntCalls.h" |
| 19 | +#include "OCREngineCalls.h" |
| 20 | + |
| 21 | +#include "InitializeLibrary.h" |
| 22 | +#include "APDFLDoc.h" |
| 23 | + |
| 24 | +#define DIR_LOC "../../../../Resources/Sample_Input/" |
| 25 | +#define DEF_INPUT "OCRImage.png" |
| 26 | +#define DEF_OUTPUT "OCRImage-out.pdf" |
| 27 | + |
| 28 | +int main(int argc, char **argv) { |
| 29 | + APDFLib libInit; |
| 30 | + ASErrorCode errCode = 0; |
| 31 | + if (libInit.isValid() == false) { |
| 32 | + errCode = libInit.getInitError(); |
| 33 | + std::cout << "Initialization failed with code " << errCode << std::endl; |
| 34 | + return libInit.getInitError(); |
| 35 | + } |
| 36 | + |
| 37 | + std::string csInputFileName(argc > 1 ? argv[1] : DIR_LOC DEF_INPUT); |
| 38 | + std::string csOutputFileName(argc > 2 ? argv[2] : DEF_OUTPUT); |
| 39 | + std::cout << "Recognizing text in " << csInputFileName.c_str() << std::endl; |
| 40 | + |
| 41 | + ASPathName sInput = APDFLDoc::makePath(csInputFileName.c_str()); |
| 42 | + ASPathName sOutput = APDFLDoc::makePath(csOutputFileName.c_str()); |
| 43 | + |
| 44 | + DURING |
| 45 | + // Sets the correct location for the OCREngine function table. |
| 46 | + gOCREngineHFT = InitOCREngineHFT; |
| 47 | + |
| 48 | + // Initialize the OCREngine plugin. |
| 49 | + if (!OCREngineInitialize()) { |
| 50 | + std::cout << "The OCREngine plugin failed to initialize." << std::endl; |
| 51 | + errCode = -1; |
| 52 | + } |
| 53 | + |
| 54 | + if (0 == errCode) { |
| 55 | + // Create a PDEImage object to perform OCR on. |
| 56 | + PDEImage image = DLCreatePDEImageFromFile(sInput, nullptr); |
| 57 | + |
| 58 | + // Set default OCR parameters. |
| 59 | + OCRParamsRec ocrParams = PDOCRDefaultParams(); |
| 60 | + |
| 61 | + // Set languages to configure OCREngine with. |
| 62 | + OCRLanguage newLanguages[] = {OCRLanguage_English, OCRLanguage_French, OCRLanguage_ChineseTraditional, |
| 63 | + OCRLanguage_ChineseSimplified, OCRLanguage_Japanese}; |
| 64 | + |
| 65 | + ASInt32 numLanguages = sizeof(newLanguages) / sizeof(newLanguages[0]); |
| 66 | + PDOCRParamsSetLanguagesConfigured(&ocrParams, newLanguages, numLanguages); |
| 67 | + |
| 68 | + // Create the destination document for the created form. |
| 69 | + PDDoc doc = PDDocCreate(); |
| 70 | + |
| 71 | + ASFixedRect mediaBox = {}; |
| 72 | + mediaBox.left = fixedZero; |
| 73 | + mediaBox.right = FloatToASFixed(72.0 * 8.5); |
| 74 | + mediaBox.bottom = fixedZero; |
| 75 | + mediaBox.top = FloatToASFixed(72.0 * 11.0); |
| 76 | + |
| 77 | + PDPage page = PDDocCreatePage(doc, kPDEBeforeFirst, mediaBox); |
| 78 | + |
| 79 | + // Run OCR on the image to get Form element containing the image with text underneath. |
| 80 | + PDEForm form = PDOCRCreateForm(&ocrParams, doc, image, 300, OCRMissingFontStrategy_Raise); |
| 81 | + |
| 82 | + // Put that form into the page in the destination document. |
| 83 | + PDEContent content = PDPageAcquirePDEContent(page, 0); |
| 84 | + PDEContentAddElem(content, PDEContentGetNumElems(content) - 1, (PDEElement)form); |
| 85 | + PDPageSetPDEContent(page, 0); |
| 86 | + |
| 87 | + // Save the output. |
| 88 | + PDDocSave(doc, PDSaveFull | PDSaveLinearized, sOutput, NULL, NULL, NULL); |
| 89 | + |
| 90 | + // Release resources. |
| 91 | + PDPageReleasePDEContent(page, 0); |
| 92 | + PDPageRelease(page); |
| 93 | + PDDocClose(doc); |
| 94 | + |
| 95 | + PDERelease((PDEObject)form); |
| 96 | + PDERelease(reinterpret_cast<PDEObject>(image)); |
| 97 | + ASFileSysReleasePath(NULL, sInput); |
| 98 | + ASFileSysReleasePath(NULL, sOutput); |
| 99 | + |
| 100 | + // Release OCREngine resources and terminate the plugin. |
| 101 | + PDOCRReleaseParams(&ocrParams); |
| 102 | + OCREngineTerminate(); |
| 103 | + } // if 0 == errCode |
| 104 | + HANDLER |
| 105 | + errCode = ERRORCODE; |
| 106 | + libInit.displayError(errCode); |
| 107 | + END_HANDLER |
| 108 | + |
| 109 | + return errCode; // APDFLib's destructor terminates the library. |
| 110 | +} |
0 commit comments