名片包含公司或個人的商務資訊。名片上可能會有姓名、公司名稱、電話號碼、傳真、電子郵件、網站位址等資訊。您可能需要從紙本名片擷取這些資訊,並將其儲存為電子格式,例如行動電話的電子通訊錄、電子郵件客戶端,或任何其他資料儲存系統。例如,名片資訊通常會以 vCard 格式透過電子郵件或網路傳送。
您在此情境中需要執行的主要步驟如下:
- 取得名片的數位副本
您可以掃描名片,或拍攝名片照片。使用行動裝置數位相機拍攝的照片,解析度和品質可能較低。因此,可能需要先對影像進行額外處理。
- 識別名片
掃描的頁面每頁可能包含多張名片。識別必須具備高品質,且所有資訊都必須準確擷取。
- 以合適的格式儲存識別後的資料
您可以將識別後的資料儲存到不同的資料儲存系統,或將其匯出為 vCard 格式,再透過電子郵件傳送。
本主題中提供的程式碼範例僅適用於 Windows。
以下將詳細說明在此情境中使用 ABBYY FineReader Engine 的建議方式。
步驟 1. 載入 ABBYY FineReader Engine
若要開始使用 ABBYY FineReader Engine,您需要建立 Engine 物件。Engine 物件是 ABBYY FineReader Engine 物件階層中的最上層物件,提供各種全域設定、部分處理方法,以及用來建立其他物件的方法。若要建立 Engine 物件,您可以使用 InitializeEngine 函式。另請參閱 載入 Engine 物件的其他方式 (Win) 。public class EngineLoader : IDisposable
{
public EngineLoader()
{
// 使用 FREngine.dll 的完整路徑、您的 Customer Project ID,
// 以及(如適用)線上授權權杖檔案的路徑和線上授權密碼來初始化這些變數
string enginePath = "";
string customerProjectId = "";
string licensePath = "";
string licensePassword = "";
// 載入 FREngine.dll 程式庫
dllHandle = LoadLibraryEx(enginePath, IntPtr.Zero, LOAD_WITH_ALTERED_SEARCH_PATH);
try
{
if (dllHandle == IntPtr.Zero)
{
throw new Exception("Can't load " + enginePath);
}
IntPtr initializeEnginePtr = GetProcAddress(dllHandle, "InitializeEngine");
if (initializeEnginePtr == IntPtr.Zero)
{
throw new Exception("Can't find InitializeEngine function");
}
IntPtr deinitializeEnginePtr = GetProcAddress(dllHandle, "DeinitializeEngine");
if (deinitializeEnginePtr == IntPtr.Zero)
{
throw new Exception("Can't find DeinitializeEngine function");
}
IntPtr dllCanUnloadNowPtr = GetProcAddress(dllHandle, "DllCanUnloadNow");
if (dllCanUnloadNowPtr == IntPtr.Zero)
{
throw new Exception("Can't find DllCanUnloadNow function");
}
// 將指標轉換為委派
initializeEngine = (InitializeEngine)Marshal.GetDelegateForFunctionPointer(
initializeEnginePtr, typeof(InitializeEngine));
deinitializeEngine = (DeinitializeEngine)Marshal.GetDelegateForFunctionPointer(
deinitializeEnginePtr, typeof(DeinitializeEngine));
dllCanUnloadNow = (DllCanUnloadNow)Marshal.GetDelegateForFunctionPointer(
dllCanUnloadNowPtr, typeof(DllCanUnloadNow));
// 呼叫 InitializeEngine 函式
// 傳入線上授權檔案路徑和線上授權密碼
int hresult = initializeEngine(customerProjectId, licensePath, licensePassword,
"", "", false, ref engine);
Marshal.ThrowExceptionForHR(hresult);
}
catch (Exception)
{
// 釋放 FREngine.dll 程式庫
engine = null;
// 在呼叫 FreeLibrary 前刪除所有物件
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
FreeLibrary(dllHandle);
dllHandle = IntPtr.Zero;
initializeEngine = null;
deinitializeEngine = null;
dllCanUnloadNow = null;
throw;
}
}
// Kernel32.dll 函式
[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibraryEx(string dllToLoad, IntPtr reserved, uint flags);
private const uint LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008;
[DllImport("kernel32.dll")]
private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[DllImport("kernel32.dll")]
private static extern bool FreeLibrary(IntPtr hModule);
// FREngine.dll 函式
[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]
private delegate int InitializeEngine(string customerProjectId, string licensePath,
string licensePassword, string tempFolder, string dataFolder, bool isSharedCPUCoresMode,
ref FREngine.IEngine engine);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate int DeinitializeEngine();
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate int DllCanUnloadNow();
// 私有變數
private FREngine.IEngine engine = null;
// FREngine.dll 的 Handle
private IntPtr dllHandle = IntPtr.Zero;
private InitializeEngine initializeEngine = null;
private DeinitializeEngine deinitializeEngine = null;
private DllCanUnloadNow dllCanUnloadNow = null;
}
若要將影像載入 FineReader Engine,可使用下列物件的方法:若要將影像載入 FRDocument 物件,請執行下列其中一項操作:上述所有方法均以 PrepareImageMode 物件作為參數,可讓您指定影像預處理的各項參數。請呼叫 IEngine::CreatePrepareImageMode 函式建立此物件,視需要修改其屬性後,再將其傳遞給需要使用它的函式。FREngine.IEngine engine;
string imagePath;
FREngine.IPrepareImageMode pim = engine.CreatePrepareImageMode();
pim.DocumentType = FREngine.DocumentTypeEnum.DT_BusinessCard;
FREngine.IFRDocument frDoc = engine.CreateFRDocument();
frDoc.AddImageFile(imagePath, pim, null);
已識別的名片 (BusinessCard 物件) 可包含下列欄位:  | - 姓名
- 公司名稱
- 公司職稱
- 公司地址
- 電話號碼
- 傳真
- 手機號碼
- 電子郵件
- 網站
|
您可以依欄位類型 (FieldByType 屬性) 或依其在欄位集合中的索引 (Field 屬性) 存取各個欄位。每個欄位都有 Value 屬性,可用來以 string 格式存取欄位值。對於欄位中的每個字元,您都可以取得其識別候選結果 (GetCharParams 方法) 。有些欄位可能由多個元件組成,例如地址欄位可包含郵遞區號、國家、USA 的州、城市及街道地址。若要存取欄位元件,您可以使用 Component 屬性或 FindComponent 方法。前者可讓您依元件的索引進行存取;後者則可依元件類型尋找元件。對於每個元件,您都可以檢視其類型和值,並取得每個字元的參數與識別候選結果 (GetCharParams 方法) 。// 取得姓名欄位
FREngine.IBusinessCardField nameField = card.FieldByType( FREngine.BusinessCardFieldTypeEnum.BCFT_Name, 0 );
// 取得包含名字的元件
FREngine.IBusinessCardFieldComponent firstNameComponent =
nameField.FindComponent( FREngine.BusinessCardFieldComponentTypeEnum.BCFCT_FirstName );
// 識別出的名字
string firstName = firstNameComponent.Value;
BusinessCard 物件提供專用的 ExportToVCard 方法,可用來將名片儲存為 vCard 格式。檔案路徑會作為參數傳入。您也可以將名片儲存為任何其他可用的匯出格式,例如 XML。// 將識別出的資料儲存為 vCard 格式
card.ExportToVCard("D:\\sample.vcf");
// 儲存為 XML
frDoc.Export("D:\\Demo.xml", FREngine.FileExportFormatEnum.FEF_XML, null);
步驟 7. 卸除 ABBYY FineReader Engine
完成 ABBYY FineReader Engine 的相關工作後,您需要卸除 Engine 物件。若要這樣做,請使用匯出的 DeinitializeEngine 函式。public class EngineLoader : IDisposable
{
// 卸除 FineReader Engine
public void Dispose()
{
if (engine == null)
{
// Engine 尚未載入
return;
}
engine = null;
// 在呼叫 FreeLibrary 之前刪除所有物件
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
int hresult = deinitializeEngine();
hresult = dllCanUnloadNow();
if (hresult == 0)
{
FreeLibrary(dllHandle);
}
dllHandle = IntPtr.Zero;
initializeEngine = null;
deinitializeEngine = null;
dllCanUnloadNow = null;
// 清理後擲回例外狀況
Marshal.ThrowExceptionForHR(hresult);
}
// Kernel32.dll 函式
[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibraryEx(string dllToLoad, IntPtr reserved, uint flags);
private const uint LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008;
[DllImport("kernel32.dll")]
private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[DllImport("kernel32.dll")]
private static extern bool FreeLibrary(IntPtr hModule);
// FREngine.dll 函式
[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)]
private delegate int InitializeEngine( string customerProjectId, string LicensePath, string LicensePassword, , , , ref FREngine.IEngine engine);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate int DeinitializeEngine();
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate int DllCanUnloadNow();
// 私有變數
private FREngine.IEngine engine = null;
// FREngine.dll 的控制代碼
private IntPtr dllHandle = IntPtr.Zero;
private InitializeEngine initializeEngine = null;
private DeinitializeEngine deinitializeEngine = null;
private DllCanUnloadNow dllCanUnloadNow = null;
}
您可以使用 FREngineDistribution.csv 檔案,自動建立應用程式正常運作所需的檔案清單。若要依此情境進行處理,請在第 5 欄 (RequiredByModule) 中選取下列值:
Core
Core.Resources
Opening
Opening, Processing
Processing
Processing.BCR
Processing.OCR
Processing.OCR, Processing.ICR
Processing.OCR.NaturalLanguages
Processing.OCR.NaturalLanguages, Processing.ICR.NaturalLanguages
Export
Export, Processing
如果您修改了標準情境,請據此調整所需模組。您也需要指定應用程式使用的介面語言、識別語言,以及任何其他附加功能 (例如,如果您需要開啟 PDF 檔案,請使用 Opening.PDF;如果您需要識別 中日韓 (CJK) 語言 的文字,請使用 Processing.OCR.CJK) 。詳情請參閱 Working with the FREngineDistribution.csv File。
以下為說明檔中的相關章節,您可在其中找到針對各個處理階段設定參數的更多資訊:
基本使用情境實作