你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
FormRecognizerAsyncClient 类
- java.
lang. Object - com.
azure. ai. formrecognizer. FormRecognizerAsyncClient
- com.
public final class FormRecognizerAsyncClient
此类提供一个异步客户端,用于连接到 表单识别器 Azure 认知服务。
此客户端提供用于执行以下操作的异步方法:
- 自定义表单分析:从特定于不同业务数据和用例的表单和文档中提取和分析数据。 通过将自定义训练的模型 modelId 传递到 方法, beginRecognizeCustomForms 使用该模型。
- 预生成模型分析:使用 支持的预生成模型 分析收据、名片、发票和其他文档 使用 beginRecognizeReceipts 方法识别收据信息。
- 布局分析:从窗体和文档中提取和分析文本、选择标记、表格和边界框坐标。 使用 beginRecognizeContent tpo 方法执行布局分析。
- 轮询和回调:它包括轮询服务以检查分析操作的状态或注册回调以在分析完成时接收通知的机制。
注意: 此客户端仅支持 V2_1 和 更低版本。 建议使用较新的服务版本 DocumentAnalysisClient 和 DocumentModelAdministrationClient。
若要使用 API 版本 2022-08-31 及更新,请参阅 迁移指南 。
服务客户端是开发人员使用 Azure 表单识别器的交互点。 FormRecognizerClient 是同步服务客户端, FormRecognizerAsyncClient 是异步服务客户端。 本文档中显示的示例使用名为 DefaultAzureCredential 的凭据对象进行身份验证,该对象适用于大多数方案,包括本地开发和生产环境。 此外,我们建议使用 托管标识 在生产环境中进行身份验证。 可以在 Azure 标识文档中找到有关不同身份验证方式及其相应凭据类型的详细信息。
示例:使用 DefaultAzureCredential 构造 FormRecognizerClient
下面的代码示例演示如何 FormRecognizerAsyncClient使用“DefaultAzureCredentialBuilder”对其进行配置来创建 。
FormRecognizerAsyncClient formRecognizerAsyncClient = new FormRecognizerClientBuilder()
.endpoint("{endpoint}")
.credential(new DefaultAzureCredentialBuilder().build())
.buildAsyncClient();
此外,请参阅下面的代码示例,以用于 AzureKeyCredential 创建客户端。
FormRecognizerAsyncClient formRecognizerAsyncClient = new FormRecognizerClientBuilder()
.credential(new AzureKeyCredential("{key}"))
.endpoint("{endpoint}")
.buildAsyncClient();
方法摘要
方法继承自 java.lang.Object
方法详细信息
beginRecognizeBusinessCards
public PollerFlux<>
使用光学字符识别 (OCR) 和预生成的业务卡训练模型来识别业务卡数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
有关业务卡上的字段,请参阅此处。 请注意, businessCard
如果启用了重试 (默认) ,则传递的 必须可重播。 换句话说, Flux
必须在每次订阅时生成相同的数据。
代码示例
File businessCard = new File("{local/file_path/fileName.jpg}");
Flux<ByteBuffer> buffer = toFluxByteBuffer(new ByteArrayInputStream(Files.readAllBytes(businessCard.toPath())));
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeBusinessCards(buffer, businessCard.length())
.flatMap(AsyncPollResponse::getFinalResult)
.subscribe(recognizedBusinessCards -> {
for (int i = 0; i < recognizedBusinessCards.size(); i++) {
RecognizedForm recognizedForm = recognizedBusinessCards.get(i);
Map<String, FormField> recognizedFields = recognizedForm.getFields();
System.out.printf("----------- Recognized Business Card page %d -----------%n", i);
FormField contactNamesFormField = recognizedFields.get("ContactNames");
if (contactNamesFormField != null) {
if (FieldValueType.LIST == contactNamesFormField.getValue().getValueType()) {
List<FormField> contactNamesList = contactNamesFormField.getValue().asList();
contactNamesList.stream()
.filter(contactName -> FieldValueType.MAP == contactName.getValue().getValueType())
.map(contactName -> {
System.out.printf("Contact name: %s%n", contactName.getValueData().getText());
return contactName.getValue().asMap();
})
.forEach(contactNamesMap -> contactNamesMap.forEach((key, contactName) -> {
if ("FirstName".equals(key)) {
if (FieldValueType.STRING == contactName.getValue().getValueType()) {
String firstName = contactName.getValue().asString();
System.out.printf("\tFirst Name: %s, confidence: %.2f%n",
firstName, contactName.getConfidence());
}
}
if ("LastName".equals(key)) {
if (FieldValueType.STRING == contactName.getValue().getValueType()) {
String lastName = contactName.getValue().asString();
System.out.printf("\tLast Name: %s, confidence: %.2f%n",
lastName, contactName.getConfidence());
}
}
}));
}
}
FormField jobTitles = recognizedFields.get("JobTitles");
if (jobTitles != null) {
if (FieldValueType.LIST == jobTitles.getValue().getValueType()) {
List<FormField> jobTitlesItems = jobTitles.getValue().asList();
jobTitlesItems.forEach(jobTitlesItem -> {
if (FieldValueType.STRING == jobTitlesItem.getValue().getValueType()) {
String jobTitle = jobTitlesItem.getValue().asString();
System.out.printf("Job Title: %s, confidence: %.2f%n",
jobTitle, jobTitlesItem.getConfidence());
}
});
}
}
}
});
Parameters:
Returns:
beginRecognizeBusinessCards
public PollerFlux<>
使用光学字符识别 (OCR) 和预生成的业务卡训练模型识别文档中的业务卡数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
有关业务卡上的字段,请参阅此处。 请注意, businessCard
如果启用了重试 (默认) ,则传递的 必须可重播。 换句话说, Flux
必须在每次订阅时生成相同的数据。
代码示例
File businessCard = new File("{local/file_path/fileName.jpg}");
boolean includeFieldElements = true;
// Utility method to convert input stream to Byte buffer
Flux<ByteBuffer> buffer = toFluxByteBuffer(new ByteArrayInputStream(Files.readAllBytes(businessCard.toPath())));
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeBusinessCards(buffer, businessCard.length(),
new RecognizeBusinessCardsOptions()
.setContentType(FormContentType.IMAGE_JPEG)
.setFieldElementsIncluded(includeFieldElements))
.setPollInterval(Duration.ofSeconds(5))
.flatMap(AsyncPollResponse::getFinalResult)
.subscribe(recognizedBusinessCards -> {
for (int i = 0; i < recognizedBusinessCards.size(); i++) {
RecognizedForm recognizedForm = recognizedBusinessCards.get(i);
Map<String, FormField> recognizedFields = recognizedForm.getFields();
System.out.printf("----------- Recognized Business Card page %d -----------%n", i);
FormField contactNamesFormField = recognizedFields.get("ContactNames");
if (contactNamesFormField != null) {
if (FieldValueType.LIST == contactNamesFormField.getValue().getValueType()) {
List<FormField> contactNamesList = contactNamesFormField.getValue().asList();
contactNamesList.stream()
.filter(contactName -> FieldValueType.MAP == contactName.getValue().getValueType())
.map(contactName -> {
System.out.printf("Contact name: %s%n", contactName.getValueData().getText());
return contactName.getValue().asMap();
})
.forEach(contactNamesMap -> contactNamesMap.forEach((key, contactName) -> {
if ("FirstName".equals(key)) {
if (FieldValueType.STRING == contactName.getValue().getValueType()) {
String firstName = contactName.getValue().asString();
System.out.printf("\tFirst Name: %s, confidence: %.2f%n",
firstName, contactName.getConfidence());
}
}
if ("LastName".equals(key)) {
if (FieldValueType.STRING == contactName.getValue().getValueType()) {
String lastName = contactName.getValue().asString();
System.out.printf("\tLast Name: %s, confidence: %.2f%n",
lastName, contactName.getConfidence());
}
}
}));
}
}
FormField jobTitles = recognizedFields.get("JobTitles");
if (jobTitles != null) {
if (FieldValueType.LIST == jobTitles.getValue().getValueType()) {
List<FormField> jobTitlesItems = jobTitles.getValue().asList();
jobTitlesItems.forEach(jobTitlesItem -> {
if (FieldValueType.STRING == jobTitlesItem.getValue().getValueType()) {
String jobTitle = jobTitlesItem.getValue().asString();
System.out.printf("Job Title: %s, confidence: %.2f%n",
jobTitle, jobTitlesItem.getConfidence());
}
});
}
}
}
});
Parameters:
Returns:
beginRecognizeBusinessCardsFromUrl
public PollerFlux<>
使用光学字符识别 (OCR) 和预生成的业务卡训练模型来识别业务卡数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
有关业务卡上的字段,请参阅此处。
代码示例
String formUrl = "{form_url}";
String modelId = "{custom_trained_model_id}";
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeCustomFormsFromUrl(modelId, formUrl)
// if training polling operation completed, retrieve the final result.
.flatMap(AsyncPollResponse::getFinalResult)
.flatMap(Flux::fromIterable)
.subscribe(recognizedForm -> recognizedForm.getFields()
.forEach((fieldText, formField) -> {
System.out.printf("Field text: %s%n", fieldText);
System.out.printf("Field value data text: %s%n", formField.getValueData().getText());
System.out.printf("Confidence score: %.2f%n", formField.getConfidence());
}));
Parameters:
Returns:
beginRecognizeBusinessCardsFromUrl
public PollerFlux<>
使用光学字符识别 (OCR) 和预先生成的业务卡训练模型来识别业务卡数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持。
有关在企业卡中找到的字段,请参阅此处。
代码示例
String businessCardUrl = "{business_card_url}";
boolean includeFieldElements = true;
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeBusinessCardsFromUrl(businessCardUrl,
new RecognizeBusinessCardsOptions()
.setFieldElementsIncluded(includeFieldElements))
.setPollInterval(Duration.ofSeconds(5))
.flatMap(AsyncPollResponse::getFinalResult)
.subscribe(recognizedBusinessCards -> {
for (int i = 0; i < recognizedBusinessCards.size(); i++) {
RecognizedForm recognizedBusinessCard = recognizedBusinessCards.get(i);
Map<String, FormField> recognizedFields = recognizedBusinessCard.getFields();
System.out.printf("----------- Recognized Business Card page %d -----------%n", i);
FormField contactNamesFormField = recognizedFields.get("ContactNames");
if (contactNamesFormField != null) {
if (FieldValueType.LIST == contactNamesFormField.getValue().getValueType()) {
List<FormField> contactNamesList = contactNamesFormField.getValue().asList();
contactNamesList.stream()
.filter(contactName -> FieldValueType.MAP == contactName.getValue().getValueType())
.map(contactName -> {
System.out.printf("Contact name: %s%n", contactName.getValueData().getText());
return contactName.getValue().asMap();
})
.forEach(contactNamesMap -> contactNamesMap.forEach((key, contactName) -> {
if ("FirstName".equals(key)) {
if (FieldValueType.STRING == contactName.getValue().getValueType()) {
String firstName = contactName.getValue().asString();
System.out.printf("\tFirst Name: %s, confidence: %.2f%n",
firstName, contactName.getConfidence());
}
}
if ("LastName".equals(key)) {
if (FieldValueType.STRING == contactName.getValue().getValueType()) {
String lastName = contactName.getValue().asString();
System.out.printf("\tLast Name: %s, confidence: %.2f%n",
lastName, contactName.getConfidence());
}
}
}));
}
}
FormField jobTitles = recognizedFields.get("JobTitles");
if (jobTitles != null) {
if (FieldValueType.LIST == jobTitles.getValue().getValueType()) {
List<FormField> jobTitlesItems = jobTitles.getValue().asList();
jobTitlesItems.forEach(jobTitlesItem -> {
if (FieldValueType.STRING == jobTitlesItem.getValue().getValueType()) {
String jobTitle = jobTitlesItem.getValue().asString();
System.out.printf("Job Title: %s, confidence: %.2f%n",
jobTitle, jobTitlesItem.getConfidence());
}
});
}
}
}
});
Parameters:
Returns:
beginRecognizeContent
public PollerFlux<>
使用光学字符识别 (OCR) 识别内容/布局数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持。
请注意, data
如果启用重试 (默认) ,则传递的 必须可重播。 换句话说, Flux
每次订阅 时都必须生成相同的数据。
代码示例
File form = new File("{local/file_path/fileName.jpg}");
// Utility method to convert input stream to Byte buffer
Flux<ByteBuffer> buffer = toFluxByteBuffer(new ByteArrayInputStream(Files.readAllBytes(form.toPath())));
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeContent(buffer, form.length())
.flatMap(AsyncPollResponse::getFinalResult)
.flatMap(Flux::fromIterable)
.subscribe(formPage -> {
System.out.printf("Page Angle: %s%n", formPage.getTextAngle());
System.out.printf("Page Dimension unit: %s%n", formPage.getUnit());
// Table information
System.out.println("Recognized Tables: ");
formPage.getTables().forEach(formTable ->
formTable.getCells().forEach(recognizedTableCell ->
System.out.printf("%s ", recognizedTableCell.getText())));
});
Parameters:
Returns:
beginRecognizeContent
public PollerFlux<>
使用光学字符识别 (OCR) 识别内容/布局数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持。
请注意, data
如果启用重试 (默认) ,则传递的 必须可重播。 换句话说, Flux
每次订阅 时都必须生成相同的数据。
内容识别支持自动语言识别和多语言文档,因此仅当你想要强制将文档作为特定语言在 中 RecognizeContentOptions进行处理时,才提供语言代码。
代码示例
File form = new File("{local/file_path/fileName.jpg}");
// Utility method to convert input stream to Byte buffer
Flux<ByteBuffer> buffer = toFluxByteBuffer(new ByteArrayInputStream(Files.readAllBytes(form.toPath())));
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeContent(buffer, form.length(),
new RecognizeContentOptions()
.setContentType(FormContentType.IMAGE_JPEG)
.setPollInterval(Duration.ofSeconds(5)))
.flatMap(AsyncPollResponse::getFinalResult)
.flatMap(Flux::fromIterable)
.subscribe(formPage -> {
System.out.printf("Page Angle: %s%n", formPage.getTextAngle());
System.out.printf("Page Dimension unit: %s%n", formPage.getUnit());
// Table information
System.out.println("Recognized Tables: ");
formPage.getTables().forEach(formTable -> formTable.getCells().forEach(recognizedTableCell ->
System.out.printf("%s ", recognizedTableCell.getText())));
});
Parameters:
Returns:
beginRecognizeContentFromUrl
public PollerFlux<>
使用光学字符识别 (OCR) 识别文档中的内容/布局数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持。
代码示例
String formUrl = "{formUrl}";
formRecognizerAsyncClient.beginRecognizeContentFromUrl(formUrl)
// if training polling operation completed, retrieve the final result.
.flatMap(AsyncPollResponse::getFinalResult)
.flatMap(Flux::fromIterable)
.subscribe(formPage -> {
System.out.printf("Page Angle: %s%n", formPage.getTextAngle());
System.out.printf("Page Dimension unit: %s%n", formPage.getUnit());
// Table information
System.out.println("Recognized Tables: ");
formPage.getTables().forEach(formTable ->
formTable.getCells().forEach(recognizedTableCell ->
System.out.printf("%s ", recognizedTableCell.getText())));
});
Parameters:
Returns:
beginRecognizeContentFromUrl
public PollerFlux<>
使用光学字符识别 (OCR) 和自定义训练模型识别文档中的布局数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持。
内容识别支持自动语言识别和多语言文档,因此仅当你想要强制将文档作为特定语言在 中 RecognizeContentOptions进行处理时,才提供语言代码。
代码示例
String formUrl = "{formUrl}";
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeContentFromUrl(formUrl,
new RecognizeContentOptions().setPollInterval(Duration.ofSeconds(5)))
.flatMap(AsyncPollResponse::getFinalResult)
.flatMap(Flux::fromIterable)
.subscribe(formPage -> {
System.out.printf("Page Angle: %s%n", formPage.getTextAngle());
System.out.printf("Page Dimension unit: %s%n", formPage.getUnit());
// Table information
System.out.println("Recognized Tables: ");
formPage.getTables().forEach(formTable ->
formTable.getCells().forEach(recognizedTableCell ->
System.out.printf("%s ", recognizedTableCell.getText())));
});
Parameters:
Returns:
beginRecognizeCustomForms
public PollerFlux<>
使用光学字符识别 (OCR) 和带或不带标签的自定义训练模型来识别文档中的表单数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持。
请注意, data
如果启用重试 (默认) ,则传递的 必须可重播。 换句话说, Flux
每次订阅 时都必须生成相同的数据。
代码示例
File form = new File("{local/file_path/fileName.jpg}");
String modelId = "{custom_trained_model_id}";
// Utility method to convert input stream to Byte buffer
Flux<ByteBuffer> buffer = toFluxByteBuffer(new ByteArrayInputStream(Files.readAllBytes(form.toPath())));
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeCustomForms(modelId, buffer, form.length())
// if training polling operation completed, retrieve the final result.
.flatMap(AsyncPollResponse::getFinalResult)
.flatMap(Flux::fromIterable)
.subscribe(recognizedForm -> recognizedForm.getFields()
.forEach((fieldText, formField) -> {
System.out.printf("Field text: %s%n", fieldText);
System.out.printf("Field value data text: %s%n", formField.getValueData().getText());
System.out.printf("Confidence score: %.2f%n", formField.getConfidence());
}));
Parameters:
Returns:
beginRecognizeCustomForms
public PollerFlux<>
使用光学字符识别 (OCR) 和带或不带标签的自定义训练模型来识别文档中的表单数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持。
请注意, data
如果启用重试 (默认) ,则传递的 必须可重播。 换句话说, Flux
每次订阅 时都必须生成相同的数据。
代码示例
File form = new File("{local/file_path/fileName.jpg}");
String modelId = "{custom_trained_model_id}";
boolean includeFieldElements = true;
// Utility method to convert input stream to Byte buffer
Flux<ByteBuffer> buffer = toFluxByteBuffer(new ByteArrayInputStream(Files.readAllBytes(form.toPath())));
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeCustomForms(modelId, buffer, form.length(),
new RecognizeCustomFormsOptions()
.setContentType(FormContentType.IMAGE_JPEG)
.setFieldElementsIncluded(includeFieldElements)
.setPollInterval(Duration.ofSeconds(5)))
// if training polling operation completed, retrieve the final result.
.flatMap(AsyncPollResponse::getFinalResult)
.flatMap(Flux::fromIterable)
.subscribe(recognizedForm -> recognizedForm.getFields()
.forEach((fieldName, formField) -> {
System.out.printf("Field text: %s%n", fieldName);
System.out.printf("Field value data text: %s%n", formField.getValueData().getText());
System.out.printf("Confidence score: %.2f%n", formField.getConfidence());
}));
Parameters:
Returns:
beginRecognizeCustomFormsFromUrl
public PollerFlux<>
使用光学字符识别 (OCR) 和带或不带标签的自定义训练模型来识别文档中的表单数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持。
代码示例
String formUrl = "{form_url}";
String modelId = "{custom_trained_model_id}";
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeCustomFormsFromUrl(modelId, formUrl)
// if training polling operation completed, retrieve the final result.
.flatMap(AsyncPollResponse::getFinalResult)
.flatMap(Flux::fromIterable)
.subscribe(recognizedForm -> recognizedForm.getFields()
.forEach((fieldText, formField) -> {
System.out.printf("Field text: %s%n", fieldText);
System.out.printf("Field value data text: %s%n", formField.getValueData().getText());
System.out.printf("Confidence score: %.2f%n", formField.getConfidence());
}));
Parameters:
Returns:
beginRecognizeCustomFormsFromUrl
public PollerFlux<>
使用光学字符识别 (OCR) 和自定义训练模型识别文档中的表单数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持。
代码示例
String formUrl = "{formUrl}";
String modelId = "{model_id}";
boolean includeFieldElements = true;
formRecognizerAsyncClient.beginRecognizeCustomFormsFromUrl(modelId, formUrl,
new RecognizeCustomFormsOptions()
.setFieldElementsIncluded(includeFieldElements)
.setPollInterval(Duration.ofSeconds(10)))
// if training polling operation completed, retrieve the final result.
.flatMap(AsyncPollResponse::getFinalResult)
.flatMap(Flux::fromIterable)
.subscribe(recognizedForm -> recognizedForm.getFields()
.forEach((fieldText, formField) -> {
System.out.printf("Field text: %s%n", fieldText);
System.out.printf("Field value data text: %s%n", formField.getValueData().getText());
System.out.printf("Confidence score: %.2f%n", formField.getConfidence());
}));
Parameters:
Returns:
beginRecognizeIdentityDocuments
public PollerFlux<>
使用光学字符识别 (OCR) 和基于身份证明文件模型训练的预生成模型来分析标识文档,以从护照和美国驾驶执照中提取关键信息。 有关在标识文档上找到的字段,请参阅 此处 。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持。
请注意, identityDocument
如果启用重试 (默认) ,则传递的 必须可重播。 换句话说, Flux
每次订阅 时都必须生成相同的数据。
代码示例
File license = new File("local/file_path/license.jpg");
Flux<ByteBuffer> buffer =
toFluxByteBuffer(new ByteArrayInputStream(Files.readAllBytes(license.toPath())));
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeIdentityDocuments(buffer, license.length())
.flatMap(AsyncPollResponse::getFinalResult)
.subscribe(recognizedIDDocumentResult -> {
for (int i = 0; i < recognizedIDDocumentResult.size(); i++) {
RecognizedForm recognizedForm = recognizedIDDocumentResult.get(i);
Map<String, FormField> recognizedFields = recognizedForm.getFields();
System.out.printf("----------- Recognized license info for page %d -----------%n", i);
FormField firstNameField = recognizedFields.get("FirstName");
if (firstNameField != null) {
if (FieldValueType.STRING == firstNameField.getValue().getValueType()) {
String firstName = firstNameField.getValue().asString();
System.out.printf("First Name: %s, confidence: %.2f%n",
firstName, firstNameField.getConfidence());
}
}
FormField lastNameField = recognizedFields.get("LastName");
if (lastNameField != null) {
if (FieldValueType.STRING == lastNameField.getValue().getValueType()) {
String lastName = lastNameField.getValue().asString();
System.out.printf("Last name: %s, confidence: %.2f%n",
lastName, lastNameField.getConfidence());
}
}
FormField countryRegionFormField = recognizedFields.get("CountryRegion");
if (countryRegionFormField != null) {
if (FieldValueType.STRING == countryRegionFormField.getValue().getValueType()) {
String countryRegion = countryRegionFormField.getValue().asCountryRegion();
System.out.printf("Country or region: %s, confidence: %.2f%n",
countryRegion, countryRegionFormField.getConfidence());
}
}
FormField dateOfExpirationField = recognizedFields.get("DateOfExpiration");
if (dateOfExpirationField != null) {
if (FieldValueType.DATE == dateOfExpirationField.getValue().getValueType()) {
LocalDate expirationDate = dateOfExpirationField.getValue().asDate();
System.out.printf("Document date of expiration: %s, confidence: %.2f%n",
expirationDate, dateOfExpirationField.getConfidence());
}
}
FormField documentNumberField = recognizedFields.get("DocumentNumber");
if (documentNumberField != null) {
if (FieldValueType.STRING == documentNumberField.getValue().getValueType()) {
String documentNumber = documentNumberField.getValue().asString();
System.out.printf("Document number: %s, confidence: %.2f%n",
documentNumber, documentNumberField.getConfidence());
}
}
}
});
Parameters:
Returns:
beginRecognizeIdentityDocuments
public PollerFlux<>
使用光学字符识别 (OCR) 和基于身份证明文件模型训练的预生成模型来分析标识文档,以从护照和美国驾驶执照中提取关键信息。 有关在标识文档上找到的字段,请参阅 此处 。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
请注意, identityDocument
如果启用了重试 (默认) ,则传递的 必须可重播。 换句话说, Flux
必须在每次订阅时生成相同的数据。
代码示例
File licenseDocument = new File("local/file_path/license.jpg");
boolean includeFieldElements = true;
// Utility method to convert input stream to Byte buffer
Flux<ByteBuffer> buffer =
toFluxByteBuffer(new ByteArrayInputStream(Files.readAllBytes(licenseDocument.toPath())));
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeIdentityDocuments(buffer,
licenseDocument.length(),
new RecognizeIdentityDocumentOptions()
.setContentType(FormContentType.IMAGE_JPEG)
.setFieldElementsIncluded(includeFieldElements))
.setPollInterval(Duration.ofSeconds(5))
.flatMap(AsyncPollResponse::getFinalResult)
.subscribe(recognizedIDDocumentResult -> {
for (int i = 0; i < recognizedIDDocumentResult.size(); i++) {
RecognizedForm recognizedForm = recognizedIDDocumentResult.get(i);
Map<String, FormField> recognizedFields = recognizedForm.getFields();
System.out.printf("----------- Recognized license info for page %d -----------%n", i);
FormField firstNameField = recognizedFields.get("FirstName");
if (firstNameField != null) {
if (FieldValueType.STRING == firstNameField.getValue().getValueType()) {
String firstName = firstNameField.getValue().asString();
System.out.printf("First Name: %s, confidence: %.2f%n",
firstName, firstNameField.getConfidence());
}
}
FormField lastNameField = recognizedFields.get("LastName");
if (lastNameField != null) {
if (FieldValueType.STRING == lastNameField.getValue().getValueType()) {
String lastName = lastNameField.getValue().asString();
System.out.printf("Last name: %s, confidence: %.2f%n",
lastName, lastNameField.getConfidence());
}
}
FormField countryRegionFormField = recognizedFields.get("CountryRegion");
if (countryRegionFormField != null) {
if (FieldValueType.STRING == countryRegionFormField.getValue().getValueType()) {
String countryRegion = countryRegionFormField.getValue().asCountryRegion();
System.out.printf("Country or region: %s, confidence: %.2f%n",
countryRegion, countryRegionFormField.getConfidence());
}
}
FormField dateOfExpirationField = recognizedFields.get("DateOfExpiration");
if (dateOfExpirationField != null) {
if (FieldValueType.DATE == dateOfExpirationField.getValue().getValueType()) {
LocalDate expirationDate = dateOfExpirationField.getValue().asDate();
System.out.printf("Document date of expiration: %s, confidence: %.2f%n",
expirationDate, dateOfExpirationField.getConfidence());
}
}
FormField documentNumberField = recognizedFields.get("DocumentNumber");
if (documentNumberField != null) {
if (FieldValueType.STRING == documentNumberField.getValue().getValueType()) {
String documentNumber = documentNumberField.getValue().asString();
System.out.printf("Document number: %s, confidence: %.2f%n",
documentNumber, documentNumberField.getConfidence());
}
}
}
});
Parameters:
Returns:
beginRecognizeIdentityDocumentsFromUrl
public PollerFlux<>
使用光学字符识别 (OCR) 和基于身份证明文件模型训练的预生成模型从护照和美国驾驶执照中提取关键信息,分析身份文档。 有关在标识文档上找到的字段,请参阅 此处 。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
代码示例
String idDocumentUrl = "idDocumentUrl";
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeIdentityDocumentsFromUrl(idDocumentUrl)
.flatMap(AsyncPollResponse::getFinalResult)
.subscribe(recognizedIDDocumentResult -> {
for (int i = 0; i < recognizedIDDocumentResult.size(); i++) {
RecognizedForm recognizedForm = recognizedIDDocumentResult.get(i);
Map<String, FormField> recognizedFields = recognizedForm.getFields();
System.out.printf("----------- Recognized license info for page %d -----------%n", i);
FormField firstNameField = recognizedFields.get("FirstName");
if (firstNameField != null) {
if (FieldValueType.STRING == firstNameField.getValue().getValueType()) {
String firstName = firstNameField.getValue().asString();
System.out.printf("First Name: %s, confidence: %.2f%n",
firstName, firstNameField.getConfidence());
}
}
FormField lastNameField = recognizedFields.get("LastName");
if (lastNameField != null) {
if (FieldValueType.STRING == lastNameField.getValue().getValueType()) {
String lastName = lastNameField.getValue().asString();
System.out.printf("Last name: %s, confidence: %.2f%n",
lastName, lastNameField.getConfidence());
}
}
FormField countryRegionFormField = recognizedFields.get("CountryRegion");
if (countryRegionFormField != null) {
if (FieldValueType.STRING == countryRegionFormField.getValue().getValueType()) {
String countryRegion = countryRegionFormField.getValue().asCountryRegion();
System.out.printf("Country or region: %s, confidence: %.2f%n",
countryRegion, countryRegionFormField.getConfidence());
}
}
FormField dateOfExpirationField = recognizedFields.get("DateOfExpiration");
if (dateOfExpirationField != null) {
if (FieldValueType.DATE == dateOfExpirationField.getValue().getValueType()) {
LocalDate expirationDate = dateOfExpirationField.getValue().asDate();
System.out.printf("Document date of expiration: %s, confidence: %.2f%n",
expirationDate, dateOfExpirationField.getConfidence());
}
}
FormField documentNumberField = recognizedFields.get("DocumentNumber");
if (documentNumberField != null) {
if (FieldValueType.STRING == documentNumberField.getValue().getValueType()) {
String documentNumber = documentNumberField.getValue().asString();
System.out.printf("Document number: %s, confidence: %.2f%n",
documentNumber, documentNumberField.getConfidence());
}
}
}
});
Parameters:
Returns:
beginRecognizeIdentityDocumentsFromUrl
public PollerFlux<>
使用光学字符识别 (OCR) 和基于身份证明文件模型训练的预生成模型从护照和美国驾驶执照中提取关键信息,分析身份文档。 有关在标识文档上找到的字段,请参阅 此处 。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
代码示例
String licenseDocumentUrl = "licenseDocumentUrl";
boolean includeFieldElements = true;
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeIdentityDocumentsFromUrl(licenseDocumentUrl,
new RecognizeIdentityDocumentOptions()
.setFieldElementsIncluded(includeFieldElements))
.setPollInterval(Duration.ofSeconds(5))
.flatMap(AsyncPollResponse::getFinalResult)
.subscribe(recognizedIDDocumentResult -> {
for (int i = 0; i < recognizedIDDocumentResult.size(); i++) {
RecognizedForm recognizedForm = recognizedIDDocumentResult.get(i);
Map<String, FormField> recognizedFields = recognizedForm.getFields();
System.out.printf("----------- Recognized license info for page %d -----------%n", i);
FormField firstNameField = recognizedFields.get("FirstName");
if (firstNameField != null) {
if (FieldValueType.STRING == firstNameField.getValue().getValueType()) {
String firstName = firstNameField.getValue().asString();
System.out.printf("First Name: %s, confidence: %.2f%n",
firstName, firstNameField.getConfidence());
}
}
FormField lastNameField = recognizedFields.get("LastName");
if (lastNameField != null) {
if (FieldValueType.STRING == lastNameField.getValue().getValueType()) {
String lastName = lastNameField.getValue().asString();
System.out.printf("Last name: %s, confidence: %.2f%n",
lastName, lastNameField.getConfidence());
}
}
FormField countryRegionFormField = recognizedFields.get("CountryRegion");
if (countryRegionFormField != null) {
if (FieldValueType.STRING == countryRegionFormField.getValue().getValueType()) {
String countryRegion = countryRegionFormField.getValue().asCountryRegion();
System.out.printf("Country or region: %s, confidence: %.2f%n",
countryRegion, countryRegionFormField.getConfidence());
}
}
FormField dateOfExpirationField = recognizedFields.get("DateOfExpiration");
if (dateOfExpirationField != null) {
if (FieldValueType.DATE == dateOfExpirationField.getValue().getValueType()) {
LocalDate expirationDate = dateOfExpirationField.getValue().asDate();
System.out.printf("Document date of expiration: %s, confidence: %.2f%n",
expirationDate, dateOfExpirationField.getConfidence());
}
}
FormField documentNumberField = recognizedFields.get("DocumentNumber");
if (documentNumberField != null) {
if (FieldValueType.STRING == documentNumberField.getValue().getValueType()) {
String documentNumber = documentNumberField.getValue().asString();
System.out.printf("Document number: %s, confidence: %.2f%n",
documentNumber, documentNumberField.getConfidence());
}
}
}
});
Parameters:
Returns:
beginRecognizeInvoices
public PollerFlux<>
使用光学字符识别 (OCR) 和预生成发票训练模型来识别发票数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
有关发票上的字段,请参阅 此处 。 请注意, invoice
如果启用了重试 (默认) ,则传递的 必须可重播。 换句话说, Flux
必须在每次订阅时生成相同的数据。
代码示例
File invoice = new File("local/file_path/invoice.jpg");
Flux<ByteBuffer> buffer =
toFluxByteBuffer(new ByteArrayInputStream(Files.readAllBytes(invoice.toPath())));
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeInvoices(buffer, invoice.length())
.flatMap(AsyncPollResponse::getFinalResult)
.subscribe(recognizedInvoices -> {
for (int i = 0; i < recognizedInvoices.size(); i++) {
RecognizedForm recognizedForm = recognizedInvoices.get(i);
Map<String, FormField> recognizedFields = recognizedForm.getFields();
FormField customAddrFormField = recognizedFields.get("CustomerAddress");
if (customAddrFormField != null) {
if (FieldValueType.STRING == customAddrFormField.getValue().getValueType()) {
System.out.printf("Customer Address: %s%n", customAddrFormField.getValue().asString());
}
}
FormField invoiceDateFormField = recognizedFields.get("InvoiceDate");
if (invoiceDateFormField != null) {
if (FieldValueType.DATE == invoiceDateFormField.getValue().getValueType()) {
LocalDate invoiceDate = invoiceDateFormField.getValue().asDate();
System.out.printf("Invoice Date: %s, confidence: %.2f%n",
invoiceDate, invoiceDateFormField.getConfidence());
}
}
}
});
Parameters:
Returns:
beginRecognizeInvoices
public PollerFlux<>
使用光学字符识别 (OCR) 和预生成发票训练模型从文档识别发票数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
有关发票上的字段,请参阅 此处 。 请注意, invoice
如果启用了重试 (默认) ,则传递的 必须可重播。 换句话说, Flux
必须在每次订阅时生成相同的数据。
代码示例
File invoice = new File("local/file_path/invoice.jpg");
boolean includeFieldElements = true;
// Utility method to convert input stream to Byte buffer
Flux<ByteBuffer> buffer =
toFluxByteBuffer(new ByteArrayInputStream(Files.readAllBytes(invoice.toPath())));
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeInvoices(buffer,
invoice.length(),
new RecognizeInvoicesOptions()
.setContentType(FormContentType.IMAGE_JPEG)
.setFieldElementsIncluded(includeFieldElements))
.setPollInterval(Duration.ofSeconds(5))
.flatMap(AsyncPollResponse::getFinalResult)
.subscribe(recognizedInvoices -> {
for (int i = 0; i < recognizedInvoices.size(); i++) {
RecognizedForm recognizedForm = recognizedInvoices.get(i);
Map<String, FormField> recognizedFields = recognizedForm.getFields();
FormField customAddrFormField = recognizedFields.get("CustomerAddress");
if (customAddrFormField != null) {
if (FieldValueType.STRING == customAddrFormField.getValue().getValueType()) {
System.out.printf("Customer Address: %s%n", customAddrFormField.getValue().asString());
}
}
FormField invoiceDateFormField = recognizedFields.get("InvoiceDate");
if (invoiceDateFormField != null) {
if (FieldValueType.DATE == invoiceDateFormField.getValue().getValueType()) {
LocalDate invoiceDate = invoiceDateFormField.getValue().asDate();
System.out.printf("Invoice Date: %s, confidence: %.2f%n",
invoiceDate, invoiceDateFormField.getConfidence());
}
}
}
});
Parameters:
Returns:
beginRecognizeInvoicesFromUrl
public PollerFlux<>
使用光学字符识别 (OCR) 和预生成发票训练模型来识别发票数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
有关发票上的字段,请参阅 此处 。
代码示例
String invoiceUrl = "invoice_url";
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeInvoicesFromUrl(invoiceUrl)
.flatMap(AsyncPollResponse::getFinalResult)
.subscribe(recognizedInvoices -> {
for (int i = 0; i < recognizedInvoices.size(); i++) {
RecognizedForm recognizedForm = recognizedInvoices.get(i);
Map<String, FormField> recognizedFields = recognizedForm.getFields();
FormField customAddrFormField = recognizedFields.get("CustomerAddress");
if (customAddrFormField != null) {
if (FieldValueType.STRING == customAddrFormField.getValue().getValueType()) {
System.out.printf("Customer Address: %s%n", customAddrFormField.getValue().asString());
}
}
FormField invoiceDateFormField = recognizedFields.get("InvoiceDate");
if (invoiceDateFormField != null) {
if (FieldValueType.DATE == invoiceDateFormField.getValue().getValueType()) {
LocalDate invoiceDate = invoiceDateFormField.getValue().asDate();
System.out.printf("Invoice Date: %s, confidence: %.2f%n",
invoiceDate, invoiceDateFormField.getConfidence());
}
}
}
});
Parameters:
Returns:
beginRecognizeInvoicesFromUrl
public PollerFlux<>
使用光学字符识别 (OCR) 和预生成发票训练模型来识别发票数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
代码示例
String invoiceUrl = "invoice_url";
boolean includeFieldElements = true;
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeInvoicesFromUrl(invoiceUrl,
new RecognizeInvoicesOptions()
.setFieldElementsIncluded(includeFieldElements))
.setPollInterval(Duration.ofSeconds(5))
.flatMap(AsyncPollResponse::getFinalResult)
.subscribe(recognizedInvoices -> {
for (int i = 0; i < recognizedInvoices.size(); i++) {
RecognizedForm recognizedForm = recognizedInvoices.get(i);
Map<String, FormField> recognizedFields = recognizedForm.getFields();
FormField customAddrFormField = recognizedFields.get("CustomerAddress");
if (customAddrFormField != null) {
if (FieldValueType.STRING == customAddrFormField.getValue().getValueType()) {
System.out.printf("Customer Address: %s%n", customAddrFormField.getValue().asString());
}
}
FormField invoiceDateFormField = recognizedFields.get("InvoiceDate");
if (invoiceDateFormField != null) {
if (FieldValueType.DATE == invoiceDateFormField.getValue().getValueType()) {
LocalDate invoiceDate = invoiceDateFormField.getValue().asDate();
System.out.printf("Invoice Date: %s, confidence: %.2f%n",
invoiceDate, invoiceDateFormField.getConfidence());
}
}
}
});
Parameters:
Returns:
beginRecognizeReceipts
public PollerFlux<>
使用光学字符识别 (OCR) 和预生成的收据训练模型来识别收据数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
有关在收据上找到的字段,请参阅 此处 。 请注意, receipt
如果启用了重试 (默认) ,则传递的 必须可重播。 换句话说, Flux
必须在每次订阅时生成相同的数据。
代码示例
String formUrl = "{form_url}";
String modelId = "{custom_trained_model_id}";
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeCustomFormsFromUrl(modelId, formUrl)
// if training polling operation completed, retrieve the final result.
.flatMap(AsyncPollResponse::getFinalResult)
.flatMap(Flux::fromIterable)
.subscribe(recognizedForm -> recognizedForm.getFields()
.forEach((fieldText, formField) -> {
System.out.printf("Field text: %s%n", fieldText);
System.out.printf("Field value data text: %s%n", formField.getValueData().getText());
System.out.printf("Confidence score: %.2f%n", formField.getConfidence());
}));
Parameters:
Returns:
beginRecognizeReceipts
public PollerFlux<>
使用光学字符识别 (OCR) 和预生成收据训练模型识别文档中的回执数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
有关在收据上找到的字段,请参阅 此处 。 请注意, receipt
如果启用了重试 (默认) ,则传递的 必须可重播。 换句话说, Flux
必须在每次订阅时生成相同的数据。
代码示例
String formUrl = "{form_url}";
String modelId = "{custom_trained_model_id}";
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeCustomFormsFromUrl(modelId, formUrl)
// if training polling operation completed, retrieve the final result.
.flatMap(AsyncPollResponse::getFinalResult)
.flatMap(Flux::fromIterable)
.subscribe(recognizedForm -> recognizedForm.getFields()
.forEach((fieldText, formField) -> {
System.out.printf("Field text: %s%n", fieldText);
System.out.printf("Field value data text: %s%n", formField.getValueData().getText());
System.out.printf("Confidence score: %.2f%n", formField.getConfidence());
}));
Parameters:
Returns:
beginRecognizeReceiptsFromUrl
public PollerFlux<>
使用光学字符识别 (OCR) 和预生成的收据训练模型来识别收据数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
有关在收据上找到的字段,请参阅 此处 。
代码示例
String formUrl = "{form_url}";
String modelId = "{custom_trained_model_id}";
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeCustomFormsFromUrl(modelId, formUrl)
// if training polling operation completed, retrieve the final result.
.flatMap(AsyncPollResponse::getFinalResult)
.flatMap(Flux::fromIterable)
.subscribe(recognizedForm -> recognizedForm.getFields()
.forEach((fieldText, formField) -> {
System.out.printf("Field text: %s%n", fieldText);
System.out.printf("Field value data text: %s%n", formField.getValueData().getText());
System.out.printf("Confidence score: %.2f%n", formField.getConfidence());
}));
Parameters:
Returns:
beginRecognizeReceiptsFromUrl
public PollerFlux<>
使用光学字符识别 (OCR) 和预生成的收据训练模型来识别收据数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
代码示例
String formUrl = "{form_url}";
String modelId = "{custom_trained_model_id}";
// if training polling operation completed, retrieve the final result.
formRecognizerAsyncClient.beginRecognizeCustomFormsFromUrl(modelId, formUrl)
// if training polling operation completed, retrieve the final result.
.flatMap(AsyncPollResponse::getFinalResult)
.flatMap(Flux::fromIterable)
.subscribe(recognizedForm -> recognizedForm.getFields()
.forEach((fieldText, formField) -> {
System.out.printf("Field text: %s%n", fieldText);
System.out.printf("Field value data text: %s%n", formField.getValueData().getText());
System.out.printf("Confidence score: %.2f%n", formField.getConfidence());
}));
Parameters:
Returns:
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