你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
FormRecognizerClient 类
- java.
lang. Object - com.
azure. ai. formrecognizer. FormRecognizerClient
- com.
public final class FormRecognizerClient
此类提供一个同步客户端,用于连接到 Azure 认知服务表单识别器。
此客户端提供同步方法来执行:
- 自定义表单分析:从特定于不同业务数据和用例的表单和文档中提取和分析数据。 通过将其 modelId 传递到 方法, beginRecognizeCustomForms 使用自定义训练的模型。
- 预生成模型分析:使用 支持的预生成模型 分析收据、名片、发票和其他文档 使用 beginRecognizeReceipts 方法识别收据信息。
- 布局分析:从窗体和文档中提取和分析文本、选择标记、表格和边界框坐标。 使用 beginRecognizeContent(InputStream form, long length) tpo 方法执行布局分析。
- 轮询和回调:它包括轮询服务以检查分析操作的状态或注册回调以在分析完成时接收通知的机制。
若要使用 API 版本 2022-08-31 及更新,请参阅 迁移指南 。
服务客户端是开发人员使用 Azure 表单识别器的交互点。 FormRecognizerClient 是同步服务客户端, FormRecognizerAsyncClient 是异步服务客户端。 本文档中显示的示例使用名为 DefaultAzureCredential 的凭据对象进行身份验证,该对象适用于大多数方案,包括本地开发和生产环境。 此外,建议在生产环境中使用 托管标识 进行身份验证。 可以在 Azure 标识文档中找到有关不同身份验证方式及其相应凭据类型的详细信息。
示例:使用 DefaultAzureCredential 构造 FormRecognizerClient
下面的代码示例演示如何使用“DefaultAzureCredentialBuilder”创建 FormRecognizerClient它来配置它。
FormRecognizerClient formRecognizerClient = new FormRecognizerClientBuilder()
.endpoint("{endpoint}")
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
此外,请参阅下面的代码示例以用于 AzureKeyCredential 创建客户端。
FormRecognizerClient formRecognizerClient = new FormRecognizerClientBuilder()
.credential(new AzureKeyCredential("{key}"))
.endpoint("{endpoint}")
.buildClient();
方法摘要
方法继承自 java.lang.Object
方法详细信息
beginRecognizeBusinessCards
public SyncPoller
使用光学字符识别 (OCR) 和预生成的训练业务卡模型,从提供的文档数据中识别业务卡数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持
有关在企业卡中找到的字段,请参阅此处。
代码示例
File businessCard = new File("{local/file_path/fileName.jpg}");
byte[] fileContent = Files.readAllBytes(businessCard.toPath());
try (InputStream targetStream = new ByteArrayInputStream(fileContent)) {
formRecognizerClient.beginRecognizeBusinessCards(targetStream, businessCard.length()).getFinalResult()
.forEach(recognizedBusinessCard -> {
Map<String, FormField> recognizedFields = recognizedBusinessCard.getFields();
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 SyncPoller
使用光学字符识别 (OCR) 和预生成的训练业务卡模型,从提供的文档数据中识别业务卡数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持
有关在企业卡中找到的字段,请参阅此处。
代码示例
File businessCard = new File("{local/file_path/fileName.jpg}");
boolean includeFieldElements = true;
byte[] fileContent = Files.readAllBytes(businessCard.toPath());
try (InputStream targetStream = new ByteArrayInputStream(fileContent)) {
for (RecognizedForm recognizedForm : formRecognizerClient.beginRecognizeBusinessCards(targetStream,
businessCard.length(),
new RecognizeBusinessCardsOptions()
.setContentType(FormContentType.IMAGE_JPEG)
.setFieldElementsIncluded(includeFieldElements),
Context.NONE).setPollInterval(Duration.ofSeconds(5))
.getFinalResult()) {
Map<String, FormField> recognizedFields = recognizedForm.getFields();
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 SyncPoller
使用光学字符识别 (OCR) 和预先生成的业务卡训练模型,从文档中识别业务卡数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持
有关在企业卡中找到的字段,请参阅此处。
代码示例
String businessCardUrl = "{business_card_url}";
formRecognizerClient.beginRecognizeBusinessCardsFromUrl(businessCardUrl)
.getFinalResult()
.forEach(recognizedBusinessCard -> {
Map<String, FormField> recognizedFields = recognizedBusinessCard.getFields();
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 SyncPoller
使用光学字符识别 (OCR) 和预先生成的业务卡训练模型,识别文档中的业务卡数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持
有关在企业卡中找到的字段,请参阅此处。
代码示例
String businessCardUrl = "{business_card_url}";
formRecognizerClient.beginRecognizeBusinessCardsFromUrl(businessCardUrl,
new RecognizeBusinessCardsOptions()
.setFieldElementsIncluded(true), Context.NONE)
.setPollInterval(Duration.ofSeconds(5)).getFinalResult()
.forEach(recognizedBusinessCard -> {
Map<String, FormField> recognizedFields = recognizedBusinessCard.getFields();
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 SyncPoller
使用光学字符识别 (OCR) 和自定义训练模型识别布局数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
代码示例
File form = new File("{local/file_path/fileName.pdf}");
byte[] fileContent = Files.readAllBytes(form.toPath());
try (InputStream targetStream = new ByteArrayInputStream(fileContent)) {
formRecognizerClient.beginRecognizeContent(targetStream, form.length())
.getFinalResult()
.forEach(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()
.stream()
.flatMap(formTable -> formTable.getCells().stream())
.forEach(recognizedTableCell -> System.out.printf("%s ", recognizedTableCell.getText()));
});
}
Parameters:
Returns:
beginRecognizeContent
public SyncPoller
使用光学字符识别 (OCR) 从提供的文档数据中识别内容/布局数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持
内容识别支持自动语言标识和多语言文档,因此,仅当你想要强制在 中 RecognizeContentOptions将文档作为该特定语言进行处理时,才提供语言代码。
代码示例
File form = new File("{file_source_url}");
byte[] fileContent = Files.readAllBytes(form.toPath());
try (InputStream targetStream = new ByteArrayInputStream(fileContent)) {
for (FormPage formPage : formRecognizerClient.beginRecognizeContent(targetStream, form.length(),
new RecognizeContentOptions()
.setPollInterval(Duration.ofSeconds(5)), Context.NONE)
.getFinalResult()) {
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()
.stream()
.flatMap(formTable -> formTable.getCells().stream())
.forEach(recognizedTableCell -> System.out.printf("%s ", recognizedTableCell.getText()));
}
}
Parameters:
Returns:
beginRecognizeContentFromUrl
public SyncPoller
使用光学字符识别 (OCR) 识别文档中的内容/布局数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
代码示例
String formUrl = "{form_url}";
formRecognizerClient.beginRecognizeContentFromUrl(formUrl)
.getFinalResult()
.forEach(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()
.stream()
.flatMap(formTable -> formTable.getCells().stream())
.forEach(recognizedTableCell -> System.out.printf("%s ", recognizedTableCell.getText()));
});
Parameters:
Returns:
beginRecognizeContentFromUrl
public SyncPoller
使用光学字符识别 (OCR) 识别内容/布局数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
内容识别支持自动语言标识和多语言文档,因此,仅当你想要强制在 中 RecognizeContentOptions将文档作为该特定语言进行处理时,才提供语言代码。
代码示例
String formPath = "{file_source_url}";
formRecognizerClient.beginRecognizeContentFromUrl(formPath,
new RecognizeContentOptions()
.setPollInterval(Duration.ofSeconds(5)), Context.NONE)
.getFinalResult()
.forEach(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()
.stream()
.flatMap(formTable -> formTable.getCells().stream())
.forEach(recognizedTableCell -> System.out.printf("%s ", recognizedTableCell.getText()));
});
Parameters:
Returns:
beginRecognizeCustomForms
public SyncPoller
使用光学字符识别 (OCR) 和带或不带标签的自定义训练模型,识别文档中的表单数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
代码示例
File form = new File("{local/file_path/fileName.jpg}");
String modelId = "{custom_trained_model_id}";
byte[] fileContent = Files.readAllBytes(form.toPath());
try (InputStream targetStream = new ByteArrayInputStream(fileContent)) {
formRecognizerClient.beginRecognizeCustomForms(modelId, targetStream, form.length())
.getFinalResult()
.stream()
.map(RecognizedForm::getFields)
.forEach(formFieldMap -> formFieldMap.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 SyncPoller
使用光学字符识别 (OCR) 和自定义训练模型识别文档中的表单数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
代码示例
File form = new File("{local/file_path/fileName.jpg}");
String modelId = "{custom_trained_model_id}";
boolean includeFieldElements = true;
byte[] fileContent = Files.readAllBytes(form.toPath());
try (InputStream targetStream = new ByteArrayInputStream(fileContent)) {
formRecognizerClient.beginRecognizeCustomForms(modelId, targetStream, form.length(),
new RecognizeCustomFormsOptions()
.setContentType(FormContentType.IMAGE_JPEG)
.setFieldElementsIncluded(includeFieldElements)
.setPollInterval(Duration.ofSeconds(10)), Context.NONE)
.getFinalResult()
.stream()
.map(RecognizedForm::getFields)
.forEach(formFieldMap -> formFieldMap.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 SyncPoller
使用光学字符识别 (OCR) 和带或不带标签的自定义训练模型,识别文档中的表单数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持
代码示例
String formUrl = "{form_url}";
String modelId = "{custom_trained_model_id}";
formRecognizerClient.beginRecognizeCustomFormsFromUrl(modelId, formUrl).getFinalResult()
.stream()
.map(RecognizedForm::getFields)
.forEach(formFieldMap -> formFieldMap.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 SyncPoller
使用光学字符识别 (OCR) 和自定义训练模型识别文档中的表单数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持
代码示例
String analyzeFilePath = "{file_source_url}";
String modelId = "{model_id}";
boolean includeFieldElements = true;
formRecognizerClient.beginRecognizeCustomFormsFromUrl(modelId, analyzeFilePath,
new RecognizeCustomFormsOptions()
.setFieldElementsIncluded(includeFieldElements)
.setPollInterval(Duration.ofSeconds(10)), Context.NONE)
.getFinalResult()
.stream()
.map(RecognizedForm::getFields)
.forEach(formFieldMap -> formFieldMap.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 SyncPoller
使用光学字符识别 (OCR) 和基于身份证明文件模型训练的预生成模型从护照和美国驾驶执照中提取关键信息,分析身份文档。 有关在标识文档上找到的字段,请参阅 此处 。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持
代码示例
File license = new File("local/file_path/license.jpg");
ByteArrayInputStream inputStream = new ByteArrayInputStream(Files.readAllBytes(license.toPath()));
// if training polling operation completed, retrieve the final result.
formRecognizerClient.beginRecognizeIdentityDocuments(inputStream, license.length())
.getFinalResult()
.stream()
.map(RecognizedForm::getFields)
.forEach(recognizedFields -> {
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 SyncPoller
使用光学字符识别 (OCR) 和基于身份证明文件模型训练的预生成模型从护照和美国驾驶执照中提取关键信息,分析身份文档。 有关在标识文档上找到的字段,请参阅 此处 。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持
代码示例
File licenseDocument = new File("local/file_path/license.jpg");
boolean includeFieldElements = true;
// Utility method to convert input stream to Byte buffer
ByteArrayInputStream inputStream = new ByteArrayInputStream(Files.readAllBytes(licenseDocument.toPath()));
// if training polling operation completed, retrieve the final result.
formRecognizerClient.beginRecognizeIdentityDocuments(inputStream,
licenseDocument.length(),
new RecognizeIdentityDocumentOptions()
.setContentType(FormContentType.IMAGE_JPEG)
.setFieldElementsIncluded(includeFieldElements),
Context.NONE)
.setPollInterval(Duration.ofSeconds(5))
.getFinalResult()
.stream()
.map(RecognizedForm::getFields)
.forEach(recognizedFields -> {
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 dateOfBirthField = recognizedFields.get("DateOfBirth");
if (dateOfBirthField != null) {
if (FieldValueType.DATE == dateOfBirthField.getValue().getValueType()) {
LocalDate dateOfBirth = dateOfBirthField.getValue().asDate();
System.out.printf("Date of Birth: %s, confidence: %.2f%n",
dateOfBirth, dateOfBirthField.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 SyncPoller
使用光学字符识别 (OCR) 和基于身份证明文件模型训练的预生成模型来分析标识文档,以从护照和美国驾驶执照中提取关键信息。 有关在标识文档上找到的字段,请参阅 此处 。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持
代码示例
String licenseDocumentUrl = "licenseDocumentUrl";
// if training polling operation completed, retrieve the final result.
formRecognizerClient.beginRecognizeIdentityDocumentsFromUrl(licenseDocumentUrl)
.getFinalResult()
.stream()
.map(RecognizedForm::getFields)
.forEach(recognizedFields -> {
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 dateOfBirthField = recognizedFields.get("DateOfBirth");
if (dateOfBirthField != null) {
if (FieldValueType.DATE == dateOfBirthField.getValue().getValueType()) {
LocalDate dateOfBirth = dateOfBirthField.getValue().asDate();
System.out.printf("Date of Birth: %s, confidence: %.2f%n",
dateOfBirth, dateOfBirthField.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 SyncPoller
使用光学字符识别 (OCR) 和基于身份证明文件模型训练的预生成模型来分析标识文档,以从护照和美国驾驶执照中提取关键信息。 有关在标识文档上找到的字段,请参阅 此处 。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持
代码示例
String licenseDocumentUrl = "licenseDocumentUrl";
boolean includeFieldElements = true;
// if training polling operation completed, retrieve the final result.
formRecognizerClient.beginRecognizeIdentityDocumentsFromUrl(licenseDocumentUrl,
new RecognizeIdentityDocumentOptions()
.setFieldElementsIncluded(includeFieldElements),
Context.NONE).setPollInterval(Duration.ofSeconds(5))
.getFinalResult()
.stream()
.map(RecognizedForm::getFields)
.forEach(recognizedFields -> {
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 SyncPoller
使用光学字符识别 (OCR) 和预生成的定型发票模型从提供的文档数据中识别数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持
有关发票上的字段,请参阅 此处 。
代码示例
File invoice = new File("local/file_path/invoice.jpg");
ByteArrayInputStream inputStream = new ByteArrayInputStream(Files.readAllBytes(invoice.toPath()));
// if training polling operation completed, retrieve the final result.
formRecognizerClient.beginRecognizeInvoices(inputStream, invoice.length())
.getFinalResult()
.stream()
.map(RecognizedForm::getFields)
.forEach(recognizedFields -> {
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 SyncPoller
使用光学字符识别 (OCR) 和预生成的定型发票模型从提供的文档数据中识别数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持
有关发票上的字段,请参阅 此处 。
代码示例
File invoice = new File("local/file_path/invoice.jpg");
boolean includeFieldElements = true;
// Utility method to convert input stream to Byte buffer
ByteArrayInputStream inputStream = new ByteArrayInputStream(Files.readAllBytes(invoice.toPath()));
// if training polling operation completed, retrieve the final result.
formRecognizerClient.beginRecognizeInvoices(inputStream,
invoice.length(),
new RecognizeInvoicesOptions()
.setContentType(FormContentType.IMAGE_JPEG)
.setFieldElementsIncluded(includeFieldElements),
Context.NONE)
.setPollInterval(Duration.ofSeconds(5))
.getFinalResult()
.stream()
.map(RecognizedForm::getFields)
.forEach(recognizedFields -> {
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 SyncPoller
使用光学字符识别 (OCR) 和预生成发票训练模型识别文档中的发票数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持
有关发票上找到的字段,请参阅 此处 。
代码示例
String invoiceUrl = "invoice_url";
// if training polling operation completed, retrieve the final result.
formRecognizerClient.beginRecognizeInvoicesFromUrl(invoiceUrl)
.getFinalResult()
.stream()
.map(RecognizedForm::getFields)
.forEach(recognizedFields -> {
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 SyncPoller
使用光学字符识别 (OCR) 和预生成的发票训练模型识别文档中的发票数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持
代码示例
String invoiceUrl = "invoice_url";
boolean includeFieldElements = true;
// if training polling operation completed, retrieve the final result.
formRecognizerClient.beginRecognizeInvoicesFromUrl(invoiceUrl,
new RecognizeInvoicesOptions()
.setFieldElementsIncluded(includeFieldElements),
Context.NONE).setPollInterval(Duration.ofSeconds(5))
.getFinalResult()
.stream()
.map(RecognizedForm::getFields)
.forEach(recognizedFields -> {
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 SyncPoller
使用光学字符识别 (OCR) 和预生成的定型收据模型识别提供的文档数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持
有关在收据上找到的字段,请参阅 此处 。
代码示例
File receipt = new File("{receipt_url}");
byte[] fileContent = Files.readAllBytes(receipt.toPath());
try (InputStream targetStream = new ByteArrayInputStream(fileContent)) {
formRecognizerClient.beginRecognizeReceipts(targetStream, receipt.length()).getFinalResult()
.forEach(recognizedReceipt -> {
Map<String, FormField> recognizedFields = recognizedReceipt.getFields();
FormField merchantNameField = recognizedFields.get("MerchantName");
if (merchantNameField != null) {
if (FieldValueType.STRING == merchantNameField.getValue().getValueType()) {
String merchantName = merchantNameField.getValue().asString();
System.out.printf("Merchant Name: %s, confidence: %.2f%n",
merchantName, merchantNameField.getConfidence());
}
}
FormField merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber");
if (merchantPhoneNumberField != null) {
if (FieldValueType.PHONE_NUMBER == merchantPhoneNumberField.getValue().getValueType()) {
String merchantAddress = merchantPhoneNumberField.getValue().asPhoneNumber();
System.out.printf("Merchant Phone number: %s, confidence: %.2f%n",
merchantAddress, merchantPhoneNumberField.getConfidence());
}
}
FormField transactionDateField = recognizedFields.get("TransactionDate");
if (transactionDateField != null) {
if (FieldValueType.DATE == transactionDateField.getValue().getValueType()) {
LocalDate transactionDate = transactionDateField.getValue().asDate();
System.out.printf("Transaction Date: %s, confidence: %.2f%n",
transactionDate, transactionDateField.getConfidence());
}
}
FormField receiptItemsField = recognizedFields.get("Items");
if (receiptItemsField != null) {
System.out.printf("Receipt Items: %n");
if (FieldValueType.LIST == receiptItemsField.getValue().getValueType()) {
List<FormField> receiptItems = receiptItemsField.getValue().asList();
receiptItems.stream()
.filter(receiptItem -> FieldValueType.MAP == receiptItem.getValue().getValueType())
.map(formField -> formField.getValue().asMap())
.forEach(formFieldMap -> formFieldMap.forEach((key, formField) -> {
if ("Quantity".equals(key)) {
if (FieldValueType.FLOAT == formField.getValue().getValueType()) {
Float quantity = formField.getValue().asFloat();
System.out.printf("Quantity: %f, confidence: %.2f%n",
quantity, formField.getConfidence());
}
}
}));
}
}
});
}
Parameters:
Returns:
beginRecognizeReceipts
public SyncPoller
使用光学字符识别 (OCR) 和预生成的定型收据模型识别提供的文档数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持
有关在收据上找到的字段,请参阅 此处 。
代码示例
File receipt = new File("{local/file_path/fileName.jpg}");
boolean includeFieldElements = true;
byte[] fileContent = Files.readAllBytes(receipt.toPath());
try (InputStream targetStream = new ByteArrayInputStream(fileContent)) {
for (RecognizedForm recognizedForm : formRecognizerClient
.beginRecognizeReceipts(targetStream, receipt.length(),
new RecognizeReceiptsOptions()
.setContentType(FormContentType.IMAGE_JPEG)
.setFieldElementsIncluded(includeFieldElements)
.setLocale(FormRecognizerLocale.EN_US)
.setPollInterval(Duration.ofSeconds(5)), Context.NONE)
.getFinalResult()) {
Map<String, FormField> recognizedFields = recognizedForm.getFields();
FormField merchantNameField = recognizedFields.get("MerchantName");
if (merchantNameField != null) {
if (FieldValueType.STRING == merchantNameField.getValue().getValueType()) {
String merchantName = merchantNameField.getValue().asString();
System.out.printf("Merchant Name: %s, confidence: %.2f%n",
merchantName, merchantNameField.getConfidence());
}
}
FormField merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber");
if (merchantPhoneNumberField != null) {
if (FieldValueType.PHONE_NUMBER == merchantPhoneNumberField.getValue().getValueType()) {
String merchantAddress = merchantPhoneNumberField.getValue().asPhoneNumber();
System.out.printf("Merchant Phone number: %s, confidence: %.2f%n",
merchantAddress, merchantPhoneNumberField.getConfidence());
}
}
FormField transactionDateField = recognizedFields.get("TransactionDate");
if (transactionDateField != null) {
if (FieldValueType.DATE == transactionDateField.getValue().getValueType()) {
LocalDate transactionDate = transactionDateField.getValue().asDate();
System.out.printf("Transaction Date: %s, confidence: %.2f%n",
transactionDate, transactionDateField.getConfidence());
}
}
FormField receiptItemsField = recognizedFields.get("Items");
if (receiptItemsField != null) {
System.out.printf("Receipt Items: %n");
if (FieldValueType.LIST == receiptItemsField.getValue().getValueType()) {
List<FormField> receiptItems = receiptItemsField.getValue().asList();
receiptItems.stream()
.filter(receiptItem -> FieldValueType.MAP == receiptItem.getValue().getValueType())
.map(formField -> formField.getValue().asMap())
.forEach(formFieldMap -> formFieldMap.forEach((key, formField) -> {
if ("Quantity".equals(key)) {
if (FieldValueType.FLOAT == formField.getValue().getValueType()) {
Float quantity = formField.getValue().asFloat();
System.out.printf("Quantity: %f, confidence: %.2f%n",
quantity, formField.getConfidence());
}
}
}));
}
}
}
}
Parameters:
Returns:
beginRecognizeReceiptsFromUrl
public SyncPoller
使用光学字符识别 (OCR) 和预生成的收据训练模型识别文档中的收据数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持
有关在收据上找到的字段,请参阅 此处 。
代码示例
String receiptUrl = "{file_source_url}";
formRecognizerClient.beginRecognizeReceiptsFromUrl(receiptUrl)
.getFinalResult()
.forEach(recognizedReceipt -> {
Map<String, FormField> recognizedFields = recognizedReceipt.getFields();
FormField merchantNameField = recognizedFields.get("MerchantName");
if (merchantNameField != null) {
if (FieldValueType.STRING == merchantNameField.getValue().getValueType()) {
String merchantName = merchantNameField.getValue().asString();
System.out.printf("Merchant Name: %s, confidence: %.2f%n",
merchantName, merchantNameField.getConfidence());
}
}
FormField merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber");
if (merchantPhoneNumberField != null) {
if (FieldValueType.PHONE_NUMBER == merchantPhoneNumberField.getValue().getValueType()) {
String merchantAddress = merchantPhoneNumberField.getValue().asPhoneNumber();
System.out.printf("Merchant Phone number: %s, confidence: %.2f%n",
merchantAddress, merchantPhoneNumberField.getConfidence());
}
}
FormField transactionDateField = recognizedFields.get("TransactionDate");
if (transactionDateField != null) {
if (FieldValueType.DATE == transactionDateField.getValue().getValueType()) {
LocalDate transactionDate = transactionDateField.getValue().asDate();
System.out.printf("Transaction Date: %s, confidence: %.2f%n",
transactionDate, transactionDateField.getConfidence());
}
}
FormField receiptItemsField = recognizedFields.get("Items");
if (receiptItemsField != null) {
System.out.printf("Receipt Items: %n");
if (FieldValueType.LIST == receiptItemsField.getValue().getValueType()) {
List<FormField> receiptItems = receiptItemsField.getValue().asList();
receiptItems.stream()
.filter(receiptItem -> FieldValueType.MAP == receiptItem.getValue().getValueType())
.map(formField -> formField.getValue().asMap())
.forEach(formFieldMap -> formFieldMap.forEach((key, formField) -> {
if ("Quantity".equals(key)) {
if (FieldValueType.FLOAT == formField.getValue().getValueType()) {
Float quantity = formField.getValue().asFloat();
System.out.printf("Quantity: %f, confidence: %.2f%n",
quantity, formField.getConfidence());
}
}
}));
}
}
});
Parameters:
Returns:
beginRecognizeReceiptsFromUrl
public SyncPoller
使用光学字符识别 (OCR) 和预生成的收据训练模型识别文档中的收据数据。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持
代码示例
String receiptUrl = "{receipt_url}";
formRecognizerClient.beginRecognizeReceiptsFromUrl(receiptUrl,
new RecognizeReceiptsOptions()
.setLocale(FormRecognizerLocale.EN_US)
.setPollInterval(Duration.ofSeconds(5))
.setFieldElementsIncluded(true), Context.NONE)
.getFinalResult()
.forEach(recognizedReceipt -> {
Map<String, FormField> recognizedFields = recognizedReceipt.getFields();
FormField merchantNameField = recognizedFields.get("MerchantName");
if (merchantNameField != null) {
if (FieldValueType.STRING == merchantNameField.getValue().getValueType()) {
String merchantName = merchantNameField.getValue().asString();
System.out.printf("Merchant Name: %s, confidence: %.2f%n",
merchantName, merchantNameField.getConfidence());
}
}
FormField merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber");
if (merchantPhoneNumberField != null) {
if (FieldValueType.PHONE_NUMBER == merchantPhoneNumberField.getValue().getValueType()) {
String merchantAddress = merchantPhoneNumberField.getValue().asPhoneNumber();
System.out.printf("Merchant Phone number: %s, confidence: %.2f%n",
merchantAddress, merchantPhoneNumberField.getConfidence());
}
}
FormField transactionDateField = recognizedFields.get("TransactionDate");
if (transactionDateField != null) {
if (FieldValueType.DATE == transactionDateField.getValue().getValueType()) {
LocalDate transactionDate = transactionDateField.getValue().asDate();
System.out.printf("Transaction Date: %s, confidence: %.2f%n",
transactionDate, transactionDateField.getConfidence());
}
}
FormField receiptItemsField = recognizedFields.get("Items");
if (receiptItemsField != null) {
System.out.printf("Receipt Items: %n");
if (FieldValueType.LIST == receiptItemsField.getValue().getValueType()) {
List<FormField> receiptItems = receiptItemsField.getValue().asList();
receiptItems.stream()
.filter(receiptItem -> FieldValueType.MAP == receiptItem.getValue().getValueType())
.map(formField -> formField.getValue().asMap())
.forEach(formFieldMap -> formFieldMap.forEach((key, formField) -> {
if ("Quantity".equals(key)) {
if (FieldValueType.FLOAT == formField.getValue().getValueType()) {
Float quantity = formField.getValue().asFloat();
System.out.printf("Quantity: %f, confidence: %.2f%n",
quantity, formField.getConfidence());
}
}
}));
}
}
});
Parameters:
Returns: