
概覽
光學標記識別(OMR)是一個電子過程,促進了閱讀和捕捉人們在專門設計的文件表格(例如測試或調查)上標記的資料,這些表格包括用戶填寫的圓圈或方形輸入。通過使用在 Java 中從影像中提取資料,我們可以高效地處理這些調查表、問卷或測試表的掃描影像,使得程式能夠讀取用戶的輸入。本文將指導您如何在 Java 中執行 OMR 並從影像中提取資料。
本篇文章將涵蓋以下主題:
Java OMR API 提取影像中的資料
要執行 OMR 操作並在 Java 中從影像中提取資料,我們將使用Aspose.OMR for Java API。這個強大的工具可以設計、創建和識別答卷、測試、選擇題、測驗、反饋表、調查和選票。
API 中的OmrEngine類負責創建模板和處理影像。其getTemplateProcessor(String templatePath)方法初始化一個TemplateProcessor實例,專門用於處理模板和影像。要識別影像,可以使用recognizeImage(String imagePath)方法,該方法返回所有 OMR 元素作為RecognitionResult類實例。通過getCsv()方法,您可以生成一個包含識別結果的CSV字串。此外,recalculate(RecognitionResult result, int recognitionThreshold)方法可以根據自訂參數完善識別結果。
請下載該 API 的JAR或在基於 Maven 的 Java 應用中添加以下 pom.xml 配置。
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-omr</artifactId>
<version>19.12</version>
</dependency>
在 Java 中從影像中提取資料
要執行 OMR 操作,我們需要準備好的 OMR 模板文件 (.omr) 和用戶填寫的表單或表格影像。使用 OMR 操作進行在 Java 中從影像中提取資料的過程涉及以下步驟:
- 首先,創建**OmrEngine**類的實例。
- 接下來,調用**getTemplateProcessor()方法並初始化一個TemplateProcessor**類對象,將 OMR 模板文件路徑作為參數傳遞。
- 然後,通過調用**recognizeImage()方法使用影像路徑作為參數來獲取RecognitionResult**對象。
- 之後,使用**getCsv()**方法獲得作為 CSV 字串的識別結果。
- 最後,將 CSV 結果作為 CSV 文件保存在本地磁碟上。
以下代碼範例演示了如何通過將 OMR 資料轉換為 CSV 格式來執行在 Java 中從影像中提取資料。
// This code example demonstrates how to perform OMR on an image and extract data | |
// OMR Template file path | |
String templatePath = "C:\\Files\\OMR\\Sheet.omr"; | |
// Image file path | |
String imagePath = "C:\\Files\\OMR\\Sheet1.png"; | |
// Initialize OMR Engine | |
OmrEngine engine = new OmrEngine(); | |
// Get template processor | |
TemplateProcessor templateProcessor = engine.getTemplateProcessor(templatePath); | |
// Recognize image | |
RecognitionResult result = templateProcessor.recognizeImage(imagePath); | |
// Get results in CSV | |
String csvResult = result.getCsv(); | |
// Save CSV file | |
PrintWriter wr = new PrintWriter(new FileOutputStream("C:\\Files\\OMR\\Sheet1.csv"), true); | |
wr.println(csvResult); |

