azure-ai-formrecognizer-java

โดย microsoft

การเปลี่ยนชื่อแบรนด์: Azure AI Form Recognizer เปลี่ยนเป็น Azure AI Document Intelligence แล้ว โปรเจกต์ใหม่ควรใช้ com.azure:azure-ai-documentintelligence แพ็กเกจ azure-ai-formrecognizer รุ่นเก่ารองรับเฉพาะ API เวอร์ชัน 2023-07-31 เท่านั้น ดูคู่มือการย้ายข้อมูล

npx skills add https://github.com/microsoft/skills --skill azure-ai-formrecognizer-java

Azure AI Document Intelligence SDK for Java

Rebranding: Azure AI Form Recognizer is now Azure AI Document Intelligence. New projects should use com.azure:azure-ai-documentintelligence. The legacy azure-ai-formrecognizer package targets API version 2023-07-31 only. See Migration Guide.

Before Implementation

Search microsoft-docs MCP for current API patterns:

  • Query: "azure-ai-documentintelligence Java SDK"
  • Verify: Parameters match installed SDK version (latest GA: 1.0.7)

Installation

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-ai-documentintelligence</artifactId>
    <version>1.0.0</version>
</dependency>

<!-- For DefaultAzureCredential -->
<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-identity</artifactId>
    <version>1.14.2</version>
</dependency>

Environment Variables

DOCUMENT_INTELLIGENCE_ENDPOINT=https://<resource>.cognitiveservices.azure.com/ # Required for all auth methods
AZURE_TOKEN_CREDENTIALS=prod  # Required only if DefaultAzureCredential is used in production

Authentication

DefaultAzureCredential (Recommended)

import com.azure.ai.documentintelligence.DocumentIntelligenceClient;
import com.azure.ai.documentintelligence.DocumentIntelligenceClientBuilder;
import com.azure.core.credential.TokenCredential;
import com.azure.identity.AzureIdentityEnvVars;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.identity.ManagedIdentityCredentialBuilder;

TokenCredential credential = new DefaultAzureCredentialBuilder()
    .requireEnvVars(AzureIdentityEnvVars.AZURE_TOKEN_CREDENTIALS)
    .build();
// Or use a specific credential directly in production:
// See https://learn.microsoft.com/java/api/overview/azure/identity-readme?view=azure-java-stable#credential-classes
// TokenCredential credential = new ManagedIdentityCredentialBuilder().build();

DocumentIntelligenceClient client = new DocumentIntelligenceClientBuilder()
    .endpoint(System.getenv("DOCUMENT_INTELLIGENCE_ENDPOINT"))
    .credential(credential)
    .buildClient();

API Key

import com.azure.core.credential.AzureKeyCredential;

DocumentIntelligenceClient client = new DocumentIntelligenceClientBuilder()
    .endpoint(System.getenv("DOCUMENT_INTELLIGENCE_ENDPOINT"))
    .credential(new AzureKeyCredential(System.getenv("DOCUMENT_INTELLIGENCE_KEY")))
    .buildClient();

Administration Client

import com.azure.ai.documentintelligence.DocumentIntelligenceAdministrationClient;
import com.azure.ai.documentintelligence.DocumentIntelligenceAdministrationClientBuilder;
import com.azure.core.credential.TokenCredential;
import com.azure.identity.AzureIdentityEnvVars;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.identity.ManagedIdentityCredentialBuilder;

TokenCredential credential = new DefaultAzureCredentialBuilder()
    .requireEnvVars(AzureIdentityEnvVars.AZURE_TOKEN_CREDENTIALS)
    .build();
// Or use a specific credential directly in production:
// See https://learn.microsoft.com/java/api/overview/azure/identity-readme?view=azure-java-stable#credential-classes
// TokenCredential credential = new ManagedIdentityCredentialBuilder().build();

DocumentIntelligenceAdministrationClient adminClient = new DocumentIntelligenceAdministrationClientBuilder()
    .endpoint(System.getenv("DOCUMENT_INTELLIGENCE_ENDPOINT"))
    .credential(credential)
    .buildClient();

