/
AnalyzeAddOnBarcodes.java
73 lines (62 loc) · 3.23 KB
/
AnalyzeAddOnBarcodes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.ai.documentintelligence;
import com.azure.ai.documentintelligence.models.AnalyzeDocumentRequest;
import com.azure.ai.documentintelligence.models.AnalyzeResult;
import com.azure.ai.documentintelligence.models.AnalyzeResultOperation;
import com.azure.ai.documentintelligence.models.DocumentAnalysisFeature;
import com.azure.ai.documentintelligence.models.DocumentBarcode;
import com.azure.ai.documentintelligence.models.DocumentPage;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.util.polling.SyncPoller;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
/**
* This sample demonstrates how to extract all identified barcodes using the add-on 'BARCODES' capability.
* Add-on capabilities are available within all models except for the Business card model.
* This sample uses Layout model to demonstrate.
*/
public class AnalyzeAddOnBarcodes {
/**
* Main method to invoke this demo.
*
* @param args Unused. Arguments to the program.
* @throws IOException Exception thrown when there is an error in reading all the bytes from the File.
*/
public static void main(final String[] args) throws IOException {
// Instantiate a client that will be used to call the service.
DocumentIntelligenceClient client = new DocumentIntelligenceClientBuilder()
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildClient();
File barcodesDocument = new File("../documentintelligence/azure-ai-documentintelligence/src/samples/resources/"
+ "sample-forms/addOns/barcodes.jpg");
SyncPoller<AnalyzeResultOperation, AnalyzeResultOperation> analyzeLayoutResultPoller =
client.beginAnalyzeDocument("prebuilt-layout", null,
null,
null,
Arrays.asList(DocumentAnalysisFeature.BARCODES),
null,
null,
new AnalyzeDocumentRequest().setBase64Source(Files.readAllBytes(barcodesDocument.toPath())));
AnalyzeResult analyzeLayoutResult = analyzeLayoutResultPoller.getFinalResult().getAnalyzeResult();
// pages
List<DocumentPage> pages = analyzeLayoutResult.getPages();
for (int i = 0; i < pages.size(); i++) {
DocumentPage documentPage = pages.get(i);
System.out.printf("----Barcodes detected from page #%d----%n", i);
List<DocumentBarcode> barcodes = documentPage.getBarcodes();
System.out.printf("Detected %d barcodes:%n", barcodes.size());
for (int j = 0; j < barcodes.size(); j++) {
DocumentBarcode documentBarcode = barcodes.get(j);
System.out.printf("- Barcode %d: %s%n", j, documentBarcode.getValue());
System.out.printf(" Kind: %s%n", documentBarcode.getKind());
System.out.printf(" Confidence: %.2f%n", documentBarcode.getConfidence());
System.out.printf(" Bounding regions: %s%n", documentBarcode.getPolygon());
}
}
}
}