|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package documentai.v1beta3; |
| 18 | + |
| 19 | +// [START documentai_process_ocr_document] |
| 20 | + |
| 21 | +import com.google.cloud.documentai.v1beta3.Document; |
| 22 | +import com.google.cloud.documentai.v1beta3.DocumentProcessorServiceClient; |
| 23 | +import com.google.cloud.documentai.v1beta3.ProcessRequest; |
| 24 | +import com.google.cloud.documentai.v1beta3.ProcessResponse; |
| 25 | +import com.google.cloud.documentai.v1beta3.RawDocument; |
| 26 | +import com.google.protobuf.ByteString; |
| 27 | +import java.io.IOException; |
| 28 | +import java.nio.file.Files; |
| 29 | +import java.nio.file.Paths; |
| 30 | +import java.util.List; |
| 31 | +import java.util.concurrent.ExecutionException; |
| 32 | +import java.util.concurrent.TimeoutException; |
| 33 | + |
| 34 | +public class ProcessOcrDocument { |
| 35 | + public static void processOcrDocument() |
| 36 | + throws IOException, InterruptedException, ExecutionException, TimeoutException { |
| 37 | + // TODO(developer): Replace these variables before running the sample. |
| 38 | + String projectId = "your-project-id"; |
| 39 | + String location = "your-project-location"; // Format is "us" or "eu". |
| 40 | + String processerId = "your-processor-id"; |
| 41 | + String filePath = "path/to/input/file.pdf"; |
| 42 | + processOcrDocument(projectId, location, processerId, filePath); |
| 43 | + } |
| 44 | + |
| 45 | + public static void processOcrDocument( |
| 46 | + String projectId, String location, String processorId, String filePath) |
| 47 | + throws IOException, InterruptedException, ExecutionException, TimeoutException { |
| 48 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 49 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 50 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 51 | + try (DocumentProcessorServiceClient client = DocumentProcessorServiceClient.create()) { |
| 52 | + // The full resource name of the processor, e.g.: |
| 53 | + // projects/project-id/locations/location/processor/processor-id |
| 54 | + // You must create new processors in the Cloud Console first |
| 55 | + String name = |
| 56 | + String.format("projects/%s/locations/%s/processors/%s", projectId, location, processorId); |
| 57 | + |
| 58 | + // Read the file. |
| 59 | + byte[] imageFileData = Files.readAllBytes(Paths.get(filePath)); |
| 60 | + |
| 61 | + // Convert the image data to a Buffer and base64 encode it. |
| 62 | + ByteString content = ByteString.copyFrom(imageFileData); |
| 63 | + |
| 64 | + RawDocument document = |
| 65 | + RawDocument.newBuilder().setContent(content).setMimeType("application/pdf").build(); |
| 66 | + |
| 67 | + // Configure the process request. |
| 68 | + ProcessRequest request = |
| 69 | + ProcessRequest.newBuilder().setName(name).setRawDocument(document).build(); |
| 70 | + |
| 71 | + // Recognizes text entities in the PDF document |
| 72 | + ProcessResponse result = client.processDocument(request); |
| 73 | + Document documentResponse = result.getDocument(); |
| 74 | + |
| 75 | + System.out.println("Document processing complete."); |
| 76 | + |
| 77 | + // Read the text recognition output from the processor |
| 78 | + // For a full list of Document object attributes, |
| 79 | + // please reference this page: |
| 80 | + // https://googleapis.dev/java/google-cloud-document-ai/latest/index.html |
| 81 | + |
| 82 | + // Get all of the document text as one big string |
| 83 | + String text = documentResponse.getText(); |
| 84 | + System.out.printf("Full document text: '%s'\n", escapeNewlines(text)); |
| 85 | + |
| 86 | + // Read the text recognition output from the processor |
| 87 | + List<Document.Page> pages = documentResponse.getPagesList(); |
| 88 | + System.out.printf("There are %s page(s) in this document.\n", pages.size()); |
| 89 | + |
| 90 | + for (Document.Page page : pages) { |
| 91 | + System.out.printf("Page %d:\n", page.getPageNumber()); |
| 92 | + printPageDimensions(page.getDimension()); |
| 93 | + printDetectedLanguages(page.getDetectedLanguagesList()); |
| 94 | + printParagraphs(page.getParagraphsList(), text); |
| 95 | + printBlocks(page.getBlocksList(), text); |
| 96 | + printLines(page.getLinesList(), text); |
| 97 | + printTokens(page.getTokensList(), text); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private static void printPageDimensions(Document.Page.Dimension dimension) { |
| 103 | + String unit = dimension.getUnit(); |
| 104 | + System.out.printf(" Width: %.1f %s\n", dimension.getWidth(), unit); |
| 105 | + System.out.printf(" Height: %.1f %s\n", dimension.getHeight(), unit); |
| 106 | + } |
| 107 | + |
| 108 | + private static void printDetectedLanguages( |
| 109 | + List<Document.Page.DetectedLanguage> detectedLangauges) { |
| 110 | + System.out.println(" Detected languages:"); |
| 111 | + for (Document.Page.DetectedLanguage detectedLanguage : detectedLangauges) { |
| 112 | + String languageCode = detectedLanguage.getLanguageCode(); |
| 113 | + float confidence = detectedLanguage.getConfidence(); |
| 114 | + System.out.printf(" %s (%.2f%%)\n", languageCode, confidence * 100.0); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private static void printParagraphs(List<Document.Page.Paragraph> paragraphs, String text) { |
| 119 | + System.out.printf(" %d paragraphs detected:\n", paragraphs.size()); |
| 120 | + Document.Page.Paragraph firstParagraph = paragraphs.get(0); |
| 121 | + String firstParagraphText = getLayoutText(firstParagraph.getLayout().getTextAnchor(), text); |
| 122 | + System.out.printf(" First paragraph text: %s\n", escapeNewlines(firstParagraphText)); |
| 123 | + Document.Page.Paragraph lastParagraph = paragraphs.get(paragraphs.size() - 1); |
| 124 | + String lastParagraphText = getLayoutText(lastParagraph.getLayout().getTextAnchor(), text); |
| 125 | + System.out.printf(" Last paragraph text: %s\n", escapeNewlines(lastParagraphText)); |
| 126 | + } |
| 127 | + |
| 128 | + private static void printBlocks(List<Document.Page.Block> blocks, String text) { |
| 129 | + System.out.printf(" %d blocks detected:\n", blocks.size()); |
| 130 | + Document.Page.Block firstBlock = blocks.get(0); |
| 131 | + String firstBlockText = getLayoutText(firstBlock.getLayout().getTextAnchor(), text); |
| 132 | + System.out.printf(" First block text: %s\n", escapeNewlines(firstBlockText)); |
| 133 | + Document.Page.Block lastBlock = blocks.get(blocks.size() - 1); |
| 134 | + String lastBlockText = getLayoutText(lastBlock.getLayout().getTextAnchor(), text); |
| 135 | + System.out.printf(" Last block text: %s\n", escapeNewlines(lastBlockText)); |
| 136 | + } |
| 137 | + |
| 138 | + private static void printLines(List<Document.Page.Line> lines, String text) { |
| 139 | + System.out.printf(" %d lines detected:\n", lines.size()); |
| 140 | + Document.Page.Line firstLine = lines.get(0); |
| 141 | + String firstLineText = getLayoutText(firstLine.getLayout().getTextAnchor(), text); |
| 142 | + System.out.printf(" First line text: %s\n", escapeNewlines(firstLineText)); |
| 143 | + Document.Page.Line lastLine = lines.get(lines.size() - 1); |
| 144 | + String lastLineText = getLayoutText(lastLine.getLayout().getTextAnchor(), text); |
| 145 | + System.out.printf(" Last line text: %s\n", escapeNewlines(lastLineText)); |
| 146 | + } |
| 147 | + |
| 148 | + private static void printTokens(List<Document.Page.Token> tokens, String text) { |
| 149 | + System.out.printf(" %d tokens detected:\n", tokens.size()); |
| 150 | + Document.Page.Token firstToken = tokens.get(0); |
| 151 | + String firstTokenText = getLayoutText(firstToken.getLayout().getTextAnchor(), text); |
| 152 | + System.out.printf(" First token text: %s\n", escapeNewlines(firstTokenText)); |
| 153 | + Document.Page.Token lastToken = tokens.get(tokens.size() - 1); |
| 154 | + String lastTokenText = getLayoutText(lastToken.getLayout().getTextAnchor(), text); |
| 155 | + System.out.printf(" Last token text: %s\n", escapeNewlines(lastTokenText)); |
| 156 | + } |
| 157 | + |
| 158 | + // Extract shards from the text field |
| 159 | + private static String getLayoutText(Document.TextAnchor textAnchor, String text) { |
| 160 | + if (textAnchor.getTextSegmentsList().size() > 0) { |
| 161 | + int startIdx = (int) textAnchor.getTextSegments(0).getStartIndex(); |
| 162 | + int endIdx = (int) textAnchor.getTextSegments(0).getEndIndex(); |
| 163 | + return text.substring(startIdx, endIdx); |
| 164 | + } |
| 165 | + return "[NO TEXT]"; |
| 166 | + } |
| 167 | + |
| 168 | + private static String escapeNewlines(String s) { |
| 169 | + return s.replace("\n", "\\n").replace("\r", "\\r"); |
| 170 | + } |
| 171 | +} |
| 172 | +// [END documentai_process_ocr_document] |
0 commit comments