Class DocumentAnalysisAsyncClient


  • public final class DocumentAnalysisAsyncClient
    extends Object
    This class provides an asynchronous client that contains the operations that apply to Azure Form Recognizer. Operations allowed by the client are analyzing information from documents and images using custom-built document analysis models, prebuilt models for invoices, receipts, identity documents and business cards, and the layout model.

    Instantiating an asynchronous Document Analysis Client

     DocumentAnalysisAsyncClient documentAnalysisAsyncClient = new DocumentAnalysisClientBuilder()
         .credential(new AzureKeyCredential("{key}"))
         .endpoint("{endpoint}")
         .buildAsyncClient();
     
    See Also:
    DocumentAnalysisClientBuilder
    • Method Detail

      • beginAnalyzeDocumentFromUrl

        public com.azure.core.util.polling.PollerFlux<DocumentOperationResult,​AnalyzeResult> beginAnalyzeDocumentFromUrl​(String modelId,
                                                                                                                               String documentUrl)
        Analyzes data from documents with optical character recognition (OCR) and semantic values from a given document using any of the prebuilt models or a custom-built analysis 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

        Analyze a document using the URL of the document.

         String documentUrl = "{document_url}";
         String modelId = "{model_id}";
         documentAnalysisAsyncClient.beginAnalyzeDocumentFromUrl(modelId, documentUrl)
             // if polling operation completed, retrieve the final result.
             .flatMap(AsyncPollResponse::getFinalResult)
             .subscribe(analyzeResult ->
                 analyzeResult.getDocuments()
                     .stream()
                     .forEach(document ->
                         document.getFields()
                             .forEach((key, documentField) -> {
                                 System.out.printf("Field text: %s%n", key);
                                 System.out.printf("Field value data content: %s%n", documentField.getContent());
                                 System.out.printf("Confidence score: %.2f%n", documentField.getConfidence());
                             })));
         
        Parameters:
        modelId - The unique model ID to be used. Use this to specify the custom model ID or prebuilt model ID. Prebuilt model IDs supported can be found here
        documentUrl - The URL of the document to analyze.
        Returns:
        A PollerFlux that polls the progress of the analyze document operation until it has completed, has failed, or has been cancelled. The completed operation returns an AnalyzeResult.
        Throws:
        DocumentModelOperationException - If analyze operation fails and the AnalyzeResultOperation returns with an OperationStatus.FAILED..
        IllegalArgumentException - If documentUrl or modelId is null.
      • beginAnalyzeDocumentFromUrl

        public com.azure.core.util.polling.PollerFlux<DocumentOperationResult,​AnalyzeResult> beginAnalyzeDocumentFromUrl​(String modelId,
                                                                                                                               String documentUrl,
                                                                                                                               AnalyzeDocumentOptions analyzeDocumentOptions)
        Analyzes data from documents with optical character recognition (OCR) and semantic values from a given document using any of the prebuilt models or a custom-built analysis 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

        Analyze a document using the URL of the document with configurable options.

         String documentUrl = "{document_url}";
         // analyze a receipt using prebuilt model
         String modelId = "prebuilt-receipt";
        
         documentAnalysisAsyncClient.beginAnalyzeDocumentFromUrl(modelId, documentUrl,
                 new AnalyzeDocumentOptions().setPages(Arrays.asList("1", "3")))
             // if polling operation completed, retrieve the final result.
             .flatMap(AsyncPollResponse::getFinalResult)
             .subscribe(analyzeResult -> {
                 System.out.println(analyzeResult.getModelId());
                 analyzeResult.getDocuments()
                     .stream()
                     .forEach(document ->
                         document.getFields()
                             .forEach((key, documentField) -> {
                                 System.out.printf("Field text: %s%n", key);
                                 System.out.printf("Field value data content: %s%n", documentField.getContent());
                                 System.out.printf("Confidence score: %.2f%n", documentField.getConfidence());
                             }));
             });
        
         
        Parameters:
        modelId - The unique model ID to be used. Use this to specify the custom model ID or prebuilt model ID. Prebuilt model IDs supported can be found here
        documentUrl - The source URL to the input form.
        analyzeDocumentOptions - The additional configurable options that may be passed when analyzing documents.
        Returns:
        A PollerFlux that polls progress of the analyze document operation until it has completed, has failed, or has been cancelled. The completed operation returns an AnalyzeResult.
        Throws:
        DocumentModelOperationException - If analyze operation fails and the AnalyzeResultOperation returns with an OperationStatus.FAILED.
        IllegalArgumentException - If documentUrl or modelId is null.
      • beginAnalyzeDocument

        public com.azure.core.util.polling.PollerFlux<DocumentOperationResult,​AnalyzeResult> beginAnalyzeDocument​(String modelId,
                                                                                                                        Flux<ByteBuffer> document,
                                                                                                                        long length)
        Analyzes data from documents with optical character recognition (OCR) and semantic values from a given document using any of the prebuilt models or a custom-built analysis model.

        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

        Analyze a document.

         File document = new File("{local/file_path/fileName.jpg}");
         String modelId = "{model_id}";
         // Utility method to convert input stream to Byte buffer
         Flux<ByteBuffer> buffer =
             Utility.toFluxByteBuffer(new ByteArrayInputStream(Files.readAllBytes(document.toPath())));
        
         documentAnalysisAsyncClient.beginAnalyzeDocument(modelId, buffer, document.length())
             // if polling operation completed, retrieve the final result.
             .flatMap(AsyncPollResponse::getFinalResult)
             .subscribe(analyzeResult ->
                 analyzeResult.getDocuments()
                     .stream()
                     .forEach(analyzedDocument ->
                         analyzedDocument.getFields()
                             .forEach((key, documentField) -> {
                                 System.out.printf("Field text: %s%n", key);
                                 System.out.printf("Field value data content: %s%n", documentField.getContent());
                                 System.out.printf("Confidence score: %.2f%n", documentField.getConfidence());
                             })));
         
        Parameters:
        modelId - The unique model ID to be used. Use this to specify the custom model ID or prebuilt model ID. Prebuilt model IDs supported can be found here
        document - The data of the document to analyze information from.
        length - The exact length of the data.
        Returns:
        A PollerFlux that polls the progress of the analyze document operation until it has completed, has failed, or has been cancelled. The completed operation returns an AnalyzeResult.
        Throws:
        DocumentModelOperationException - If analyze operation fails and the AnalyzeResultOperation returns with an OperationStatus.FAILED.
        IllegalArgumentException - If document or modelId is null.
      • beginAnalyzeDocument

        public com.azure.core.util.polling.PollerFlux<DocumentOperationResult,​AnalyzeResult> beginAnalyzeDocument​(String modelId,
                                                                                                                        Flux<ByteBuffer> document,
                                                                                                                        long length,
                                                                                                                        AnalyzeDocumentOptions analyzeDocumentOptions)
        Analyzes data from documents with optical character recognition (OCR) and semantic values from a given document using any of the prebuilt models or a custom-built analysis model.

        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

        Analyze a document with configurable options. .

         File document = new File("{local/file_path/fileName.jpg}");
         String modelId = "{model_id}";
        
         // Utility method to convert input stream to Byte buffer
         Flux<ByteBuffer> buffer =
             Utility.toFluxByteBuffer(new ByteArrayInputStream(Files.readAllBytes(document.toPath())));
        
         documentAnalysisAsyncClient.beginAnalyzeDocument(modelId, buffer, document.length(),
                 new AnalyzeDocumentOptions().setPages(Arrays.asList("1", "3")))
             // if polling operation completed, retrieve the final result.
             .flatMap(AsyncPollResponse::getFinalResult)
             .subscribe(analyzeResult -> {
                 System.out.println(analyzeResult.getModelId());
                 analyzeResult.getDocuments()
                     .stream()
                     .forEach(analyzedDocument ->
                         analyzedDocument.getFields()
                             .forEach((key, documentField) -> {
                                 System.out.printf("Field text: %s%n", key);
                                 System.out.printf("Field value data content: %s%n", documentField.getContent());
                                 System.out.printf("Confidence score: %.2f%n", documentField.getConfidence());
                             }));
             });
         
        Parameters:
        modelId - The unique model ID to be used. Use this to specify the custom model ID or prebuilt model ID. Prebuilt model IDs supported can be found here
        document - The data of the document to analyze information from.
        length - The exact length of the data.
        analyzeDocumentOptions - The additional configurable options that may be passed when analyzing documents.
        Returns:
        A PollerFlux that polls the progress of the analyze document operation until it has completed, has failed, or has been cancelled. The completed operation returns an AnalyzeResult.
        Throws:
        DocumentModelOperationException - If analyze operation fails and the AnalyzeResultOperation returns with an OperationStatus.FAILED.
        IllegalArgumentException - If document or modelId is null.