翻譯|使用教程|編輯:胡濤|2022-11-15 14:14:11.653|閱讀 213 次
概述:本文介紹在leadtools中,使用 AWS Lambda 轉換文檔,歡迎查閱~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
LEADTOOLS 是一個綜合工具包的集合,用于將識別、文檔、醫療、成像和多媒體技術整合到桌面、服務器、平板電腦、網絡和移動解決方案中,是一項企業級文檔自動化解決方案,有捕捉,OCR,OMR,表單識別和處理,PDF,打印捕獲,歸檔,注釋和顯示功能。利用業界領先的圖像處理技術,能夠智能識別文件,可以用來識別任何類型的掃描或傳真形式的圖像。
本教程展示了如何配置和運行 .NET Core 文檔服務。
概括 | 本教程介紹如何設置 AWS Lambda 以與 LEADTOOLS SDK 一起使用以及如何使用 .NET Core 轉換文檔。 |
完成時間 | 60分鐘 |
平臺 | |
IDE | Visual Studio 2019、2022、AWS Lambda Visual Studio 擴展 |
開發許可證 | 下載 LEADTOOLS |
在學習從 LEADDocument中添加和刪除頁面 - C# .NET Core 教程之前,通過查看添加引用和設置許可教程來熟悉創建項目的基本步驟。
完成 Visual Studio 的 AWS 設置;
要設置開發環境以在 Visual Studio 中使用 AWS,請完成來自 Amazon 的以下 2 個教程:
完成這些教程后,應安裝適用于 Visual Studio 的 AWS 工具包,并基本了解如何為 .NET Core 創建新的 AWS Lambda 項目并發布到 AWS。
在 Visual Studio 中,創建一個新的AWS Lambda 項目(.NET Core - C#)項目。為項目命名和位置,然后單擊創建。
選擇一個空函數并單擊確定。
所需的參考取決于項目的目的。對于這個項目,需要以下 NuGet 包:
在解決方案資源管理器中右鍵單擊 C# 項目,然后選擇管理 NuGet 包...。
瀏覽LEADTOOLS,然后選擇Leadtools.Document.SdkNuGet 包并安裝它。接受 LEAD 的最終用戶許可協議。
有關特定格式所需的編解碼器 DLL 的完整列表,請參閱。
添加 LEADRequest.cs 類右鍵單擊 C# 項目并選擇Add,然后選擇Class。為其命名LEADRequest.cs并單擊添加。
將以下內容添加到using塊中:
【C#】
using Leadtools.Document.Writer;
【C#】
public class LEADRequest { public string InputUrl { get; set; } public DocumentFormat DocumentFormat { get; set; } public LEADRequest() { } }
右鍵單擊 C# 項目并選擇Add,然后選擇Class。為其命名LEADLambdaHandler.cs并單擊添加。
在新類中,將以下內容添加到using塊中:
【C#】
using System; using System.Diagnostics; using System.IO; using System.Net.Http; using Amazon.Lambda.Core; using Leadtools; using Leadtools.Document; using Leadtools.Document.Converter; using Leadtools.Document.Writer; using Leadtools.Ocr;
復制下面的代碼以將所需的功能添加到類中:
【C#】
public class LEADLambdaHandler { // Global Variables IOcrEngine ocrEngine; DocumentConverter documentConverter; static HttpClient httpClient = new HttpClient(); // Constructor which handles all initialization to make sure the function is as fast as possible once it is warmed up public LEADLambdaHandler() { InitEnvironment(); Platform.LibraryPath = "/opt/native-runtimes/"; InitLEADTOOLS(); } // Method which initializes the Lambda environment for use by the LEADTOOLS SDK void InitEnvironment() { // Set the LD_LIBRARY_PATH in function console to include: // /var/lang/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib:/tmp ExecuteBashCommand("ln -s /lib64/libdl.so.2 /tmp/libdl.so"); } // Initialize the LEADTOOLS SDK Classes void InitLEADTOOLS() { SetLicense(); RasterDefaults.TemporaryDirectory = "/tmp"; RasterDefaults.SetResourceDirectory(LEADResourceDirectory.Fonts, "/opt/ShadowFonts"); ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD); ocrEngine.Startup(null, null, "/tmp", "/opt/OcrLEADRuntime"); documentConverter = new DocumentConverter(); documentConverter.SetOcrEngineInstance(ocrEngine, true); } // Helper method for executing shell scripts in the Lambda environment string ExecuteBashCommand(string command) { command = command.Replace("\"", "\"\""); var proc = new Process { StartInfo = new ProcessStartInfo { FileName = "/bin/bash", Arguments = "-c \"" + command + "\"", UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true } }; proc.Start(); proc.WaitForExit(); return proc.StandardOutput.ReadToEnd(); } // Set license code TODO: Replace the licString and developerKey with a valid license and key void SetLicense() { string licString = "[License]\n" + "License = <doc><ver>2.0</ver>`ADD LICENSE HERE`</doc>"; string developerKey = "ADD DEVELOPMENT KEY HERE"; byte[] licBytes = System.Text.Encoding.UTF8.GetBytes(licString); RasterSupport.SetLicense(licBytes, developerKey); } // Main conversion function public string ConvertDocument(LEADRequest request, StringWriter sw) { var isUrl = Uri.IsWellFormedUriString(request.InputUrl, UriKind.RelativeOrAbsolute); if (isUrl) { var response = httpClient.GetAsync(request.InputUrl).Result; if (response.IsSuccessStatusCode) { var stream = response.Content.ReadAsStreamAsync().Result; using (var document = DocumentFactory.LoadFromStream(stream, new LoadDocumentOptions())) { string ext = DocumentWriter.GetFormatFileExtension(request.DocumentFormat); string fileName = Path.Combine("/tmp", Path.ChangeExtension(Path.GetFileName(request.InputUrl), ext)); DocumentConverterJobData jobData = DocumentConverterJobs.CreateJobData(document, fileName, request.DocumentFormat); DocumentConverterJob job = documentConverter.Jobs.CreateJob(jobData); documentConverter.Jobs.RunJob(job); if (job.Errors.Count > 0) foreach (var error in job.Errors) sw.WriteLine($"Error during conversion: {error.Error.Message} {error.Error.StackTrace}"); else return fileName; } } else sw.WriteLine("Download of URL is not successful"); } else sw.WriteLine("Url is invalid."); return null; } }
許可證解鎖項目所需的功能。它必須在調用任何工具包函數之前設置。有關詳細信息,包括針對不同平臺的教程,請參閱。
有兩種類型的運行時許可證:
筆記
教程 中有更詳細的介紹。
當觸發 AWS Lambda 函數時,將FunctionHandler調用該方法。Function函數首次啟動時,將在此之前調用全局類中的所有內容。當容器處于warm狀態時,它只會調用該FunctionHandler方法,直到執行上下文發生變化。
為了利用此功能,大部分初始化代碼都在構造函數中完成,以便在容器處于熱LEADLambdaHandler狀態時盡可能快地調用每個函數。
打開Function.cs作為項目一部分包含的文件,并將以下內容添加到using其余 using 語句下的塊中。
【C#】
using System.IO;
將以下全局變量添加到Function類中:
【C#】
public LEADLambdaHandler LEADHandler = new LEADLambdaHandler();
將該方法替換為FunctionHandler以下方法以接受 aLEADRequest作為輸入而不是字符串輸入,然后調用該LEADHandler.ConvertDocument方法:
【C#】
public string FunctionHandler(LEADRequest request, ILambdaContext context) { StringWriter sw = new StringWriter(); try { string outputFile = LEADHandler.ConvertDocument(request, sw); if (outputFile != null) sw.WriteLine($"Successfully saved to {outputFile}."); else sw.WriteLine("Error occurred. Output file not saved."); } catch (Exception ex) { sw.WriteLine(ex.Message); sw.WriteLine(ex.StackTrace); if (ex.InnerException != null) { sw.WriteLine(ex.InnerException.Message); sw.WriteLine(ex.InnerException.StackTrace); } } return sw.ToString(); }
添加所有代碼后,構建項目以確保一切都按預期工作。
筆記
如果需要,可以跳過此步驟。此項目中包含的 ZIP 已包含所需的 Lambda 依賴項。
LEADTOOLS SDK 需要在 Linux 機器上安裝某些依賴項才能正常運行。有關完整列表,請參閱。
AWS Lambda 提供了一個包含相同 Linux 發行版的 Docker 鏡像,以便在部署到 Lambda 之前測試功能。這也可用于安裝所需的依賴項并創建部署包。這需要安裝Docker Desktop。
indir="/var/task/" outdir="/var/task/lib/" ldcache=$(ldconfig -p) mkdir -p $outdir for i in $(ls ${indir}/*.so); do deps=$(ldd $i | grep "=>" | grep -v "liblt") while IFS= read -r line; do result="" list=( $line ) dep=$(echo $ldcache | grep "${list[0]}") if [ ! -z "$dep" ] ; then cp -vL "${list[2]}" $outdir 2> /dev/null fi done <<< "$deps" done
LEADTOOLS SDK 需要在 Lambda 函數中安裝環境依賴項。為了保持包的小和靈活,使用了 Lambda 層。有關層是什么的更多信息,請參閱。
LEADTOOLS SDK OCR 引擎需要依賴項包含在任何 OCR 部署中。有關所需內容的更多信息,請參見。
由于需要 AWS Lambda 環境,因此還需要 Shadow Fonts。有關 Shadow Fonts 的更多信息,請訪問。
壓縮lead-deps-layer文件夾的內容。確保 ziplead-deps-layer.zip不包含同名的子文件夾,并且 zip 結構類似于以下屏幕截圖:
創建層 zip 后,需要將其上傳到 AWS Lambda。
該層完成后,現在可以將函數發布到 AWS。
這將dotnet publish在項目上運行,然后壓縮文件和依賴項并將其上傳到 Lambda。
在上一步中創建的包會很大,因為它包含許多添加到層的依賴項。為了減小體積,zip 包將需要修改并手動上傳到 Lambda。
這將更新功能包,使其盡可能小。
上傳包后,LD_LIBRARY_PATH需要更新函數的環境變量以正確加載依賴項。
設置環境變量后,需要將層添加到函數中。
單擊添加將此圖層添加到函數中
確保函數的內存設置為 2048 MB:
單擊保存以保存更改
完成上一步后,該功能就可以進行測試了。
{ "InputUrl": "http://demo.leadtools.com/images/tiff/ocr1.tif", "DocumentFormat": 1 }
如果一切設置正確,執行結果應該返回succeeded并類似于:
以上便是 使用 AWS Lambda 轉換文檔 ,如果您還有其他疑問,歡迎咨詢我們或者加入我們官方技術交流群。
歡迎下載|體驗更多LEADTOOL產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn