Aspose.OCR Cloud系列

Aspose.OCR Cloud 可讓您在雲端執行光學字元辨識和文件掃描。它支援讀取和識別最常用的光柵圖像格式(BMPJPGGIFPNGTIFF)的文字。使用較少的程式碼行對影像進行字元辨識。只需將特定圖像傳遞給 Aspose.OCR Cloud API,即可傳回帶有識別文字的回應。該 API 能夠識別英語、法語、西班牙語文字並以 XML 或 JSON 格式傳回回應。在本文中,我們將討論使用 Java SDK 對影像執行 OCR 的步驟。

在識別過程中,您可以讀取字元以及相關的字體資訊。您可以對整個影像執行 OCR,或提供 X 和 Y 座標以對光柵影像的特定部分執行 OCR。由於它不依賴硬體資源,因此還能夠高速執行自動傾斜校正以及自動和手動文件佈局檢測操作。

獨立於平台

雲端 API 完全獨立於您的作業系統、資料庫系統或開發語言,您可以使用任何支援 HTTP 的語言和平台與我們的 API 互動。然而,手動編寫客戶端程式碼可能很困難、容易出錯、而且耗時。因此,為了方便我們的客戶使用Java語言,我們發布了特定語言的Aspose.OCR Cloud Java SDK。使用 SDK 時,它會在發出請求和處理回應時處理許多低階細節,使您能夠專注於編寫特定於您特定專案需求的程式碼。

從 URL 圖像識別文字

Cloud OCR API 提供了一個選項,可以直接對可透過 Web URL 取得的圖像檔案執行文字辨識操作。您不需要專門將其上傳到特定的雲端儲存。

Java 程式碼片段

private static OcrApi api;
private static final String url = "https://upload.wikimedia.org/wikipedia/commons/2/2f/Book_of_Abraham_FirstPage.png";

public static void main(String args[]) throws IOException {
  try {
	  		setUpConfig();
  } catch (Exception e) {
    // TODO 自動產生的 catch 區塊
    e.printStackTrace();
  }
  String text;

  text = recognizeByURL();
  System.out.println(text);
}

// 從 URL 上託管的圖像中識別文字的方法
private static String recognizeByURL() {
  try {
    api = new ApiClient().createService(OcrApi.class);
    Call<ResponseBody> call = api.RecognizeFromUrl(url);
    Response<ResponseBody> res = call.execute();
    ResponseBody answer = res.body();

    com.aspose.ocr.OCRResponse ocrResponse = com.aspose.ocr.OCRResponse.Deserialize(answer);
    String text = ocrResponse.text;

    return text;
    } catch (IOException e) {
      e.printStackTrace();
      return "";
    }
}

private static void setUpConfig() throws Exception {
  Configuration.setAPP_SID("xxxxx");
  Configuration.setAPI_KEY("xxxxx");

  Configuration.setAuthPath("https://api.aspose.cloud/connect/token");
  Configuration.setBasePath("https://api.aspose.cloud/v3.0");
  Configuration.setUserAgent("WebKit");
  Configuration.setTestSrcDir("sourceTest");
  Configuration.setTestDstDir("destTest");

  if (Configuration.getAPI_KEY().isEmpty() || Configuration.getAPP_SID().isEmpty()) {
      System.out.println("! Error: Setup AppSID & AppKey in BaseTest Configuration");
      throw new Exception("Setup AppSID & AppKey in BaseTest Configuration");
    }
}

從儲存中的圖像識別文本

Java 程式碼片段

private static OcrApi api;

public static void main(String args[]) throws IOException {
  try {
	  		setUpConfig();
  } catch (Exception e) {
    // TODO 自動產生的 catch 區塊
    e.printStackTrace();
  }
  String text;

  text = recognizeByContent();
  System.out.println(text);
}

private static String recognizeByContent() {
  try {
      File f = new File(Configuration.getTestSrcDir(), "0.png");
      if (!f.exists()) {
      return "Error: recognizeByContentLang: file not found";
      }

      api = new ApiClient().createService(OcrApi.class);
      RequestBody requestBody = RequestBody.create(f,MediaType.parse("application/octet-stream"));
      Call<ResponseBody> call = api.RecognizeFromContent(requestBody);
      Response<ResponseBody> res = call.execute();

      ResponseBody answer = res.body();
      com.aspose.ocr.OCRResponse ocrResponse = com.aspose.ocr.OCRResponse.Deserialize(answer);
      String text = ocrResponse.text;

      return text;

  } catch (IOException e) {
      e.printStackTrace();
      return "";
  }
}

private static void setUpConfig() throws Exception {
  Configuration.setAPP_SID("xxxxx");
  Configuration.setAPI_KEY("xxxxx");

  Configuration.setAuthPath("https://api.aspose.cloud/connect/token");
  Configuration.setBasePath("https://api.aspose.cloud/v3.0");
  Configuration.setUserAgent("WebKit");
  Configuration.setTestSrcDir("sourceTest");
  Configuration.setTestDstDir("destTest");

  if (Configuration.getAPI_KEY().isEmpty() || Configuration.getAPP_SID().isEmpty()) {
      System.out.println("! Error: Setup AppSID & AppKey in BaseTest Configuration");
      throw new Exception("Setup AppSID & AppKey in BaseTest Configuration");
    }
}

cURL 指令

也可以透過 cURL 指令存取雲端 API,同樣,也可以透過 cURL 指令存取 Aspose.OCR Cloud。但是,請注意,為了存取 API,我們需要根據個人化的客戶端憑證產生 JWT 存取權令牌。請執行以下命令來產生 JWT 存取令牌。

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=4ccf1790-accc-41e9-8d18-a78dbb2ed1aa&client_secret=caac6e3d4a4724b2feb53f4e460eade3" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

產生 JWT 令牌後,請使用下列指令對影像執行 OCR。

curl "https://api.aspose.cloud/v3.0/ocr/MyImage.png/recognize?language=1" \
-X GET \
-H  "accept: application/json" \
-H  "authorization: Bearer <jwt token>"

請求 URL

https://api.aspose.cloud/v3.0/ocr/MyImage.png/recognize?language=1

回應主體

{
  "text": "MOORE STEPHENS",
  "code": 200
}

結論

在本文中,我們討論瞭如何對影像執行 OCR 的細節。此外,我們也探索了使用 cURL 指令對影像執行光學字元辨識操作的選項。請注意,SDK 的完整原始碼可在GitHub上找到。同樣,我們也建議透過瀏覽產品文件來了解有關 API 的更多資訊。

相關文章