
Aspose.OCR Cloud 使您能够在云端执行光学字符识别和文档扫描。它支持读取和识别最常用的光栅图像格式(BMP、JPG、GIF、PNG、TIFF)中的文本。使用更少的代码行对图像执行字符识别。只需将特定图像传递给 Aspose.OCR Cloud API,就会返回包含已识别文本的响应。该 API 能够识别英语、法语、西班牙语文本,并以 XML 或 JSON 格式返回响应。在本文中,我们将讨论使用 Java SDK 对图像执行 OCR 的步骤。
在识别过程中,您可以读取字符以及相关的字体信息。您可以对整个图像执行 OCR,也可以提供 X 和 Y 坐标以对光栅图像的特定部分执行 OCR。它还能够高速执行自动倾斜校正以及自动和手动文档布局检测操作,因为它不依赖硬件资源。
独立于平台
Cloud 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 命令
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 的更多信息。