Async Client

import com.azure.ai.documentintelligence.DocumentIntelligenceAsyncClient;
import com.azure.core.credential.TokenCredential;
import com.azure.identity.AzureIdentityEnvVars;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.identity.ManagedIdentityCredentialBuilder;

TokenCredential credential = new DefaultAzureCredentialBuilder()
    .requireEnvVars(AzureIdentityEnvVars.AZURE_TOKEN_CREDENTIALS)
    .build();
// Or use a specific credential directly in production:
// See https://learn.microsoft.com/java/api/overview/azure/identity-readme?view=azure-java-stable#credential-classes
// TokenCredential credential = new ManagedIdentityCredentialBuilder().build();

DocumentIntelligenceAsyncClient asyncClient = new DocumentIntelligenceClientBuilder()
    .endpoint(System.getenv("DOCUMENT_INTELLIGENCE_ENDPOINT"))
    .credential(credential)
    .buildAsyncClient();

Prebuilt Models

Model IDPurpose
prebuilt-readExtract text, lines, words, languages
prebuilt-layoutText, tables, selection marks, structure
prebuilt-receiptReceipt data extraction
prebuilt-invoiceInvoice field extraction
prebuilt-idDocumentID documents (passport, license)
prebuilt-tax.us.w2US W2 tax forms
prebuilt-healthInsuranceCard.usUS health insurance cards
prebuilt-contractContract field extraction

Retired models: prebuilt-businessCard and prebuilt-document are retired in API version 2024-11-30. Use the legacy azure-ai-formrecognizer package for these.

Core Patterns

Analyze from File

import com.azure.ai.documentintelligence.models.*;
import com.azure.core.util.BinaryData;
import com.azure.core.util.polling.SyncPoller;
import java.io.File;

File document = new File("document.pdf");
BinaryData documentData = BinaryData.fromFile(document.toPath(), (int) document.length());

SyncPoller<AnalyzeOperationDetails, AnalyzeResult> poller =
    client.beginAnalyzeDocument("prebuilt-layout",
        new AnalyzeDocumentOptions(documentData));

AnalyzeResult result = poller.getFinalResult();

Analyze from URL

String documentUrl = "https://example.com/invoice.pdf";

SyncPoller<AnalyzeOperationDetails, AnalyzeResult> poller =
    client.beginAnalyzeDocument("prebuilt-invoice",
        new AnalyzeDocumentOptions(documentUrl));

AnalyzeResult result = poller.getFinalResult();

Extract Layout

AnalyzeResult result = poller.getFinalResult();

for (DocumentPage page : result.getPages()) {
    System.out.printf("Page has width: %.2f and height: %.2f, measured with unit: %s%n",
        page.getWidth(), page.getHeight(), page.getUnit());

    // Lines
    for (DocumentLine line : page.getLines()) {
        System.out.printf("Line '%s' is within bounding box %s.%n",
            line.getContent(), line.getPolygon());
    }

    // Selection marks
    for (DocumentSelectionMark mark : page.getSelectionMarks()) {
        System.out.printf("Selection mark is '%s' with confidence %.2f.%n",
            mark.getState(), mark.getConfidence());
    }
}

// Tables
for (DocumentTable table : result.getTables()) {
    System.out.printf("Table: %d rows x %d columns%n",
        table.getRowCount(), table.getColumnCount());
    for (DocumentTableCell cell : table.getCells()) {
        System.out.printf("Cell[%d,%d]: %s%n",
            cell.getRowIndex(), cell.getColumnIndex(), cell.getContent());
    }
}

Extract Document Fields

SyncPoller<AnalyzeOperationDetails, AnalyzeResult> poller =
    client.beginAnalyzeDocument("prebuilt-receipt",
        new AnalyzeDocumentOptions(receiptUrl));

AnalyzeResult result = poller.getFinalResult();

