Using Cloud Vision with Spring framework

Spring Cloud GCP offers convenient libraries to interface with Cloud Vision from a Spring application. These libraries include Auto-Configuration and helper classes and Spring Boot Template classes to allow developers to get started with Cloud Vision quickly.

If you're already familiar with the Spring Framework, then Spring Cloud Vision can make it easier to work with the Vision API in your application and reduce the amount of code that you need to write.

This page explains how to add Spring Cloud Vision to a Java application. For detailed information about the module, see the Spring Cloud Vision reference.

Dependency setup

To begin using this library, add the spring-cloud-gcp-starter-vision artifact to your project.

Maven coordinates, using Spring Cloud GCP BOM:


You must also create a service account and use the service account key to authenticate with Google Cloud Platform.

For more information, see the instructions for setting up a Java development environment. You do not need to install the Google Cloud Platform Client Library for Java; the Spring Boot starter installs the client library automatically.

Image analysis

After configuring the Spring Cloud GCP Vision dependencies on your classpath, you can immediately begin processing your images by getting an instance of CloudVisionTemplate using Spring dependency injection.

private CloudVisionTemplate cloudVisionTemplate;

The CloudVisionTemplate is a wrapper around the Cloud Vision Client Libraries and lets you process images easily through the Cloud Vision API. For more information about the CloudVisionTemplate features, see the Cloud Vision template reference page.

The following sections contain code samples for common use cases of the CloudVisionTemplate. All code snippets come from the Spring and Cloud Vision sample application.

Getting the classification labels for an image

The code below extracts the classification labels for an image, providing you with general descriptions of image content.

AnnotateImageResponse response = this.cloudVisionTemplate.analyzeImage(
    this.resourceLoader.getResource(imageUrl), Type.LABEL_DETECTION);

Map<String, Float> imageLabels =
                (u, v) -> {
                  throw new IllegalStateException(String.format("Duplicate key %s", u));

Extracting the Text In an Image

The code sample below describes another common operation of extracting the text from an image.

String textFromImage = this.cloudVisionTemplate.extractTextFromImage(
return "Text from image: " + textFromImage;

What's next

Apakah halaman ini membantu? Beri tahu kami pendapat Anda:

Kirim masukan tentang...

Cloud Vision API Documentation
Butuh bantuan? Kunjungi halaman dukungan kami.