x86 애플리케이션에서 OcrInternals 배포 오류
IronTesseract.ReadScreenShot() 은 Windows x64 프로세스에서만 지원되는 IronOCR의 AdvancedScan 파이프라인을 실행합니다. x86 애플리케이션에서 호출할 경우 OcrInternals 배포 오류로 실패합니다, 심지어 IronOcr.Extensions.AdvancedScan 패키지가 설치되어 있어도 마찬가지입니다.
Error while reading a screenshot, Error while deploying OcrInternals for IronOcr:
'Unable to locate 'OcrInternals' in
...\bin\Debug\runtimes\win-x86\native,
...\bin\Debug\runtimes\win.6.2-x86\native,
...\bin\Debug\runtimes\win.6-x86\native,
...\bin\Debug\,
...
nor in an embedded resource.'
Please install the NuGet Package 'IronOcr.Extension.AdvancedScan' when using IronOcr on Windows.
[Issue Code IRONOCR-OCRINTERNALS-DEPLOYMENT-ERROR-WIN]
실패는 ReadScreenShot() 호출 자체에서 발생합니다:
var ocr = new IronOcr.IronTesseract();
using (var input = new IronOcr.OcrInput())
{
input.LoadImage("Step_1-5.jpg");
var result = ocr.ReadScreenShot(input);
Console.WriteLine(result.Text);
}
var ocr = new IronOcr.IronTesseract();
using (var input = new IronOcr.OcrInput())
{
input.LoadImage("Step_1-5.jpg");
var result = ocr.ReadScreenShot(input);
Console.WriteLine(result.Text);
}
Imports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
input.LoadImage("Step_1-5.jpg")
Dim result = ocr.ReadScreenShot(input)
Console.WriteLine(result.Text)
End Using
AdvancedScan에 의존하는 네이티브 구성 요소는 x86 프로세스 내에서 지원되지 않습니다. IronOcr.Extensions.AdvancedScan 설치가 필요하지만, 이는 호스트 프로세스의 비트 수를 변경하지 않기 때문에 x86에서는 여전히 실행할 수 없습니다.
해결책
옵션 1: 직접 x64 대상으로 설정
가장 깔끔한 수정은 프로젝트의 플랫폼 대상을 x64로 전환하는 것입니다. Visual Studio에서:
- 프로젝트를 마우스 오른쪽 버튼으로 클릭하고 속성을 선택하십시오.
- 빌드 탭을 엽니다.
- 플랫폼 대상을 x64로 설정하십시오.
- 32비트 선호을 선택 해제하십시오.
- 재빌드하고 실행하십시오.
호스트 프로세스를 x64로 실행하면 ReadScreenShot() 는 지원되는 환경에서 실행됩니다.
옵션 2: 앱을 x86으로 유지하고 x64 헬퍼 프로세스 호출
주 애플리케이션이 x86이어야 할 때, OCR 작업만 작은 x64 헬퍼 프로세스로 이동하고 기존 앱에서 호출합니다. 구조는 다음과 같습니다:
MainWinForms.x86
- .NET Framework Windows Forms app
- Platform target: x86
- Does not run ReadScreenShot() directly
- Calls the x64 helper process
OcrHelper.x64
- .NET Framework Console app
- Platform target: x64
- References IronOCR
- References IronOcr.Extensions.AdvancedScan
- Runs Ocr.ReadScreenShot()
- Returns the OCR result to the main app
x86 애플리케이션은 영향을 받지 않으며 AdvancedScan이 지원되는 곳에서 실행됩니다.
x86 애플리케이션에서 헬퍼 호출
ProcessStartInfo 으로 헬퍼를 실행하고 출력을 읽습니다:
using System;
using System.Diagnostics;
using System.IO;
public static class OcrHelperClient
{
public static string ReadScreenshotWithHelper(string imagePath)
{
string helperExePath = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"OcrHelper.x64",
"OcrHelper.x64.exe"
);
if (!File.Exists(helperExePath))
{
throw new FileNotFoundException("The OCR helper executable was not found.", helperExePath);
}
var startInfo = new ProcessStartInfo
{
FileName = helperExePath,
Arguments = "\"" + imagePath + "\"",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
using (var process = new Process())
{
process.StartInfo = startInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
if (process.ExitCode != 0)
{
throw new Exception("OCR helper failed: " + error);
}
return output;
}
}
}
using System;
using System.Diagnostics;
using System.IO;
public static class OcrHelperClient
{
public static string ReadScreenshotWithHelper(string imagePath)
{
string helperExePath = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"OcrHelper.x64",
"OcrHelper.x64.exe"
);
if (!File.Exists(helperExePath))
{
throw new FileNotFoundException("The OCR helper executable was not found.", helperExePath);
}
var startInfo = new ProcessStartInfo
{
FileName = helperExePath,
Arguments = "\"" + imagePath + "\"",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
using (var process = new Process())
{
process.StartInfo = startInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
if (process.ExitCode != 0)
{
throw new Exception("OCR helper failed: " + error);
}
return output;
}
}
}
Imports System
Imports System.Diagnostics
Imports System.IO
Public Module OcrHelperClient
Public Function ReadScreenshotWithHelper(imagePath As String) As String
Dim helperExePath As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "OcrHelper.x64", "OcrHelper.x64.exe")
If Not File.Exists(helperExePath) Then
Throw New FileNotFoundException("The OCR helper executable was not found.", helperExePath)
End If
Dim startInfo As New ProcessStartInfo With {
.FileName = helperExePath,
.Arguments = """" & imagePath & """",
.UseShellExecute = False,
.RedirectStandardOutput = True,
.RedirectStandardError = True,
.CreateNoWindow = True
}
Using process As New Process()
process.StartInfo = startInfo
process.Start()
Dim output As String = process.StandardOutput.ReadToEnd()
Dim error As String = process.StandardError.ReadToEnd()
process.WaitForExit()
If process.ExitCode <> 0 Then
Throw New Exception("OCR helper failed: " & error)
End If
Return output
End Using
End Function
End Module
StandardOutput 와 StandardError 를 모두 리디렉션하면 호출자가 인식된 텍스트를 캡처하고 헬퍼의 종료 코드에서 발생한 실패를 표출할 수 있습니다.
string imagePath = @"C:\Images\Step_1-5.jpg";
string text = OcrHelperClient.ReadScreenshotWithHelper(imagePath);
Console.WriteLine(text);
string imagePath = @"C:\Images\Step_1-5.jpg";
string text = OcrHelperClient.ReadScreenshotWithHelper(imagePath);
Console.WriteLine(text);
Imports System
Dim imagePath As String = "C:\Images\Step_1-5.jpg"
Dim text As String = OcrHelperClient.ReadScreenshotWithHelper(imagePath)
Console.WriteLine(text)
x64 헬퍼 빌드
x64 콘솔 앱으로 헬퍼를 만들어 IronOcr 와 IronOcr.Extensions.AdvancedScan 를 참조합니다. 첫 번째 인자로부터 이미지 경로를 읽고, OCR을 실행한 후 결과를 stdout 에 씁니다:
using System;
using System.IO;
using IronOcr;
namespace OcrHelper.x64
{
internal static class Program
{
private static int Main(string[] args)
{
try
{
if (args.Length == 0)
{
Console.Error.WriteLine("Missing image path argument.");
return 1;
}
string imagePath = args[0];
if (!File.Exists(imagePath))
{
Console.Error.WriteLine("Image file was not found: " + imagePath);
return 2;
}
string licenseKey = Environment.GetEnvironmentVariable("IRONOCR_LICENSE_KEY");
if (!string.IsNullOrWhiteSpace(licenseKey))
{
License.LicenseKey = licenseKey;
}
var ocr = new IronTesseract();
using (var input = new OcrInput())
{
input.LoadImage(imagePath);
var result = ocr.ReadScreenShot(input);
Console.WriteLine(result.Text);
}
return 0;
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.ToString());
return 99;
}
}
}
}
using System;
using System.IO;
using IronOcr;
namespace OcrHelper.x64
{
internal static class Program
{
private static int Main(string[] args)
{
try
{
if (args.Length == 0)
{
Console.Error.WriteLine("Missing image path argument.");
return 1;
}
string imagePath = args[0];
if (!File.Exists(imagePath))
{
Console.Error.WriteLine("Image file was not found: " + imagePath);
return 2;
}
string licenseKey = Environment.GetEnvironmentVariable("IRONOCR_LICENSE_KEY");
if (!string.IsNullOrWhiteSpace(licenseKey))
{
License.LicenseKey = licenseKey;
}
var ocr = new IronTesseract();
using (var input = new OcrInput())
{
input.LoadImage(imagePath);
var result = ocr.ReadScreenShot(input);
Console.WriteLine(result.Text);
}
return 0;
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.ToString());
return 99;
}
}
}
}
Imports System
Imports System.IO
Imports IronOcr
Namespace OcrHelper.x64
Friend Module Program
Private Function Main(args As String()) As Integer
Try
If args.Length = 0 Then
Console.Error.WriteLine("Missing image path argument.")
Return 1
End If
Dim imagePath As String = args(0)
If Not File.Exists(imagePath) Then
Console.Error.WriteLine("Image file was not found: " & imagePath)
Return 2
End If
Dim licenseKey As String = Environment.GetEnvironmentVariable("IRONOCR_LICENSE_KEY")
If Not String.IsNullOrWhiteSpace(licenseKey) Then
License.LicenseKey = licenseKey
End If
Dim ocr As New IronTesseract()
Using input As New OcrInput()
input.LoadImage(imagePath)
Dim result = ocr.ReadScreenShot(input)
Console.WriteLine(result.Text)
End Using
Return 0
Catch ex As Exception
Console.Error.WriteLine(ex.ToString())
Return 99
End Try
End Function
End Module
End Namespace
서로 다른 종료 코드 (1, 2, 99)는 호출 애플리케이션이 누락된 인수와 누락된 파일 또는 예상치 못한 예외를 구별하는 데 도움을 줍니다.
생산 사용에 대한 참고
샘플은 간단하게 stdout 를 사용합니다. 생산을 위해서는 여러분의 아키텍처에 맞는 통신 방법을 선택하세요. 옵션에는 다음이 포함됩니다:
- 표준 출력 및 표준 오류.
- 임시 JSON 파일.
- 명명된 파이프.
- 로컬 HTTP 엔드포인트.
- x64 OCR 작업을 호스팅하는 Windows 서비스.
작거나 일시적인 호출: 수요에 따라 헬퍼를 시작하는 것이 일반적으로 괜찮습니다. 대량 작업량: 요청당 프로세스를 스폰하는 것보다 장기 실행되는 x64 헬퍼 서비스가 더 효율적입니다.
디버그 팁
헬퍼 접근 방식이 오작동할 때, 다음 검사를 수행하세요:
- 메인 애플리케이션이 진정으로 x86 상태를 유지해야 하는지, 그리고 스크린샷 시나리오에
Ocr.Read()가 충분하지 않은지 확인합니다. - x64 프로세스에서 직접 실행할 때
ReadScreenShot()가 성공하는지 확인합니다. - 헬퍼 프로젝트를 플랫폼 대상: x64 로 빌드하고 , x86 애플리케이션이 절대
ReadScreenShot()자체를 호출하지 않도록 합니다. - x64 헬퍼 프로젝트에
IronOcr.Extensions.AdvancedScan를 설치합니다. 헬퍼에 전달되는 이미지 경로가 헬퍼 프로세스에서 도달 가능한지 확인하십시오. - 코드, 애플리케이션 설정, 또는
IRONOCR_LICENSE_KEY환경 변수에서 IronOCR 라이선스 키를 구성합니다.
헬퍼를 게시할 때 전체 빌드 출력을 복사하고 .exe 만 복사하지 마십시오. 출력 폴더에는 모든 참조된 어셈블리와 빌드에서 생성된 네이티브 런타임 파일이 포함되어야 하며 그렇지 않으면 헬퍼가 동일한 배포 오류에 직면합니다.

