如何使用 Java 对收据的照片执行 OCR( 二 )

 
然后,我们可以使用下面的代码示例调用 Receipt OCR 函数 。在这个阶段,您需要布置一些参数并决定您希望包含哪些可选功能 。您的参数包括以下内容:

  1. 您要执行操作的输入文件
  2. 您的 Cloudmersive API 密钥(可以通过在我们的网站上注册一个免费帐户来获得,该帐户将提供每月 800 次 API 调用的限制)
您的可选功能包括以下内容:
  1. recognitionMode通过设置等于字符串来启用高级识别模式Advanced 。
  2. recognitionMode通过设置等于字符串来启用手写识别模式EnableHandwriting 。
  3. String Language通过设置等于包含首选语言的三字母标识符的字符串来设置语言(默认选项是英语)。
  4. string preprocessing通过设置等于启用预处理(默认为禁用)Advanced 。
 
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: ApikeyApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");Apikey.setApiKey("YOUR API KEY");// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.setApiKeyPrefix("Token");ImageOcrApi apiInstance = new ImageOcrApi();File imageFile = new File("/path/to/inputfile"); // File | Image file to perform OCR on.Common file formats such as PNG, JPEG are supported.String recognitionMode = "recognitionMode_example"; // String | Optional, enable advanced recognition mode by specifying 'Advanced', enable handwriting recognition by specifying 'EnableHandwriting'.Default is disabled.String language = "language_example"; // String | Optional, language of the input document, default is English (ENG).Possible values are ENG (English), ARA (Arabic), ZHO (Chinese - Simplified), ZHO-HANT (Chinese - Traditional), ASM (Assamese), AFR (Afrikaans), AMH (Amharic), AZE (Azerbaijani), AZE-CYRL (Azerbaijani - Cyrillic), BEL (Belarusian), BEN (Bengali), BOD (Tibetan), BOS (Bosnian), BUL (Bulgarian), CAT (Catalan; Valencian), CEB (Cebuano), CES (Czech), CHR (Cherokee), CYM (Welsh), DAN (Danish), DEU (German), DZO (Dzongkha), ELL (Greek), ENM (Archaic/Middle English), EPO (Esperanto), EST (Estonian), EUS (Basque), FAS (Persian), FIN (Finnish), FRA (French), FRK (Frankish), FRM (Middle-French), GLE (Irish), GLG (Galician), GRC (Ancient Greek), HAT (Hatian), HEB (Hebrew), HIN (Hindi), HRV (Croatian), HUN (Hungarian), IKU (Inuktitut), IND (Indonesian), ISL (Icelandic), ITA (Italian), ITA-OLD (Old - Italian), JAV (Javanese), JPN (Japanese), KAN (Kannada), KAT (Georgian), KAT-OLD (Old-Georgian), KAZ (Kazakh), KHM (Central Khmer), KIR (Kirghiz), KOR (Korean), KUR (Kurdish), LAO (Lao), LAT (Latin), LAV (Latvian), LIT (Lithuanian), MAL (Malayalam), MAR (Marathi), MKD (macedonian), MLT (Maltese), MSA (Malay), MYA (Burmese), NEP (Nepali), NLD (Dutch), NOR (Norwegian), ORI (Oriya), PAN (Panjabi), POL (Polish), POR (Portuguese), PUS (Pushto), RON (Romanian), RUS (Russian), SAN (Sanskrit), SIN (Sinhala), SLK (Slovak), SLV (Slovenian), SPA (Spanish), SPA-OLD (Old Spanish), SQI (Albanian), SRP (Serbian), SRP-LAT (Latin Serbian), SWA (Swahili), SWE (Swedish), SYR (Syriac), TAM (Tamil), TEL (Telugu), TGK (Tajik), TGL (Tagalog), THA (Thai), TIR (Tigrinya), TUR (Turkish), UIG (Uighur), UKR (Ukrainian), URD (Urdu), UZB (Uzbek), UZB-CYR (Cyrillic Uzbek), VIE (Vietnamese), YID (Yiddish)String preprocessing = "preprocessing_example"; // String | Optional, preprocessing mode, default is 'None'.Possible values are None (no preprocessing of the image), and 'Advanced' (automatic image enhancement of the image before OCR is Applied; this is recommended and needed to handle rotated receipts).try {ReceiptRecognitionResult result = apiInstance.imageOcrPhotoRecognizeReceipt(imageFile, recognitionMode, language, preprocessing);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling ImageOcrApi#imageOcrPhotoRecognizeReceipt");e.printStackTrace();} 
一旦您配置了可选功能,您的 API 调用就完成并准备好进行测试 。下面,我提供了一个示例 API 响应模型 JSON 供您参考:
JSON
{"Successful": true,"Timestamp": "2022-07-14T14:33:18.565Z","BusinessName": "string","BusinessWebsite": "string","AddressString": "string","PhoneNumber": "string","ReceiptItems": [{"ItemDescription": "string","ItemPrice": 0}],"ReceiptSubTotal": 0,"ReceiptTotal": 0} 
为了进一步提高 OCR 图像的质量,我建议深入研究预处理API 并测试可能解决最常见问题的那些 。例如,在 OCR 照片中,在深色背景上以小角度查看文档是很常见的 。借助有效的预处理服务,您可以自动检测这些角度并自动校正它们,从而确保您的 OCR 应用程序以尽可能高的准确度检测文本 。


推荐阅读