在 Java 中執行 OMR 並從影像中提取資料。
執行 OMR 並從多個影像中提取資料
我們可以對多個影像執行 OMR 操作,並為每個影像提取資料到單獨的 CSV 文件中,使用前面概述的步驟。要完成在 Java 中從影像中提取資料,需要對所有影像分別重複步驟 3、4 和 5。
以下是代碼範例,演示如何使用 Java 從多個影像中提取 OMR 資料。
// This code example demonstrates how to perform OMR on multiple images and extract data | |
// Working folder path | |
String folderPath = "C:\\Files\\OMR\\"; | |
// OMR Template file path | |
String templatePath = folderPath + "Sheet.omr"; | |
// Image file path | |
String[] UserImages = new String[] { "Sheet1.png", "Sheet2.png" }; | |
// Initialize OMR Engine | |
OmrEngine engine = new OmrEngine(); | |
// Get template processor | |
TemplateProcessor templateProcessor = engine.getTemplateProcessor(templatePath); | |
// Process images one by one in a loop | |
for (int i = 0; i < UserImages.length; i++) | |
{ | |
String image = UserImages[i]; | |
String imagePath = folderPath + image; | |
// Recognize image | |
RecognitionResult result = templateProcessor.recognizeImage(imagePath); | |
// Get results in CSV | |
String csvResult = result.getCsv(); | |
// Save CSV file | |
PrintWriter wr = new PrintWriter(new FileOutputStream(folderPath + "Sheet_" + i + ".csv"), true); | |
wr.println(csvResult); | |
System.out.println(csvResult); | |
} |
使用閾值提取 OMR 資料在 Java
在 Java 中執行光學標記識別(OMR)操作時,我們利用指定需求之間的 0 到 100 的閾值。此閾值在在 Java 中從影像中提取資料時會影響 API 在突出答案時的嚴格程度;較高的值會增加嚴格性。遵循先前提到的步驟對於使用所選閾值進行 OMR 處理至關重要。具體而言,在步驟 #3 中,必須調用**recognizeImage(string, int32)**方法。此重載方法需要影像文件路徑和所需的閾值作為其參數。
以下代碼範例演示了如何使用閾值值執行 OMR:
// This code example demonstrates how to perform OMR with therashold and extract data from an image | |
// OMR Template file path | |
String templatePath = "C:\\Files\\OMR\\Sheet.omr"; | |
// Image file path | |
String imagePath = "C:\\Files\\OMR\\Sheet1.png"; | |
// Threshold value | |
int CustomThreshold = 40; | |
// Initialize OMR Engine | |
OmrEngine engine = new OmrEngine(); | |
// Get template processor | |
TemplateProcessor templateProcessor = engine.getTemplateProcessor(templatePath); | |
// Recognize image | |
RecognitionResult result = templateProcessor.recognizeImage(imagePath, CustomThreshold); | |
// Get results in CSV | |
String csvResult = result.getCsv(); | |
// Save CSV file | |
PrintWriter wr = new PrintWriter(new FileOutputStream("C:\\Files\\OMR\\Sheet1_threshold.csv"), true); | |
wr.println(csvResult); | |
System.out.println(csvResult); |
使用重新計算提取 OMR 資料在 Java
處理精確的在 Java 中從影像中提取資料時,特別是與 OMR 相關時,可能需要使用不同的閾值重新計算結果。通過配置 API,重新計算可以通過**TemplateProcessor.recalculate()**方法自動化。此方法允許通過調整閾值來進行多次影像處理迭代,直到達到所需的結果。要成功執行帶有重新計算的 OMR 操作,請遵循以下步驟:
- 首先,創建**OmrEngine**類的實例。
- 接下來,調用**getTemplateProcessor()方法並初始化TemplateProcessor**類對象。它需要 OMR 模板文件路徑作為參數。
- 然後,通過調用**recognizeImage()方法使用影像路徑作為參數來獲得RecognitionResult**對象。
- 接下來,使用**getCsv()**方法將識別結果導出為 CSV 字串。
- 然後,將 CSV 結果作為 CSV 文件保存在本地磁碟上。
- 接下來,調用**recalculate()**方法。它需要 RecognitionResult 對象和閾值作為參數。
- 之後,使用**getCsv()**方法將識別結果導出為 CSV 字串。
- 最後,將 CSV 結果作為 CSV 文件保存在本地磁碟上。
以下代碼範例演示了如何使用重新計算方法執行 OMR:
// OMR Template file path | |
String templatePath = "C:\\Files\\OMR\\Sheet.omr"; | |
// Image file path | |
String imagePath = "C:\\Files\\OMR\\Sheet1.png"; | |
// Threshold value | |
int CustomThreshold = 40; | |
// Initialize OMR Engine | |
OmrEngine engine = new OmrEngine(); | |
// Get template processor | |
TemplateProcessor templateProcessor = engine.getTemplateProcessor(templatePath); | |
// Recognize image | |
RecognitionResult result = templateProcessor.recognizeImage(imagePath, CustomThreshold); | |
// Get results in CSV | |
String csvResult = result.getCsv(); | |
// Save CSV file | |
PrintWriter wr = new PrintWriter(new FileOutputStream("C:\\Files\\OMR\\Sheet1.csv"), true); | |
wr.println(csvResult); | |
// Recalculate | |
// You may apply new threshold value here | |
templateProcessor.recalculate(result, CustomThreshold); | |
// Get recalculated results in CSV | |
csvResult = result.getCsv(); | |
// Save recalculated resultant CSV file | |
PrintWriter finalWr = new PrintWriter(new FileOutputStream("C:\\Files\\OMR\\Sheet1_recalculated.csv"), true); | |
finalWr.println(csvResult); |
獲取免費許可證
您有機會獲取免費的臨時許可證,以嘗試該庫而不受評估限制。這是探索如在 Java 中從影像中提取資料等功能的好方法,讓您能夠充分評估其能力。以下的列表數據保持不變以供參考:
- 該庫能有效處理大量數據。
- 與現有系統的整合無縫。
- 提取的數據高度準確且可靠。
- 安裝步驟簡單且文檔完善。
結論
在本文中,我們學會了如何:
- 在影像上執行 OMR 操作;
- 以程式化方式提取 CSV 格式的資料;
- 在對影像執行 OMR 時應用閾值設置;
- 使用 Java 進行自動化過程重新計算 OMR 結果。
此外,在處理 Java 中的影像資料提取時,您可以通過查看Aspose.OMR for Java API 的文檔來進一步了解。如果您遇到任何問題,請隨時在我們的免費支持論壇上聯繫我們。