
Aspose.OCR Cloud を使用すると、クラウドで光学文字認識とドキュメント スキャンを実行できます。最も一般的に使用されるラスター イメージ形式 (BMP、JPG、GIF、PNG、TIFF) からのテキストの読み取りと認識をサポートしています。少ないコード行でイメージの文字認識を実行します。特定のイメージを Aspose.OCR Cloud API に渡すだけで、認識されたテキストを含む応答が返されます。API は英語、フランス語、スペイン語のテキストを認識でき、XML または JSON 形式で応答を返します。この記事では、Java SDK を使用してイメージの OCR を実行する手順について説明します。
認識プロセス中に、文字だけでなく関連するフォント情報も読み取ることができます。画像全体に OCR を実行することも、X 座標と Y 座標を指定してラスター画像の特定の部分に対して実行することもできます。また、ハードウェア リソースに依存しないため、自動スキュー補正、自動および手動のドキュメント レイアウト検出操作を高速に実行することもできます。
プラットフォームに依存しない
クラウド 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 自動生成されたキャッチブロック
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 自動生成されたキャッチブロック
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コマンド
Cloud API は cURL コマンド経由でもアクセスでき、同様に Aspose.OCR Cloud も cURL コマンド経由でアクセスできます。ただし、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 についてさらに学習することもお勧めします。