Read Screenshot
This code example demonstrates how to use the IronTesseract OCR engine to extract text from a screenshot image.
// Import necessary namespaces
using System;
using System.Linq; // For using LINQ operations such as First() and Last()
using IronOcr; // For the IronTesseract library
class Program
{
static void Main()
{
// Create an instance of the IronTesseract OCR engine
var Ocr = new IronTesseract();
// Initialize an OcrInput object and load the image containing the screenshot
var input = new OcrInput();
input.AddImage("screenshotOCR.png");
// Process the screenshot using the OCR engine
// The Read method returns an OcrResult object containing the recognized text and related regions
OcrResult result = Ocr.Read(input);
// Print the extracted text from the screenshot
Console.WriteLine("Extracted Text:");
Console.WriteLine(result.Text);
// Print the X coordinate of the first recognized text region
Console.WriteLine("\nX Coordinate of First Recognized Text Region:");
Console.WriteLine(result.TextRegions.First().Location.X);
// Print the width of the last recognized text region
Console.WriteLine("\nWidth of Last Recognized Text Region:");
Console.WriteLine(result.TextRegions.Last().Location.Width);
// Print the OCR confidence score
Console.WriteLine("\nOCR Confidence Score:");
Console.WriteLine(result.Confidence);
}
}
// Import necessary namespaces
using System;
using System.Linq; // For using LINQ operations such as First() and Last()
using IronOcr; // For the IronTesseract library
class Program
{
static void Main()
{
// Create an instance of the IronTesseract OCR engine
var Ocr = new IronTesseract();
// Initialize an OcrInput object and load the image containing the screenshot
var input = new OcrInput();
input.AddImage("screenshotOCR.png");
// Process the screenshot using the OCR engine
// The Read method returns an OcrResult object containing the recognized text and related regions
OcrResult result = Ocr.Read(input);
// Print the extracted text from the screenshot
Console.WriteLine("Extracted Text:");
Console.WriteLine(result.Text);
// Print the X coordinate of the first recognized text region
Console.WriteLine("\nX Coordinate of First Recognized Text Region:");
Console.WriteLine(result.TextRegions.First().Location.X);
// Print the width of the last recognized text region
Console.WriteLine("\nWidth of Last Recognized Text Region:");
Console.WriteLine(result.TextRegions.Last().Location.Width);
// Print the OCR confidence score
Console.WriteLine("\nOCR Confidence Score:");
Console.WriteLine(result.Confidence);
}
}
' Import necessary namespaces
Imports Microsoft.VisualBasic
Imports System
Imports System.Linq ' For using LINQ operations such as First() and Last()
Imports IronOcr ' For the IronTesseract library
Friend Class Program
Shared Sub Main()
' Create an instance of the IronTesseract OCR engine
Dim Ocr = New IronTesseract()
' Initialize an OcrInput object and load the image containing the screenshot
Dim input = New OcrInput()
input.AddImage("screenshotOCR.png")
' Process the screenshot using the OCR engine
' The Read method returns an OcrResult object containing the recognized text and related regions
Dim result As OcrResult = Ocr.Read(input)
' Print the extracted text from the screenshot
Console.WriteLine("Extracted Text:")
Console.WriteLine(result.Text)
' Print the X coordinate of the first recognized text region
Console.WriteLine(vbLf & "X Coordinate of First Recognized Text Region:")
Console.WriteLine(result.TextRegions.First().Location.X)
' Print the width of the last recognized text region
Console.WriteLine(vbLf & "Width of Last Recognized Text Region:")
Console.WriteLine(result.TextRegions.Last().Location.Width)
' Print the OCR confidence score
Console.WriteLine(vbLf & "OCR Confidence Score:")
Console.WriteLine(result.Confidence)
End Sub
End Class
Explanation:
Namespace Imports: The code imports necessary namespaces, including
System.Linq
for LINQ operations andIronOcr
for OCR functionalities.Creating OCR Engine: An instance of the
IronTesseract
class is created to use the OCR engine's capabilities for text recognition.Loading the Image: An
OcrInput
object is initialized to load the image (screenshotOCR.png
) from which text needs to be extracted.Reading the Image:
- The
Read
method of theIronTesseract
instance processes the image and returns anOcrResult
containing the recognized text. - It also contains details about the regions where text was identified.
- The
- Outputs:
- Extracted text is printed using
result.Text
. - The X coordinate of the region of the first recognized text is printed.
- The width of the region of the last recognized text is printed.
- The OCR confidence score, indicating the accuracy of the text recognition, is printed.
- Extracted text is printed using
This method allows for the extraction of text and region-specific details from screenshots, which is particularly useful for capturing text in graphical content or user interfaces.