Package com.google.cloud.spring.vision
Class DocumentOcrTemplate
java.lang.Object
com.google.cloud.spring.vision.DocumentOcrTemplate
Template providing convenient operations for interfacing with Google Cloud Vision's Document OCR
feature, which allows you to run OCR algorithms on documents (PDF or TIFF format) stored on
Google Cloud Storage.
-
Constructor Summary
ConstructorsConstructorDescriptionDocumentOcrTemplate(com.google.cloud.vision.v1.ImageAnnotatorClient imageAnnotatorClient, com.google.cloud.storage.Storage storage, Executor executor, int jsonOutputBatchSize) -
Method Summary
Modifier and TypeMethodDescriptionreadOcrOutputFile(GoogleStorageLocation jsonFile) Parses a single JSON output file and returns the list of pages stored in the file.readOcrOutputFileSet(GoogleStorageLocation jsonOutputFilePathPrefix) Parses the OCR output files who have the specifiedjsonFilesetPrefix.runOcrForDocument(GoogleStorageLocation document, GoogleStorageLocation outputFilePathPrefix) Runs OCR processing for a specifieddocumentand generates OCR output files under the path specified byoutputFilePathPrefix.
-
Constructor Details
-
DocumentOcrTemplate
public DocumentOcrTemplate(com.google.cloud.vision.v1.ImageAnnotatorClient imageAnnotatorClient, com.google.cloud.storage.Storage storage, Executor executor, int jsonOutputBatchSize)
-
-
Method Details
-
runOcrForDocument
public CompletableFuture<DocumentOcrResultSet> runOcrForDocument(GoogleStorageLocation document, GoogleStorageLocation outputFilePathPrefix) Runs OCR processing for a specifieddocumentand generates OCR output files under the path specified byoutputFilePathPrefix.For example, if you specify an
outputFilePathPrefixof "gs://bucket_name/ocr_results/myDoc_", all the output files of OCR processing will be saved under prefix, such as:- gs://bucket_name/ocr_results/myDoc_output-1-to-5.json
- gs://bucket_name/ocr_results/myDoc_output-6-to-10.json
- gs://bucket_name/ocr_results/myDoc_output-11-to-15.json
Note: OCR processing operations may take several minutes to complete, so it may not be advisable to block on the completion of the operation. One may use the returned
CompletableFutureto register callbacks or track the status of the operation.- Parameters:
document- TheGoogleStorageLocationof the document to run OCR processingoutputFilePathPrefix- TheGoogleStorageLocationof a file, folder, or a bucket describing the path for which all output files shall be saved under- Returns:
- A
CompletableFutureallowing you to register callbacks or wait for the completion of the operation.
-
readOcrOutputFileSet
Parses the OCR output files who have the specifiedjsonFilesetPrefix. This method assumes that all of the OCR output files with the prefix are a part of the same document.- Parameters:
jsonOutputFilePathPrefix- the folder location containing all of the JSON files of OCR output- Returns:
- A
DocumentOcrResultSetdescribing the OCR content of a document
-
readOcrOutputFile
Parses a single JSON output file and returns the list of pages stored in the file.Each page of the document is represented as a
TextAnnotationwhich contains the parsed OCR data.- Parameters:
jsonFile- the location of the JSON output file- Returns:
- the list of
TextAnnotationcontaining the OCR results - Throws:
RuntimeException- if the JSON file cannot be deserialized into aTextAnnotationobject
-