Extracting text inside a rectangle
Contents
[
Hide
]
Aspose.OCR allows you to extract names, dates, numbers, and other blocks from certain areas of uniform images, such as ID cards, visas, driver’s licenses, applications, and so on. Regions can be provided manually or automatically detected using the DetectRectangles
method.
To extract text from one or more areas of an image, provide a list of Rectangle
objects specifying the upper left corner and the width and height of the image area to setRecognitionAreas
method of recognition settings.
AsposeOCR api = new AsposeOCR();
// Define image regions
ArrayList<Rectangle> regions = new ArrayList<Rectangle>();
regions.add(new Rectangle(231,101,430,42));
regions.add(new Rectangle(546,224,123,26));
// Specify recognition settings
RecognitionSettings recognitionSettings = new RecognitionSettings();
recognitionSettings.setRecognitionAreas(regions);
// Recognize image
OcrInput input = new OcrInput(InputType.SingleImage);
input.Add("source.png");
List<RecognitionResult> results = api.Recognize(input, recognitionSettings);
// Extract text from selected regions
System.out.println("Name: " + result.recognitionAreasText.get(0));
System.out.println("Expiry: " + result.recognitionAreasText.get(1));
The text from each rectangle is returned in the recognitionAreasText
property of the recognition results.
Live demo
Block | Coordinates | Extracted text |
---|---|---|
Name | {X=231, Y=101, Width=430, Height=42} | SAMPLE AVERY JOSEPH |
Expiry date | {X=546, Y=224, Width=123, Height=26} | 08/15/2022 |