for (AnalyzedDocument doc : result.getDocuments()) {
    Map<String, DocumentField> fields = doc.getFields();

    DocumentField merchantName = fields.get("MerchantName");
    if (merchantName != null && merchantName.getType() == DocumentFieldType.STRING) {
        System.out.printf("Merchant: %s (confidence: %.2f)%n",
            merchantName.getValueString(), merchantName.getConfidence());
    }

    DocumentField transactionDate = fields.get("TransactionDate");
    if (transactionDate != null && transactionDate.getType() == DocumentFieldType.DATE) {
        System.out.printf("Date: %s%n", transactionDate.getValueDate());
    }
}

Analyze with Options

SyncPoller<AnalyzeOperationDetails, AnalyzeResult> poller =
    client.beginAnalyzeDocument("my-custom-model",
        new AnalyzeDocumentOptions(documentUrl)
            .setPages(Collections.singletonList("1-3"))
            .setLocale("en-US")
            .setDocumentAnalysisFeatures(Arrays.asList(DocumentAnalysisFeature.LANGUAGES))
            .setOutputContentFormat(DocumentContentFormat.TEXT));

Custom Models

Build Custom Model

String blobContainerUrl = "{SAS_URL_of_training_data}";

SyncPoller<DocumentModelBuildOperationDetails, DocumentModelDetails> poller =
    adminClient.beginBuildDocumentModel(
        new BuildDocumentModelOptions("my-custom-model", DocumentBuildMode.TEMPLATE)
            .setAzureBlobSource(new AzureBlobContentSource(blobContainerUrl)));

DocumentModelDetails model = poller.getFinalResult();
System.out.printf("Model ID: %s%n", model.getModelId());
System.out.printf("Created: %s%n", model.getCreatedOn());

model.getDocumentTypes().forEach((docType, details) -> {
    details.getFieldSchema().forEach((field, schema) -> {
        System.out.printf("Field: %s (%s)%n", field, schema.getType());
    });
});

Manage Models

// Resource limits
DocumentIntelligenceResourceDetails resourceDetails = adminClient.getResourceDetails();
System.out.printf("Models: %d / %d%n",
    resourceDetails.getCustomDocumentModels().getCount(),
    resourceDetails.getCustomDocumentModels().getLimit());

// List models
PagedIterable<DocumentModelDetails> models = adminClient.listModels();
for (DocumentModelDetails model : models) {
    System.out.printf("Model: %s, Created: %s%n",
        model.getModelId(), model.getCreatedOn());
}

// Get model
DocumentModelDetails model = adminClient.getModel("model-id");

// Delete model
adminClient.deleteModel("model-id");

Document Classification

Build Classifier

Map<String, ClassifierDocumentTypeDetails> docTypes = new HashMap<>();
docTypes.put("invoice", new ClassifierDocumentTypeDetails()
    .setAzureBlobSource(new AzureBlobContentSource(containerUrl).setPrefix("invoices/")));
docTypes.put("receipt", new ClassifierDocumentTypeDetails()
    .setAzureBlobSource(new AzureBlobContentSource(containerUrl).setPrefix("receipts/")));

SyncPoller<DocumentClassifierBuildOperationDetails, DocumentClassifierDetails> poller =
    adminClient.beginBuildClassifier(
        new BuildDocumentClassifierOptions("my-classifier", docTypes));

DocumentClassifierDetails classifier = poller.getFinalResult();

Classify Document

SyncPoller<AnalyzeOperationDetails, AnalyzeResult> poller =
    client.beginClassifyDocument("my-classifier",
        new ClassifyDocumentOptions(documentUrl));

AnalyzeResult result = poller.getFinalResult();
for (AnalyzedDocument doc : result.getDocuments()) {
    System.out.printf("Classified as: %s (confidence: %.2f)%n",
        doc.getDocumentType(), doc.getConfidence());
}

Error Handling

import com.azure.core.exception.HttpResponseException;

try {
    client.beginAnalyzeDocument("prebuilt-receipt",
        new AnalyzeDocumentOptions("invalid-url"));
} catch (HttpResponseException e) {
    System.out.printf("Status: %d, Error: %s%n",
        e.getResponse().getStatusCode(), e.getMessage());
}

Migration from azure-ai-formrecognizer

