/
AnalyzeAddOnFonts.java
171 lines (148 loc) · 7.5 KB
/
AnalyzeAddOnFonts.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// 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.DocumentStyle;
import com.azure.ai.documentintelligence.models.FontStyle;
import com.azure.ai.documentintelligence.models.FontWeight;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.util.CoreUtils;
import com.azure.core.util.polling.SyncPoller;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* This sample demonstrates how to extract all identified barcodes using the add-on 'STYLE_FONT' capability.
* Add-on capabilities are available within all models except for the Business card model.
* This sample uses Layout model to demonstrate.
*/
public class AnalyzeAddOnFonts {
/**
* 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 document = new File("../documentintelligence/azure-ai-documentintelligence/src/samples/resources/"
+ "sample-forms/addOns/fonts_and_languages.png");
SyncPoller<AnalyzeResultOperation, AnalyzeResultOperation> analyzeLayoutResultPoller =
client.beginAnalyzeDocument("prebuilt-layout", null,
null,
null,
Arrays.asList(DocumentAnalysisFeature.STYLE_FONT),
null,
null,
new AnalyzeDocumentRequest().setBase64Source(Files.readAllBytes(document.toPath())));
AnalyzeResult analyzeLayoutResult = analyzeLayoutResultPoller.getFinalResult().getAnalyzeResult();
// DocumentStyle has the following font related attributes:
Map<String, List<DocumentStyle>> similarFontFamilies = new HashMap<>(); // e.g., 'Arial, sans-serif
Map<FontStyle, List<DocumentStyle>> fontStyles = new HashMap<>(); // e.g, 'italic'
Map<FontWeight, List<DocumentStyle>> fontWeights = new HashMap<>(); // e.g., 'bold'
Map<String, List<DocumentStyle>> fontColors = new HashMap<>(); // in '#rrggbb' hexadecimal format
Map<String, List<DocumentStyle>> fontBackgroundColors = new HashMap<>(); // in '#rrggbb' hexadecimal format
// styles
List<DocumentStyle> documentStyles = analyzeLayoutResult.getStyles();
// Content
String content = analyzeLayoutResult.getContent();
boolean isDocumentContainsHandwritten = documentStyles.stream().anyMatch(documentStyle -> {
Boolean handwritten = documentStyle.isHandwritten();
return handwritten != null && handwritten;
});
if (isDocumentContainsHandwritten) {
System.out.println("Document contains handwritten content");
} else {
System.out.println("Document does not contains handwritten content");
}
System.out.println("----Fonts styles detected in the document----");
// Iterate over the styles and group them by their font attributes.
documentStyles.forEach(documentStyle -> {
String similarFontFamily = documentStyle.getSimilarFontFamily();
if (!CoreUtils.isNullOrEmpty(similarFontFamily)) {
if (!similarFontFamilies.containsKey(similarFontFamily)) {
similarFontFamilies.put(similarFontFamily, new ArrayList<>());
}
similarFontFamilies.get(similarFontFamily).add(documentStyle);
}
FontStyle fontStyle = documentStyle.getFontStyle();
if (fontStyle != null) {
if (!fontStyles.containsKey(fontStyle)) {
fontStyles.put(fontStyle, new ArrayList<>());
}
fontStyles.get(fontStyle).add(documentStyle);
}
FontWeight fontWeight = documentStyle.getFontWeight();
if (fontWeight != null) {
if (!fontWeights.containsKey(fontWeight)) {
fontWeights.put(fontWeight, new ArrayList<>());
}
fontWeights.get(fontWeight).add(documentStyle);
}
String fontColor = documentStyle.getColor();
if (!CoreUtils.isNullOrEmpty(fontColor)) {
if (!fontColors.containsKey(fontColor)) {
fontColors.put(fontColor, new ArrayList<>());
}
fontColors.get(fontColor).add(documentStyle);
}
String backgroundColor = documentStyle.getBackgroundColor();
if (!CoreUtils.isNullOrEmpty(backgroundColor)) {
if (!fontBackgroundColors.containsKey(backgroundColor)) {
fontBackgroundColors.put(backgroundColor, new ArrayList<>());
}
fontBackgroundColors.get(backgroundColor).add(documentStyle);
}
});
System.out.printf("Detected %d font families: %n", similarFontFamilies.size());
similarFontFamilies.forEach((fontFamily, styles) -> {
System.out.println("- Font family: " + fontFamily);
printSpan(styles, content);
});
System.out.printf("Detected %d font styles: %n", fontStyles.size());
fontStyles.forEach((fontStyle, styles) -> {
System.out.println("- Font style: " + fontStyle);
printSpan(styles, content);
});
System.out.printf("Detected %d font weights: %n", fontWeights.size());
fontWeights.forEach((fontWeight, styles) -> {
System.out.println("- Font weight: " + fontWeight);
printSpan(styles, content);
});
System.out.printf("Detected %d font colors: %n", fontColors.size());
fontColors.forEach((fontColor, styles) -> {
System.out.println("- Font color: " + fontColor);
printSpan(styles, content);
});
System.out.printf("Detected %d font background colors: %n", fontBackgroundColors.size());
fontBackgroundColors.forEach((fontBackgroundColor, styles) -> {
System.out.println("- Font background color: " + fontBackgroundColor);
printSpan(styles, content);
});
}
public static void printSpan(List<DocumentStyle> styles, String content) {
for (DocumentStyle style : styles) {
style.getSpans().forEach(
span -> {
int offset = span.getOffset();
int length = span.getLength();
System.out.printf(" content=\"%s\", offset=%d, length=%d%n",
content.substring(offset, offset + length),
offset,
length);
});
}
}
}