Ниже приведен код, который я использовал для распознавания текста Билла.
Я скачал credential.json из аккаунта Google.
Кроме того, я не начал свою платежную учетную запись Google Cloud.
Может ли кто-нибудь помочь мне все условия для использования Google OCR?
public class QuickstartSample {
public static void main(String[] args) throws Exception {
// Instantiates a client
try {
ImageAnnotatorClient vision = ImageAnnotatorClient.create();
// The path to the image file to annotate
String fileName = "Bill.jpg";
// Reads the image file into memory
Path path = Paths.get(fileName);
byte[] data = Files.readAllBytes(path);
ByteString imgBytes = ByteString.copyFrom(data);
// Builds the image annotation request
List<AnnotateImageRequest> requests = new ArrayList<>();
Image img = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder()
.addFeatures(feat)
.setImage(img)
.build();
requests.add(request);
// Performs label detection on the image file
BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();
for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
// System.out.printf("Error: %s\n", res.getError().getMessage());
return;
}
/* for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
annotation.getAllFields().forEach((k, v) ->
System.out.printf("%s : %s\n", k, v.toString()));
}*/
}
}
catch(Exception e)
{
}
}
}