GroupDocs.Parser for Java doesn’t include a built-in OCR engine. Text recognition from images and scanned PDFs is performed by an OCR connector: a class that extends OcrConnectorBase and wraps any paid or free OCR solution. To use it, pass the connector when creating the Parser class:
Instantiate ParserSettings object with the instance of class that implements OCR functionality;
The following example shows how to create an instance of Parser class with the custom connector:
// Create an instance of ParserSettings class with the custom OCR connector
ParserSettingssettings=newParserSettings(newYourCustomOcrConnector());// Create an instance of Parser class with the parser settings
try(Parserparser=newParser(filePath,settings)){// Extract data from the document
}
OcrConnectorBase members
OcrConnectorBase class has the following members. All of them have default implementations, so override only the members you need:
Extracts a text from the provided image stream. It is used when getText method from Parser class performs OCR - for image files and for documents with useOcr = true.
Deprecated. Extracts text areas from the image of the document page. In version 26.10 it is still used when getTextAreas method is called with useOcr = true for PDF and other documents.
Parser separates text recognition into two independent workflows: plain text and text areas. The first one is used when the text is extracted from a document, for example, by getText method. The second one is used by getTextAreas method. When creating a custom connector, implement only the workflow you need and return false from the isTextSupported or isTextAreasSupported method for the other one. If isTextSupported returns false, text extraction from images isn’t supported and getText returns null for them; if isTextAreasSupported returns false, getTextAreas returns null when OCR is required.
Warning
To support text areas both for image files and for PDF documents, implement both recognizeTextAreas overloads (one of them can delegate to the other).
The OCR settings are represented by the OcrOptions class and may include:
OcrEventHandler (getHandler) for sending warnings that occur during the text recognition;
A rectangular area of the image (getRectangle) from which text needs to be extracted; in this case, there’s no need to extract text from the entire image;
A flag for using spell checking (getUseSpellChecker), the recognition language (getLanguage) and page preview options (getPagePreviewOptions) - see OCR Usage Basics for when these values reach the connector.
An image from which the text must be extracted (the image file itself or the image of the document page).
options
Is used to define a rectangular area which restricts the area of the image and OcrEventHandler object to handle warnings while the text recognition.
The method returns the recognized text; null if text recognition isn’t supported.
The following example shows how to implement text recognition by using Aspose.OCR on-premise API. The recognition is implemented in the deprecated overload with the page index (as in the GitHub examples), and the overload which is called by the parser delegates to it:
@OverridepublicStringrecognizeText(java.io.InputStreamimageStream,OcrOptionsoptions){// This overload is called by the parser; the page index isn't passed to it
returnrecognizeText(imageStream,0,options);}@OverridepublicStringrecognizeText(java.io.InputStreamimageStream,intpageIndex,OcrOptionsoptions){try{// Create an instance of Aspose OCR API
com.aspose.ocr.AsposeOCRapi=newcom.aspose.ocr.AsposeOCR();// Convert the image stream into the memory stream
java.awt.image.BufferedImageimage=ImageIO.read(imageStream);// Create an instance of RecognitionSettings
com.aspose.ocr.RecognitionSettingssettings=newcom.aspose.ocr.RecognitionSettings();// Check if the rectangle is set
if(options!=null&&options.getRectangle()!=null){ArrayList<java.awt.Rectangle>areas=newArrayList<>();areas.add(newjava.awt.Rectangle((int)options.getRectangle().getLeft(),(int)options.getRectangle().getTop(),(int)options.getRectangle().getSize().getWidth(),(int)options.getRectangle().getSize().getHeight()));// Set recognition areas
settings.setRecognitionAreas(areas);}// Perform the text recognition
com.aspose.ocr.RecognitionResultresult=api.RecognizePage(image,settings);// Check if the handler is set
if(options!=null&&options.getHandler()!=null){// Send all recognition warnings
options.getHandler().onWarnings(pageIndex,result.warnings);}// Return a recognized text
returnresult.recognitionText;}catch(java.lang.Exceptionex){returnnull;}}
A zero-based index of the page in the document (in the case when the image represents the document page).
pageSize
A size of the image (in the case when the image represents the document page - the size of the rendered page image).
options
Is used to define a rectangular area which restricts the area of the image and OcrEventHandler object to handle warnings while the text recognition.
Both methods return a collection of PageTextArea objects (null if text areas recognition isn’t supported). Each object contains the page (Page created from the page index and pageSize), the text and the rectangular area where the text was found. Return the rectangles in the coordinates of the passed image: for PDF and other documents the parser converts them to page coordinates itself.
The following example shows how to implement text areas recognition by using Aspose.OCR on-premise API:
@Overridepublicjava.lang.Iterable<PageTextArea>recognizeTextAreas(java.io.InputStreamimageStream,SizepageSize,OcrOptionsoptions){// This overload is called by the parser for image files
returnrecognizeTextAreas(imageStream,0,pageSize,options);}@Overridepublicjava.lang.Iterable<PageTextArea>recognizeTextAreas(java.io.InputStreamimageStream,intpageIndex,SizepageSize,OcrOptionsoptions){try{// Create an instance of Aspose OCR API
com.aspose.ocr.AsposeOCRapi=newcom.aspose.ocr.AsposeOCR();// Convert the image stream into the memory stream
java.awt.image.BufferedImageimage=ImageIO.read(imageStream);// Create an instance of RecognitionSettings
com.aspose.ocr.RecognitionSettingssettings=newcom.aspose.ocr.RecognitionSettings();settings.setDetectAreas(true);// Check if the rectangle is set
if(options!=null&&options.getRectangle()!=null){ArrayList<java.awt.Rectangle>areas=newArrayList<>();areas.add(newjava.awt.Rectangle((int)options.getRectangle().getLeft(),(int)options.getRectangle().getTop(),(int)options.getRectangle().getSize().getWidth(),(int)options.getRectangle().getSize().getHeight()));// Set recognition areas
settings.setRecognitionAreas(areas);}// Perform the text recognition
com.aspose.ocr.RecognitionResultresult=api.RecognizePage(image,settings);// Check if the handler is set
if(options!=null&&options.getHandler()!=null){// Send all recognition warnings
options.getHandler().onWarnings(pageIndex,result.warnings);}// Create a page object. The pageIndex parameter represents the page index of the document; for images it's always zero.
Pagepage=newPage(pageIndex,pageSize);// Combine rectangle and text collections to produce PageTextArea collection
ArrayList<PageTextArea>areas=newArrayList<>();for(inti=0;i<result.recognitionAreasRectangles.size();i++){java.awt.Rectanglerect=result.recognitionAreasRectangles.get(i);Stringtext=result.recognitionText;areas.add(newPageTextArea(text,page,newRectangle(newPoint(rect.getX(),rect.getY()),newSize(rect.getWidth(),rect.getHeight()))));}returnareas;}catch(java.lang.Exceptionex){returnnull;}}
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples:
Along with full featured Java library we provide simple, but powerful free Apps.
You are welcome to parse documents and extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Parser App.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
On this page
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.