你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

DocumentAnalysisClient 类

  • java.lang.Object
    • com.azure.ai.formrecognizer.documentanalysis.DocumentAnalysisClient

public final class DocumentAnalysisClient

此类提供一个同步客户端,用于连接到 表单识别器 Azure 认知服务。

此客户端提供同步方法来执行以下操作:

  1. 自定义文档分析:分类、提取和分析特定于不同业务数据和用例的表单和文档中的数据。 通过将自定义训练的模型 modelId 传递到 方法, com.azure.ai.formrecognizer.documentanalysis.DocumentAnalysisClient#beginAnalyzeDocument(String, BinaryData) 使用该模型。
  2. 常规文档分析:提取文本、表、结构和键值对。 通过将 modelId=“rebuilt-document”com.azure.ai.formrecognizer.documentanalysis.DocumentAnalysisClient#beginAnalyzeDocument(String, BinaryData)传递到 方法,使用 表单识别器 服务提供的常规文档模型。
  3. 预生成模型分析:使用 分析收据、名片、发票、ID、W2 和其他文档 supported prebuilt models. Use the prebuilt receipt model provided by passing modelId="prebuilt-receipt" into the com.azure.ai.formrecognizer.documentanalysis.DocumentAnalysisClient#beginAnalyzeDocument(String, BinaryData) method.
  4. 布局分析:从窗体和文档中提取文本、选择标记和表结构及其边界框坐标。 通过将 modelId=“prebuilt-layout” com.azure.ai.formrecognizer.documentanalysis.DocumentAnalysisClient#beginAnalyzeDocument(String, BinaryData) 传递到 方法,使用为服务提供的布局分析模型。
  5. 轮询和回调:它包括轮询服务以检查分析操作的状态或注册回调以在分析完成时接收通知的机制。

此客户端还根据 URL 中的输入和来自流的输入提供不同的方法。

注意: 此客户端仅支持 V2022_08_31 和 更新版本。 若要使用较旧的服务版本, FormRecognizerClientFormTrainingClient

服务客户端是开发人员使用 Azure 表单识别器的交互点。 DocumentAnalysisClient 是同步服务客户端, DocumentAnalysisAsyncClient 是异步服务客户端。 本文档中显示的示例使用名为 DefaultAzureCredential 的凭据对象进行身份验证,该对象适用于大多数方案,包括本地开发和生产环境。 此外,我们建议使用 托管标识 在生产环境中进行身份验证。 可以在 Azure 标识文档中找到有关不同身份验证方式及其相应凭据类型的详细信息。

示例:使用 DefaultAzureCredential 构造 DocumentAnalysisAsyncClient

下面的代码示例演示如何 DocumentAnalysisClient使用“DefaultAzureCredentialBuilder”对其进行配置来创建 。

DocumentAnalysisClient documentAnalysisClient = new DocumentAnalysisClientBuilder()
     .endpoint("{endpoint}")
     .credential(new DefaultAzureCredentialBuilder().build())
     .buildClient();

此外,请参阅下面的代码示例,以用于 AzureKeyCredential 创建客户端。

DocumentAnalysisClient documentAnalysisClient = new DocumentAnalysisClientBuilder()
     .credential(new AzureKeyCredential("{key}"))
     .endpoint("{endpoint}")
     .buildClient();

方法摘要

修饰符和类型 方法和描述
SyncPoller<OperationResult,AnalyzeResult> beginAnalyzeDocument(String modelId, BinaryData document)

使用任何预生成模型或自定义分析模型,使用光学字符识别 (OCR) 分析文档中的数据。

SyncPoller<OperationResult,AnalyzeResult> beginAnalyzeDocument(String modelId, BinaryData document, AnalyzeDocumentOptions analyzeDocumentOptions, Context context)

使用任何预生成模型或自定义分析模型,使用光学字符识别 (OCR) 和给定文档中的语义值来分析文档中的数据。

SyncPoller<OperationResult,AnalyzeResult> beginAnalyzeDocumentFromUrl(String modelId, String documentUrl)

使用任何预生成模型或自定义分析模型,使用光学字符识别 (OCR) 和给定文档中的语义值来分析文档中的数据。

SyncPoller<OperationResult,AnalyzeResult> beginAnalyzeDocumentFromUrl(String modelId, String documentUrl, AnalyzeDocumentOptions analyzeDocumentOptions, Context context)

使用任何预生成模型或自定义分析模型,使用光学字符识别 (OCR) 和给定文档中的语义值来分析文档中的数据。

SyncPoller<OperationResult,AnalyzeResult> beginClassifyDocument(String classifierId, BinaryData document)

使用文档分类器对给定文档进行分类。

SyncPoller<OperationResult,AnalyzeResult> beginClassifyDocument(String classifierId, BinaryData document, Context context)

使用文档分类器对给定文档进行分类。