Old (formrecognizer v4.x)New (documentintelligence v1.x)
DocumentAnalysisClientDocumentIntelligenceClient
DocumentAnalysisClientBuilderDocumentIntelligenceClientBuilder
DocumentModelAdministrationClientDocumentIntelligenceAdministrationClient
beginAnalyzeDocumentFromUrl(modelId, url)beginAnalyzeDocument(modelId, new AnalyzeDocumentOptions(url))
beginAnalyzeDocument(modelId, data)beginAnalyzeDocument(modelId, new AnalyzeDocumentOptions(data))
SyncPoller<OperationResult, AnalyzeResult>SyncPoller<AnalyzeOperationDetails, AnalyzeResult>
field.getValueAsString()field.getValueString()
field.getValueAsDate()field.getValueDate()
field.getValueAsDouble()field.getValueNumber()
field.getValueAsList()field.getValueList()
field.getValueAsMap()field.getValueObject()
mark.getSelectionMarkState()mark.getState()
adminClient.beginBuildDocumentModel(url, mode, prefix, options, ctx)adminClient.beginBuildDocumentModel(new BuildDocumentModelOptions(id, mode).setAzureBlobSource(...))
adminClient.getResourceDetails().getCustomDocumentModelCount()adminClient.getResourceDetails().getCustomDocumentModels().getCount()
FORM_RECOGNIZER_ENDPOINTDOCUMENT_INTELLIGENCE_ENDPOINT

Reference Files

FileContents
references/examples.mdComplete code examples for all scenarios

Skills เพิ่มเติมจาก microsoft

