using IronOcr;using System;using System.Linq;// Instantiate OCR enginevar ocr = new IronTesseract();using var inputScreenshot = new OcrInput();inputScreenshot.LoadImage("screenshotOCR.png");// Perform OCROcrPhotoResult result = ocr.ReadScreenShot(inputScreenshot);// Output screenshot informationConsole.WriteLine(result.Text);Console.WriteLine(result.TextRegions.First().Region.X);Console.WriteLine(result.TextRegions.Last().Region.Width);Console.WriteLine(result.Confidence);
using IronOcr;
using System;
using System.Linq;
// Instantiate OCR engine
var ocr = new IronTesseract();
using var inputScreenshot = new OcrInput();
inputScreenshot.LoadImage("screenshotOCR.png");
// Perform OCR
OcrPhotoResult result = ocr.ReadScreenShot(inputScreenshot);
// Output screenshot information
Console.WriteLine(result.Text);
Console.WriteLine(result.TextRegions.First().Region.X);
Console.WriteLine(result.TextRegions.Last().Region.Width);
Console.WriteLine(result.Confidence);
ImportsIronOcrImportsSystemImportsSystem.Linq' Instantiate OCR engineDim ocr = New IronTesseract()Using inputScreenshot = New OcrInput() inputScreenshot.LoadImage("screenshotOCR.png") ' Perform OCR Dim result AsOcrPhotoResult = ocr.ReadScreenShot(inputScreenshot) ' Output screenshot informationConsole.WriteLine(result.Text)Console.WriteLine(result.TextRegions.First().Region.X)Console.WriteLine(result.TextRegions.Last().Region.Width)Console.WriteLine(result.Confidence)EndUsing
Imports IronOcr
Imports System
Imports System.Linq
' Instantiate OCR engine
Dim ocr = New IronTesseract()
Using inputScreenshot = New OcrInput()
inputScreenshot.LoadImage("screenshotOCR.png")
' Perform OCR
Dim result As OcrPhotoResult = ocr.ReadScreenShot(inputScreenshot)
' Output screenshot information
Console.WriteLine(result.Text)
Console.WriteLine(result.TextRegions.First().Region.X)
Console.WriteLine(result.TextRegions.Last().Region.Width)
Console.WriteLine(result.Confidence)
End Using
複雑なシナリオの場合は、前処理を追加してスクリーンショットの読み取り処理を強化します:
using IronOcr;using System;// Configure OCR engine with specific settings for screenshotsvar ocr = new IronTesseract(){ // Set language for better accuracy with non-English contentLanguage = OcrLanguage.English, // Configure for screen-resolution imagesConfiguration = new TesseractConfiguration() {PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd, // Enable whitelist for specific characters if neededWhiteListCharacters = null }};using var inputScreenshot = new OcrInput();// Load screenshot with specific DPI setting for consistencyinputScreenshot.LoadImage("screenshotOCR.png");// Apply preprocessing for better accuracyinputScreenshot.DeNoise(); // Remove screenshot artifactsinputScreenshot.Sharpen(); // Enhance text edges// Perform OCR with error handlingtry{ OcrPhotoResult result = ocr.ReadScreenShot(inputScreenshot); // Process results with confidence threshold if (result.Confidence > 0.8) {Console.WriteLine($"High confidence text extraction: {result.Text}"); } else {Console.WriteLine("Low confidence - consider image preprocessing"); }}catch (Exception ex){Console.WriteLine($"OCR Error: {ex.Message}");}
using IronOcr;
using System;
// Configure OCR engine with specific settings for screenshots
var ocr = new IronTesseract()
{
// Set language for better accuracy with non-English content
Language = OcrLanguage.English,
// Configure for screen-resolution images
Configuration = new TesseractConfiguration()
{
PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd,
// Enable whitelist for specific characters if needed
WhiteListCharacters = null
}
};
using var inputScreenshot = new OcrInput();
// Load screenshot with specific DPI setting for consistency
inputScreenshot.LoadImage("screenshotOCR.png");
// Apply preprocessing for better accuracy
inputScreenshot.DeNoise(); // Remove screenshot artifacts
inputScreenshot.Sharpen(); // Enhance text edges
// Perform OCR with error handling
try
{
OcrPhotoResult result = ocr.ReadScreenShot(inputScreenshot);
// Process results with confidence threshold
if (result.Confidence > 0.8)
{
Console.WriteLine($"High confidence text extraction: {result.Text}");
}
else
{
Console.WriteLine("Low confidence - consider image preprocessing");
}
}
catch (Exception ex)
{
Console.WriteLine($"OCR Error: {ex.Message}");
}
using IronOcr;using System;using System.Linq;var ocr = new IronTesseract();using var input = new OcrInput();input.LoadImage("screenshot.png");OcrPhotoResult result = ocr.ReadScreenShot(input);// Process each text region individuallyforeach (var region in result.TextRegions){Console.WriteLine($"Text: {region.TextInRegion}");Console.WriteLine($"Location: X={region.Region.X}, Y={region.Region.Y}");Console.WriteLine($"Size: {region.Region.Width}x{region.Region.Height}");Console.WriteLine($"Confidence: {region.RegionConf:P2}");Console.WriteLine("---");}// Find specific UI elements by text contentvar buttonRegion = result.TextRegions .FirstOrDefault(r => r.TextInRegion.Contains("Submit", StringComparison.OrdinalIgnoreCase));if (buttonRegion != null){Console.WriteLine($"Found button at: {buttonRegion.Region.X}, {buttonRegion.Region.Y}");}
using IronOcr;
using System;
using System.Linq;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("screenshot.png");
OcrPhotoResult result = ocr.ReadScreenShot(input);
// Process each text region individually
foreach (var region in result.TextRegions)
{
Console.WriteLine($"Text: {region.TextInRegion}");
Console.WriteLine($"Location: X={region.Region.X}, Y={region.Region.Y}");
Console.WriteLine($"Size: {region.Region.Width}x{region.Region.Height}");
Console.WriteLine($"Confidence: {region.RegionConf:P2}");
Console.WriteLine("---");
}
// Find specific UI elements by text content
var buttonRegion = result.TextRegions
.FirstOrDefault(r => r.TextInRegion.Contains("Submit", StringComparison.OrdinalIgnoreCase));
if (buttonRegion != null)
{
Console.WriteLine($"Found button at: {buttonRegion.Region.X}, {buttonRegion.Region.Y}");
}
using IronOcr;// Configure for multiple languagesvar ocr = new IronTesseract();ocr.AddSecondaryLanguage(OcrLanguage.ChineseSimplified);ocr.AddSecondaryLanguage(OcrLanguage.Japanese);using var input = new OcrInput();input.LoadImage("multilingual-screenshot.png");// Process with language detectionOcrPhotoResult result = ocr.ReadScreenShot(input);Console.WriteLine($"Extracted multilingual text: {result.Text}");
using IronOcr;
// Configure for multiple languages
var ocr = new IronTesseract();
ocr.AddSecondaryLanguage(OcrLanguage.ChineseSimplified);
ocr.AddSecondaryLanguage(OcrLanguage.Japanese);
using var input = new OcrInput();
input.LoadImage("multilingual-screenshot.png");
// Process with language detection
OcrPhotoResult result = ocr.ReadScreenShot(input);
Console.WriteLine($"Extracted multilingual text: {result.Text}");
ImportsIronOcr' Configure for multiple languagesDim ocr As New IronTesseract()ocr.AddSecondaryLanguage(OcrLanguage.ChineseSimplified)ocr.AddSecondaryLanguage(OcrLanguage.Japanese)Using input As New OcrInput() input.LoadImage("multilingual-screenshot.png") ' Process with language detection Dim result AsOcrPhotoResult = ocr.ReadScreenShot(input)Console.WriteLine($"Extracted multilingual text: {result.Text}")EndUsing
Imports IronOcr
' Configure for multiple languages
Dim ocr As New IronTesseract()
ocr.AddSecondaryLanguage(OcrLanguage.ChineseSimplified)
ocr.AddSecondaryLanguage(OcrLanguage.Japanese)
Using input As New OcrInput()
input.LoadImage("multilingual-screenshot.png")
' Process with language detection
Dim result As OcrPhotoResult = ocr.ReadScreenShot(input)
Console.WriteLine($"Extracted multilingual text: {result.Text}")
End Using
バッチ処理のパフォーマンス最適化
複数のスクリーンショットを処理する場合は、以下の最適化戦略を実行してください:
using IronOcr;using System.Collections.Generic;using System.Threading.Tasks;public async TaskProcessScreenshotBatchAsync(List<string> screenshotPaths){ var ocr = new IronTesseract(); // Process screenshots in parallel for better performance var tasks = screenshotPaths.Select(async path => { using var input = new OcrInput(); input.LoadImage(path); // Apply consistent preprocessing input.DeNoise(); var result = awaitTask.Run(() => ocr.ReadScreenShot(input)); return new { Path = path, Result = result }; }); var results = awaitTask.WhenAll(tasks); // Process results foreach (var item in results) {Console.WriteLine($"File: {item.Path}");Console.WriteLine($"Text: {item.Result.Text}");Console.WriteLine($"Confidence: {item.Result.Confidence:P2}"); }}
using IronOcr;
using System.Collections.Generic;
using System.Threading.Tasks;
public async Task ProcessScreenshotBatchAsync(List<string> screenshotPaths)
{
var ocr = new IronTesseract();
// Process screenshots in parallel for better performance
var tasks = screenshotPaths.Select(async path =>
{
using var input = new OcrInput();
input.LoadImage(path);
// Apply consistent preprocessing
input.DeNoise();
var result = await Task.Run(() => ocr.ReadScreenShot(input));
return new { Path = path, Result = result };
});
var results = await Task.WhenAll(tasks);
// Process results
foreach (var item in results)
{
Console.WriteLine($"File: {item.Path}");
Console.WriteLine($"Text: {item.Result.Text}");
Console.WriteLine($"Confidence: {item.Result.Confidence:P2}");
}
}
ImportsIronOcrImportsSystem.Collections.GenericImportsSystem.Threading.TasksPublicAsync Function ProcessScreenshotBatchAsync(screenshotPaths AsList(OfString)) AsTask Dim ocr = New IronTesseract() ' Process screenshots in parallel for better performance Dim tasks = screenshotPaths.Select(Async Function(path)Using input = New OcrInput() input.LoadImage(path) ' Apply consistent preprocessing input.DeNoise() Dim result = AwaitTask.Run(Function() ocr.ReadScreenShot(input)) Return New With {Key .Path = path, Key .Result = result}EndUsing End Function) Dim results = AwaitTask.WhenAll(tasks) ' Process results For Each item In resultsConsole.WriteLine($"File: {item.Path}")Console.WriteLine($"Text: {item.Result.Text}")Console.WriteLine($"Confidence: {item.Result.Confidence:P2}") NextEnd Function
Imports IronOcr
Imports System.Collections.Generic
Imports System.Threading.Tasks
Public Async Function ProcessScreenshotBatchAsync(screenshotPaths As List(Of String)) As Task
Dim ocr = New IronTesseract()
' Process screenshots in parallel for better performance
Dim tasks = screenshotPaths.Select(Async Function(path)
Using input = New OcrInput()
input.LoadImage(path)
' Apply consistent preprocessing
input.DeNoise()
Dim result = Await Task.Run(Function() ocr.ReadScreenShot(input))
Return New With {Key .Path = path, Key .Result = result}
End Using
End Function)
Dim results = Await Task.WhenAll(tasks)
' Process results
For Each item In results
Console.WriteLine($"File: {item.Path}")
Console.WriteLine($"Text: {item.Result.Text}")
Console.WriteLine($"Confidence: {item.Result.Confidence:P2}")
Next
End Function
Is PNG the best format for screenshots when using IronOCR?
Yes, using PNG format is recommended for screenshots as it preserves text clarity best due to its lossless compression, leading to more accurate OCR results.
What are some common use cases for IronOCR's `ReadScreenshot` method?
Common use cases for the `ReadScreenshot` method include automated UI testing, digital asset management, customer support tools, documentation automation, and accessibility tools for screen readers.