x86 Uygulamalarda OcrInternals Dağıtım Hatası
IronTesseract.ReadScreenShot(), yalnızca Windows x64 sürecinde desteklenen IronOCR'un AdvancedScan hattı üzerinden çalışır. Onu bir x86 uygulamasından çağırmak, IronOcr.Extensions.AdvancedScan paketi yüklü olsa bile OcrInternals dağıtım hatası ile sonuçlanır.
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]
Hata, ReadScreenShot() çağrısında ortaya çıkar:
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);
}Dim ocr = New IronOcr.IronTesseract()
Using input = New IronOcr.OcrInput()
input.LoadImage("Step_1-5.jpg")
Dim result = ocr.ReadScreenShot(input)
Console.WriteLine(result.Text)
End UsingDim ocr = New IronOcr.IronTesseract()
Using input = New IronOcr.OcrInput()
input.LoadImage("Step_1-5.jpg")
Dim result = ocr.ReadScreenShot(input)
Console.WriteLine(result.Text)
End UsingAdvancedScan'ın ReadScreenShot() bağlı olduğu yerel bileşenler bir x86 sürecinin içinde desteklenmez. IronOcr.Extensions.AdvancedScan yüklenmesi gereklidir, ancak bu, host sürecinin bitliğini değiştirmez, bu yüzden çağrı hala x86 altında çalışamaz.
Çözüm
Seçenek 1: Doğrudan x64'a Hedefleyin
En temiz çözüm, projenin platform hedefini doğrudan x64'e değiştirmektir. Visual Studio'da:
- Projeye sağ tıklayın ve Properties seçin.
- Build sekmesini açın.
- Platform hedefini x64 olarak ayarlayın.
- 32-bit Tercih Etme seçeneğini kaldırın.
- Tekrar derleyin ve çalıştırın.
Ana süreç x64 olarak çalışırken, ReadScreenShot() desteklenen bir ortamda yürütülür.
Seçenek 2: Uygulamayı x86 Tutun ve x64 Bir Yardımcı Proses Çağırın
Ana uygulama x86 kalmalı olduğunda, yalnızca OCR işlemini küçük bir x64 yardımcı işleme taşıyın ve mevcut uygulamadan çağırın. Yapı şu şekilde görünür:
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 uygulaması dokunulmadan kalırken AdvancedScan desteklenen yerde çalışır.
x86 uygulamasından yardımı çağırma
Yardımcıyı ProcessStartInfo ile başlatın ve çıktısını okuyun:
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 NotInheritable Class OcrHelperClient
Private Sub New()
End Sub
Public Shared 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 = 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 ClassImports System
Imports System.Diagnostics
Imports System.IO
Public NotInheritable Class OcrHelperClient
Private Sub New()
End Sub
Public Shared 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 = 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 ClassStandardOutput ve StandardError 'i yönlendirerek, çağıran program tanınan metni yakalayabilir ve yardımcı programın çıkış kodundan herhangi bir hatayı yüzeye çıkarabilir.
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);Dim imagePath As String = "C:\Images\Step_1-5.jpg"
Dim text As String = OcrHelperClient.ReadScreenshotWithHelper(imagePath)
Console.WriteLine(text)Dim imagePath As String = "C:\Images\Step_1-5.jpg"
Dim text As String = OcrHelperClient.ReadScreenshotWithHelper(imagePath)
Console.WriteLine(text)x64 Yardımcısını Oluşturma
IronOcr ve IronOcr.Extensions.AdvancedScan 'ye referans veren bir x64 konsol uygulaması olarak yardımcıyı oluşturun. Birinci argümandan resim yolunu okur, OCR'yi çalıştırır ve sonucu stdout 'e yazar:
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 NamespaceImports 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Çeşitli çıkış kodları (1, 2, 99), çağıran uygulamanın eksik bir argümanı eksik bir dosyadan veya beklenmeyen bir istisnadan ayırt etmesine olanak tanır.
Üretim Kullanımı İçin Notlar
Örnek, basitlik için stdout kullanır. Üretim için, mimarinize uyan iletişim yöntemini seçin. Seçenekler şunları içerir:
- Standart çıkış ve standart hata.
- Geçici JSON dosyaları.
- Adlandırılmış borular.
- Yerel bir HTTP uç noktası.
- x64 OCR işlemini barındıran bir Windows Hizmeti.
Küçük veya arada çağrılar için: yardımı talep üzerine başlatmak genellikle iyidir. Yüksek hacimli iş yükleri için: her istek başına bir proses başlatmaktan daha verimli olma eğiliminde olan uzun süreli bir x64 yardımcı hizmeti genellikle daha iyidir.
Hata Ayıklama İpuçları
Yardımcı yaklaşım kötü davranış gösterdiğinde şu kontrolleri gerçekleştirin:
- Ana uygulamanın gerçekten x86 kalması gerektiğini ve ekran görüntüsü senaryosu için
Ocr.Read()'in yeterli olmadığını onaylayın. ReadScreenShot()'in bir x64 sürecinden doğrudan çalıştırıldığında başarılı olduğunu doğrulayın.- Yardımcı projeyi Platform hedefi: x64 ile oluşturun ve x86 uygulamasının asla
ReadScreenShot()'yi kendisi çağırmadığından emin olun. IronOcr.Extensions.AdvancedScan'i x64 yardımcı projeye kurun.- Yardımcı işlemin erişebileceği görüntü yolunun yardımcıya geçirildiğini kontrol edin.
- IronOCR lisans anahtarını kodda, uygulama yapılandırmasında veya
IRONOCR_LICENSE_KEYortam değişkeninde yapılandırın.
Yardımcıyı yayınlarken, sadece .exe değil, tüm yapı çıktısını kopyalayın. Çıkış klasörü, tüm referans verilen bileşenleri ve yapıyla üretilen doğal çalışma zamanı dosyalarını içermelidir, aksi takdirde yardımcı aynı dağıtım hatasıyla karşılaşır.
Curtis Chau, Bilgisayar Bilimleri alanında Lisans Derecesine (Carleton Üniversitesi) sahip ve Node.js, TypeScript, JavaScript ve React konularında uzmanlaşmış ön uç geliştirmeyle ilgileniyor. Sezgisel ve estetik açıdan hoş kullanıcı arayüzleri oluşturma tutkunu, Curtis modern çerçevelerle çalışmayı ve iyi yapılandırılmış, görsel olarak çekici kılavuzlar oluşturmayı seviyor.