|
| 1 | +/* |
| 2 | + * Copyright 2019 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 com.example.vision; |
| 18 | + |
| 19 | +import com.google.cloud.vision.v1.AnnotateImageRequest; |
| 20 | +import com.google.cloud.vision.v1.AnnotateImageResponse; |
| 21 | +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; |
| 22 | +import com.google.cloud.vision.v1.EntityAnnotation; |
| 23 | +import com.google.cloud.vision.v1.Feature; |
| 24 | +import com.google.cloud.vision.v1.Image; |
| 25 | +import com.google.cloud.vision.v1.ImageAnnotatorClient; |
| 26 | +import com.google.cloud.vision.v1.ImageAnnotatorSettings; |
| 27 | +import com.google.cloud.vision.v1.ImageSource; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +class SetEndpoint { |
| 34 | + |
| 35 | + // Change your endpoint |
| 36 | + static void setEndpoint() throws IOException { |
| 37 | + // [START vision_set_endpoint] |
| 38 | + ImageAnnotatorSettings settings = |
| 39 | + ImageAnnotatorSettings.newBuilder().setEndpoint("eu-vision.googleapis.com:443").build(); |
| 40 | + |
| 41 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 42 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 43 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 44 | + ImageAnnotatorClient client = ImageAnnotatorClient.create(settings); |
| 45 | + // [END vision_set_endpoint] |
| 46 | + |
| 47 | + ImageSource imgSource = |
| 48 | + ImageSource.newBuilder() |
| 49 | + .setGcsImageUri("gs://cloud-samples-data/vision/text/screen.jpg") |
| 50 | + .build(); |
| 51 | + Image image = Image.newBuilder().setSource(imgSource).build(); |
| 52 | + Feature feature = Feature.newBuilder().setType(Feature.Type.TEXT_DETECTION).build(); |
| 53 | + AnnotateImageRequest request = |
| 54 | + AnnotateImageRequest.newBuilder().addFeatures(feature).setImage(image).build(); |
| 55 | + List<AnnotateImageRequest> requests = new ArrayList<>(); |
| 56 | + requests.add(request); |
| 57 | + |
| 58 | + BatchAnnotateImagesResponse batchResponse = client.batchAnnotateImages(requests); |
| 59 | + |
| 60 | + for (AnnotateImageResponse response : batchResponse.getResponsesList()) { |
| 61 | + for (EntityAnnotation annotation : response.getTextAnnotationsList()) { |
| 62 | + System.out.printf("Text: %s\n", annotation.getDescription()); |
| 63 | + System.out.println("Position:"); |
| 64 | + System.out.printf("%s\n", annotation.getBoundingPoly()); |
| 65 | + } |
| 66 | + } |
| 67 | + client.close(); |
| 68 | + } |
| 69 | +} |
0 commit comments