oss-growth
microsoft
บุคลิกภาพนักเติบโตโอเอสเอส
official
microsoft-foundry
microsoft
ปรับใช้ ประเมิน และจัดการ Foundry agents แบบครบวงจร: สร้าง Docker, push ไปยัง ACR, สร้าง hosted/prompt agent, เริ่ม container, batch eval, continuous eval, เวิร์กโฟลว์ prompt optimizer, agent.yaml, จัดชุดข้อมูลจาก traces ใช้สำหรับ: ปรับใช้ agent ไปยัง Foundry, hosted agent, สร้าง agent, เรียกใช้ agent, ประเมิน agent, รัน batch eval, continuous eval, การตรวจสอบต่อเนื่อง, สถานะ continuous eval, ปรับแต่ง prompt, ปรับปรุง prompt, prompt optimizer, ปรับแต่งคำแนะนำ agent, ปรับปรุง agent...
officialdevelopmentdevops
azure-ai
microsoft
ใช้สำหรับ Azure AI: ค้นหา, คำพูด, OpenAI, การประมวลผลเอกสารอัจฉริยะ ช่วยในการค้นหา, การค้นหาแบบเวกเตอร์/ไฮบริด, การแปลงคำพูดเป็นข้อความ, การแปลงข้อความเป็นคำพูด, การถอดเสียง, OCR เมื่อ: การค้นหา AI, การค้นหาคำถาม, การค้นหาแบบเวกเตอร์, การค้นหาแบบไฮบริด, การค้นหาเชิงความหมาย, การแปลงคำพูดเป็นข้อความ, การแปลงข้อความเป็นคำพูด, การถอดเสียง, OCR, แปลงข้อความเป็นคำพูด
officialdevelopmentapi
azure-deploy
microsoft
ดำเนินการปรับใช้ Azure สำหรับแอปพลิเคชันที่เตรียมไว้แล้วซึ่งมีไฟล์ .azure/deployment-plan.md และไฟล์โครงสร้างพื้นฐานอยู่แล้ว ห้ามใช้ทักษะนี้เมื่อผู้ใช้ขอสร้างแอปพลิเคชันใหม่ ให้ใช้ azure-prepare แทน ทักษะนี้รันคำสั่ง azd up, azd deploy, terraform apply และ az deployment พร้อมการกู้คืนข้อผิดพลาดในตัว ต้องมี .azure/deployment-plan.md จาก azure-prepare และสถานะที่ตรวจสอบแล้วจาก azure-validate เมื่อ: "run azd up", "run azd deploy", "execute deployment",...
officialdevopsaws
azure-storage
microsoft
บริการ Azure Storage รวมถึง Blob Storage, File Shares, Queue Storage, Table Storage และ Data Lake ตอบคำถามเกี่ยวกับระดับการเข้าถึงพื้นที่จัดเก็บ (hot, cool, cold, archive) ว่าเมื่อใดควรใช้แต่ละระดับ และการเปรียบเทียบระดับ ให้บริการจัดเก็บวัตถุ, แชร์ไฟล์ SMB, การส่งข้อความแบบอะซิงโครนัส, NoSQL key-value และการวิเคราะห์ข้อมูลขนาดใหญ่ รวมถึงการจัดการวงจรชีวิต ใช้สำหรับ: blob storage, file shares, queue storage, table storage, data lake, อัปโหลดไฟล์, ดาวน์โหลด blobs, storage accounts, access tiers,...
officialdevelopmentdatabase
azure-diagnostics
microsoft
ดีบักปัญหาการผลิตบน Azure โดยใช้ AppLens, Azure Monitor, สถานะทรัพยากร และการจัดลำดับความสำคัญอย่างปลอดภัย เมื่อ: ดีบักปัญหาการผลิต, แก้ไขปัญหาแอปบริการ, แอปบริการ CPU สูง, แอปบริการล้มเหลวในการปรับใช้, แก้ไขปัญหาคอนเทนเนอร์แอป, แก้ไขปัญหาฟังก์ชัน, แก้ไขปัญหา AKS, kubectl ไม่สามารถเชื่อมต่อ, kube-system/CoreDNS ล้มเหลว, pod รอ, crashloop, node ไม่พร้อม, การอัปเกรดล้มเหลว, วิเคราะห์บันทึก, KQL, ข้อมูลเชิงลึก, การดึงอิมเมจล้มเหลว, ปัญหาการเริ่มต้นเย็น, การตรวจสอบสถานะล้มเหลว,...
officialdevopsdevelopment
azure-prepare
microsoft
เตรียมแอปพลิเคชัน Azure สำหรับการปรับใช้ (infra Bicep/Terraform, azure.yaml, Dockerfiles) ใช้สำหรับสร้าง/ปรับปรุงให้ทันสมัย หรือสร้าง+ปรับใช้ ไม่ใช่สำหรับการย้ายข้ามคลาวด์ (ใช้ azure-cloud-migrate) ห้ามใช้สำหรับ: แอป copilot-sdk (ใช้ azure-hosted-copilot-sdk) เมื่อ: "สร้างแอป", "สร้างเว็บแอป", "สร้าง API", "สร้าง HTTP API แบบไร้เซิร์ฟเวอร์", "สร้างฟรอนต์เอนด์", "สร้างแบ็กเอนด์", "สร้างบริการ", "ปรับปรุงแอปพลิเคชันให้ทันสมัย", "อัปเดตแอปพลิเคชัน", "เพิ่มการรับรองความถูกต้อง", "เพิ่มการแคช", "โฮสต์บน Azure", "สร้างและ...
officialdevelopmentdevops
azure-validate
microsoft
การตรวจสอบความพร้อมก่อนการปรับใช้สำหรับ Azure ตรวจสอบเชิงลึกเกี่ยวกับการกำหนดค่า โครงสร้างพื้นฐาน (Bicep หรือ Terraform) การกำหนดบทบาท RBAC สิทธิ์ของ managed identity และข้อกำหนดเบื้องต้นก่อนการปรับใช้ เมื่อ: ตรวจสอบแอปของฉัน ตรวจสอบความพร้อมในการปรับใช้ เรียกใช้การตรวจสอบก่อนดำเนินการ ยืนยันการกำหนดค่า ตรวจสอบว่าพร้อมปรับใช้หรือไม่ ตรวจสอบ azure.yaml ตรวจสอบ Bicep ทดสอบก่อนปรับใช้ แก้ไขข้อผิดพลาดในการปรับใช้ ตรวจสอบ Azure Functions ตรวจสอบ function app ตรวจสอบ serverless...
officialdevopstesting