SyncPoller<OperationResult,AnalyzeResult> beginClassifyDocumentFromUrl(String classifierId, String documentUrl)

使用文档分类器对给定文档进行分类。

SyncPoller<OperationResult,AnalyzeResult> beginClassifyDocumentFromUrl(String classifierId, String documentUrl, Context context)

使用文档分类器对给定文档进行分类。

方法继承自 java.lang.Object

方法详细信息

beginAnalyzeDocument

public SyncPoller beginAnalyzeDocument(String modelId, BinaryData document)

使用任何预生成模型或自定义分析模型,使用光学字符识别 (OCR) 分析文档中的数据。

该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。

代码示例

File document = new File("{local/file_path/fileName.jpg}");
     String modelId = "{custom_trained_model_id}";
     byte[] fileContent = Files.readAllBytes(document.toPath());

     documentAnalysisClient.beginAnalyzeDocument(modelId, BinaryData.fromBytes(fileContent))
         .getFinalResult()
         .getDocuments().stream()
         .map(AnalyzedDocument::getFields)
         .forEach(documentFieldMap -> documentFieldMap.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 - 要使用的唯一模型 ID。 用于指定自定义模型 ID 或预生成模型 ID。 可在此处找到支持的预生成模型 ID
document - 要从中分析信息的文档的数据。

Returns:

一个 , SyncPoller<T,U> 它轮询分析文档操作的进度,直到它完成、失败或已取消。 完成的操作返回 AnalyzeResult

beginAnalyzeDocument

public SyncPoller beginAnalyzeDocument(String modelId, BinaryData document, AnalyzeDocumentOptions analyzeDocumentOptions, Context context)

使用任何预生成模型或自定义分析模型,使用光学字符识别 (OCR) 和给定文档中的语义值来分析文档中的数据。

该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。

代码示例

使用可配置的选项分析文档。

File document = new File("{local/file_path/fileName.jpg}");
 String modelId = "{custom_trained_model_id}";
 byte[] fileContent = Files.readAllBytes(document.toPath());

 documentAnalysisClient.beginAnalyzeDocument(modelId, BinaryData.fromBytes(fileContent),
         new AnalyzeDocumentOptions().setPages(Arrays.asList("1", "3")), Context.NONE)
     .getFinalResult()
     .getDocuments().stream()
     .map(AnalyzedDocument::getFields)
     .forEach(documentFieldMap -> documentFieldMap.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 - 要使用的唯一模型 ID。 用于指定自定义模型 ID 或预生成模型 ID。 可在此处找到支持的预生成模型 ID
document - 要从中分析信息的文档的数据。
analyzeDocumentOptions - 分析文档时可能传递的其他可配置 AnalyzeDocumentOptions 项。
context - 在服务调用期间通过 HTTP 管道传递的其他上下文。

Returns:

一个 , SyncPoller<T,U> 它轮询分析文档操作的进度,直到它完成、失败或已取消。 完成的操作返回 AnalyzeResult

beginAnalyzeDocumentFromUrl

public SyncPoller beginAnalyzeDocumentFromUrl(String modelId, String documentUrl)

使用任何预生成模型或自定义分析模型,使用光学字符识别 (OCR) 和给定文档中的语义值来分析文档中的数据。

该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持

代码示例

使用文档的 URL 分析文档。

String documentUrl = "{document_url}";
 String modelId = "{custom_trained_model_id}";

 documentAnalysisClient.beginAnalyzeDocumentFromUrl(modelId, documentUrl).getFinalResult()
     .getDocuments().stream()
     .map(AnalyzedDocument::getFields)
     .forEach(documentFieldMap -> documentFieldMap.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 - 要使用的唯一模型 ID。 用于指定自定义模型 ID 或预生成模型 ID。 可在此处找到支持的预生成模型 ID
documentUrl - 要分析的文档的 URL。

Returns:

一个 SyncPoller<T,U> ,用于轮询分析文档操作的进度,直到它完成、失败或被取消为止。 完成的操作返回 AnalyzeResult

beginAnalyzeDocumentFromUrl

public SyncPoller beginAnalyzeDocumentFromUrl(String modelId, String documentUrl, AnalyzeDocumentOptions analyzeDocumentOptions, Context context)

使用任何预生成模型或自定义分析模型,使用光学字符识别 (OCR) 和给定文档中的语义值来分析文档中的数据。

该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持

代码示例

使用具有可配置选项的文档 URL 分析文档。

String documentUrl = "{document_url}";
 String modelId = "{custom_trained_model_id}";

 documentAnalysisClient.beginAnalyzeDocumentFromUrl(modelId, documentUrl).getFinalResult()
     .getDocuments().stream()
     .map(AnalyzedDocument::getFields)
     .forEach(documentFieldMap -> documentFieldMap.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 - 要使用的唯一模型 ID。 用于指定自定义模型 ID 或预生成模型 ID。 可在此处找到支持的预生成模型 ID
documentUrl - 输入文档的源 URL。
analyzeDocumentOptions - 分析文档时可能传递的其他可配置 AnalyzeDocumentOptions 项。
context - 在服务调用期间通过 HTTP 管道传递的其他上下文。

Returns:

一个 SyncPoller<T,U> ,用于轮询分析文档操作的进度,直到它完成、失败或被取消为止。 完成的操作返回 AnalyzeResult

beginClassifyDocument

public SyncPoller beginClassifyDocument(String classifierId, BinaryData document)

使用文档分类器对给定文档进行分类。 有关如何生成自定义分类器模型的详细信息,请参阅

该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。

代码示例

File document = new File("{local/file_path/fileName.jpg}");
 String classifierId = "{custom_trained_classifier_id}";
 byte[] fileContent = Files.readAllBytes(document.toPath());

 documentAnalysisClient.beginClassifyDocument(classifierId, BinaryData.fromBytes(fileContent))
     .getFinalResult()
     .getDocuments()
     .forEach(analyzedDocument -> System.out.printf("Doc Type: %s%n", analyzedDocument.getDocType()));

Parameters:

classifierId - 要使用的唯一分类器 ID。 使用此来指定自定义分类器 ID。
document - 要从中分析信息的文档的数据。

Returns:

一个 , SyncPoller<T,U> 它轮询分析文档操作的进度,直到它完成、失败或已取消。 完成的操作返回 AnalyzeResult

beginClassifyDocument

public SyncPoller beginClassifyDocument(String classifierId, BinaryData document, Context context)

使用文档分类器对给定文档进行分类。 有关如何生成自定义分类器模型的详细信息,请参阅

该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。

代码示例

File document = new File("{local/file_path/fileName.jpg}");
 String classifierId = "{custom_trained_classifier_id}";
 byte[] fileContent = Files.readAllBytes(document.toPath());

 documentAnalysisClient.beginClassifyDocument(classifierId, BinaryData.fromBytes(fileContent), Context.NONE)
     .getFinalResult()
     .getDocuments()
     .forEach(analyzedDocument -> System.out.printf("Doc Type: %s%n", analyzedDocument.getDocType()));

Parameters:

classifierId - 要使用的唯一分类器 ID。 使用此来指定自定义分类器 ID。
document - 要从中分析信息的文档的数据。
context - 在服务调用期间通过 HTTP 管道传递的其他上下文。

Returns:

一个 SyncPoller<T,U> ,它轮询分析文档操作的进度,直到它完成、失败或被取消。 完成的操作返回 AnalyzeResult

beginClassifyDocumentFromUrl

public SyncPoller beginClassifyDocumentFromUrl(String classifierId, String documentUrl)

使用文档分类器对给定文档进行分类。 有关如何生成自定义分类器模型的详细信息,请参阅

该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持

代码示例

使用具有可配置选项的文档 URL 分析文档。

String documentUrl = "{file_source_url}";
 String classifierId = "{custom_trained_classifier_id}";

 documentAnalysisClient.beginClassifyDocumentFromUrl(classifierId, documentUrl)
     .getFinalResult()
     .getDocuments()
     .forEach(analyzedDocument -> System.out.printf("Doc Type: %s%n", analyzedDocument.getDocType()));

Parameters:

classifierId - 要使用的唯一分类器 ID。 使用此来指定自定义分类器 ID。 可在此处找到支持的预生成模型 ID
documentUrl - 输入文档的源 URL。

Returns:

一个 SyncPoller<T,U> ,用于轮询分析文档操作的进度,直到它已完成、失败或已取消。 完成的操作返回 AnalyzeResult

beginClassifyDocumentFromUrl

public SyncPoller beginClassifyDocumentFromUrl(String classifierId, String documentUrl, Context context)

使用文档分类器对给定文档进行分类。 有关如何生成自定义分类器模型的详细信息,请参阅

该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持

代码示例

使用具有可配置选项的文档 URL 分析文档。

String documentUrl = "{file_source_url}";
 String classifierId = "{custom_trained_classifier_id}";

 documentAnalysisClient.beginClassifyDocumentFromUrl(classifierId, documentUrl, Context.NONE)
     .getFinalResult()
     .getDocuments()
     .forEach(analyzedDocument -> System.out.printf("Doc Type: %s%n", analyzedDocument.getDocType()));

Parameters:

classifierId - 要使用的唯一分类器 ID。 使用此来指定自定义分类器 ID。 可在此处找到支持的预生成模型 ID
documentUrl - 输入文档的源 URL。
context - 在服务调用期间通过 HTTP 管道传递的其他上下文。

Returns:

一个 SyncPoller<T,U> ,用于轮询分析文档操作的进度,直到它已完成、失败或已取消。 完成的操作返回 AnalyzeResult

适用于