FormRecognizerAsyncClient Class
- java.
lang. Object - com.
azure. ai. formrecognizer. FormRecognizerAsyncClient
- com.
public final class FormRecognizerAsyncClient
This class provides an asynchronous client to connect to the Form Recognizer Azure Cognitive Service.
This client provides asynchronous methods to perform:
- Custom Form Analysis: Extraction and analysis of data from forms and documents specific to distinct business data and use cases. Use the custom trained model by passing its modelId into the beginRecognizeCustomForms(String modelId, Flux<ByteBuffer> form, long length) method.
- Prebuilt Model Analysis: Analyze receipts, business cards, invoices and other documents with supported prebuilt models Use the beginRecognizeReceipts(Flux<ByteBuffer> receipt, long length) method to recognize receipt information.
- Layout Analysis: Extraction and analysis of text, selection marks, tables, and bounding box coordinates, from forms and documents. Use beginRecognizeContent(Flux<ByteBuffer> form, long length) method too perform layout analysis.
- Polling and Callbacks: It includes mechanisms for polling the service to check the status of an analysis operation or registering callbacks to receive notifications when the analysis is complete.
Note: This client only supports V2_1 and lower. Recommended to use a newer service version, DocumentAnalysisClient and DocumentModelAdministrationClient.
Refer to the Migration guide to use API versions 2022-08-31 and up.
Service clients are the point of interaction for developers to use Azure Form Recognizer. FormRecognizerClient is the synchronous service client and FormRecognizerAsyncClient is the asynchronous service client. The examples shown in this document use a credential object named DefaultAzureCredential for authentication, which is appropriate for most scenarios, including local development and production environments. Additionally, we recommend using managed identity for authentication in production environments. You can find more information on different ways of authenticating and their corresponding credential types in the Azure Identity documentation".
Sample: Construct a FormRecognizerClient with DefaultAzureCredential
The following code sample demonstrates the creation of a FormRecognizerAsyncClient, using the `DefaultAzureCredentialBuilder` to configure it.
FormRecognizerAsyncClient formRecognizerAsyncClient = new FormRecognizerClientBuilder()
.endpoint("{endpoint}")
.credential(new DefaultAzureCredentialBuilder().build())
.buildAsyncClient();
Further, see the code sample below to use AzureKeyCredential for client creation.
FormRecognizerAsyncClient formRecognizerAsyncClient = new FormRecognizerClientBuilder()
.credential(new AzureKeyCredential("{key}"))
.endpoint("{endpoint}")
.buildAsyncClient();
Method Summary
Methods inherited from java.lang.Object
Method Details
beginRecognizeBusinessCards
public PollerFlux
Recognizes business card data using optical character recognition (OCR) and a prebuilt business card trained model.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
See here for fields found on a business card.
Note that the businessCard
passed must be replayable if retries are enabled (the default). In other words, the Flux
must produce the same data each time it is subscribed to.
Code sample
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
Recognizes business card data from documents using optical character recognition (OCR) and a prebuilt business card trained model.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
See here for fields found on a business card.
Note that the businessCard
passed must be replayable if retries are enabled (the default). In other words, the Flux
must produce the same data each time it is subscribed to.
Code sample
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
Recognizes business card data using optical character recognition (OCR) and a prebuilt business card trained model.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
See here for fields found on a business card.
Code sample
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
Recognizes business card data using optical character recognition (OCR) and a prebuilt business card trained model.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
See here for fields found on a business card.
Code sample
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
Recognizes content/layout data using optical character recognition (OCR).
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
Note that the data
passed must be replayable if retries are enabled (the default). In other words, the Flux
must produce the same data each time it is subscribed to.
Code sample
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
Recognizes content/layout data using optical character recognition (OCR).
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
Note that the data
passed must be replayable if retries are enabled (the default). In other words, the Flux
must produce the same data each time it is subscribed to.
Content recognition supports auto language identification and multilanguage documents, so only provide a language code if you would like to force the documented to be processed as that specific language in the RecognizeContentOptions.
Code sample
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
Recognizes content/layout data from documents using optical character recognition (OCR).
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
Code sample
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
Recognizes layout data from documents using optical character recognition (OCR) and a custom trained model.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
Content recognition supports auto language identification and multilanguage documents, so only provide a language code if you would like to force the documented to be processed as that specific language in the RecognizeContentOptions.
Code sample
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
Recognizes form data from documents using optical character recognition (OCR) and a custom trained model with or without labels.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
Note that the data
passed must be replayable if retries are enabled (the default). In other words, the Flux
must produce the same data each time it is subscribed to.
Code sample
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
Recognizes form data from documents using optical character recognition (OCR) and a custom trained model with or without labels.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
Note that the data
passed must be replayable if retries are enabled (the default). In other words, the Flux
must produce the same data each time it is subscribed to.
Code sample
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
Recognizes form data from documents using optical character recognition (OCR) and a custom trained model with or without labels.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
Code sample
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
Recognizes form data from documents using optical character recognition (OCR) and a custom trained model.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
Code sample
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
Analyze identity documents using optical character recognition (OCR) and a prebuilt model trained on identity documents model to extract key information from passports and US driver licenses. See here for fields found on an identity document.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
Note that the identityDocument
passed must be replayable if retries are enabled (the default). In other words, the Flux
must produce the same data each time it is subscribed to.
Code sample
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
Analyze identity documents using optical character recognition (OCR) and a prebuilt model trained on identity documents model to extract key information from passports and US driver licenses. See here for fields found on an identity document.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
Note that the identityDocument
passed must be replayable if retries are enabled (the default). In other words, the Flux
must produce the same data each time it is subscribed to.
Code sample
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
Analyze identity documents using optical character recognition (OCR) and a prebuilt model trained on identity documents model to extract key information from passports and US driver licenses. See here for fields found on an identity document.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
Code sample
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
Analyze identity documents using optical character recognition (OCR) and a prebuilt model trained on identity documents model to extract key information from passports and US driver licenses. See here for fields found on an identity document.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
Code sample
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
Recognizes invoice data using optical character recognition (OCR) and a prebuilt invoice trained model.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
See here for fields found on a invoice. Note that the invoice
passed must be replayable if retries are enabled (the default). In other words, the Flux
must produce the same data each time it is subscribed to.
Code sample
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
Recognizes invoice data from documents using optical character recognition (OCR) and a prebuilt invoice trained model.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
See here for fields found on a invoice. Note that the invoice
passed must be replayable if retries are enabled (the default). In other words, the Flux
must produce the same data each time it is subscribed to.
Code sample
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
Recognizes invoice data using optical character recognition (OCR) and a prebuilt invoice trained model.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
See here for fields found on a invoice.
Code sample
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
Recognizes invoice data using optical character recognition (OCR) and a prebuilt invoice trained model.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
Code sample
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
Recognizes receipt data using optical character recognition (OCR) and a prebuilt receipt trained model.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
See here for fields found on a receipt.
Note that the receipt
passed must be replayable if retries are enabled (the default). In other words, the Flux
must produce the same data each time it is subscribed to.
Code sample
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
Recognizes receipt data from documents using optical character recognition (OCR) and a prebuilt receipt trained model.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
See here for fields found on a receipt.
Note that the receipt
passed must be replayable if retries are enabled (the default). In other words, the Flux
must produce the same data each time it is subscribed to.
Code sample
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
Recognizes receipt data using optical character recognition (OCR) and a prebuilt receipt trained model.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
See here for fields found on a receipt.
Code sample
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
Recognizes receipt data using optical character recognition (OCR) and a prebuilt receipt trained model.
The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
Code sample
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:
Applies to
Azure SDK for Java