[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Using Vision with Spring framework\n\n[Spring Cloud Google Cloud](https://spring.io/projects/spring-cloud-gcp) offers convenient libraries\nto interface with the Vision API from a Spring application. These libraries\ninclude\n[Auto-Configuration and helper classes](https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html)\nand Spring Boot Template classes to allow developers to get started\nwith the Vision API quickly.\n\nIf you're already familiar with the\n[Spring Framework](https://spring.io/projects/spring-framework), then\nSpring Cloud Vision can\nmake it easier to work with the Vision API in your application and\nreduce the amount of code that you need to write.\n\nThis page explains how to add Spring Cloud Vision to a Java\napplication. For detailed information about the module, see the\n[Spring Cloud Vision reference](https://googlecloudplatform.github.io/spring-cloud-gcp/4.1.3/reference/html/index.html#cloud-vision).\n\nDependency setup\n----------------\n\nTo begin using this library, add the `spring-cloud-gcp-starter-vision` artifact\nto your project.\n\nMaven coordinates, using Spring Cloud Google Cloud BOM: \n\n \u003cdependencyManagement\u003e\n \u003cdependencies\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003eorg.springframework.cloud\u003c/groupId\u003e\n \u003cartifactId\u003espring-cloud-gcp-dependencies\u003c/artifactId\u003e\n \u003cversion\u003e1.2.8.RELEASE\u003c/version\u003e\n \u003ctype\u003epom\u003c/type\u003e\n \u003cscope\u003eimport\u003c/scope\u003e\n \u003c/dependency\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\n \u003cartifactId\u003espring-boot-dependencies\u003c/artifactId\u003e\n \u003cversion\u003e${spring.version}\u003c/version\u003e\n \u003ctype\u003epom\u003c/type\u003e\n \u003cscope\u003eimport\u003c/scope\u003e\n \u003c/dependency\u003e\n \u003c/dependencies\u003e\n \u003c/dependencyManagement\u003e\n\n \u003cdependency\u003e\n \u003cgroupId\u003eorg.springframework.cloud\u003c/groupId\u003e\n \u003cartifactId\u003espring-cloud-gcp-starter-vision\u003c/artifactId\u003e\n \u003c/dependency\u003e\n\nFor more information, see the instructions for [setting up a Java development\nenvironment](/java/docs/setup). You do not need to install the Google Cloud Client\nLibrary for Java; the Spring Boot starter installs the client library\nautomatically.\n\nImage analysis\n--------------\n\nAfter configuring the Spring Cloud Google Cloud Vision dependencies on your\nclasspath, you can immediately begin processing your images by getting\nan instance of `CloudVisionTemplate` using [Spring dependency injection](https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-spring-beans-and-dependency-injection.html). \n\n @Autowired private CloudVisionTemplate cloudVisionTemplate;\n\nThe `CloudVisionTemplate` is a wrapper around the Vision API\nClient Libraries and lets you process images easily through the\nVision API.\nFor more information about the `CloudVisionTemplate` features, see\nthe [Cloud Vision template reference page](https://googlecloudplatform.github.io/spring-cloud-gcp/4.1.3/reference/html/index.html#cloud-vision).\n\nThe following sections contain code samples for common use cases of\nthe `CloudVisionTemplate`. All code snippets come from the [Spring and\nCloud Vision sample application](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/vision/spring-framework).\n\n### Getting the classification labels for an image\n\nThe code below extracts the classification labels for an image, providing you\nwith general descriptions of image content. \n\n AnnotateImageResponse response =\n this.cloudVisionTemplate.analyzeImage(\n this.resourceLoader.getResource(imageUrl), Type.LABEL_DETECTION);\n\n Map\u003cString, Float\u003e imageLabels =\n response.getLabelAnnotationsList().stream()\n .collect(\n Collectors.toMap(\n EntityAnnotation::getDescription,\n EntityAnnotation::getScore,\n (u, v) -\u003e {\n throw new IllegalStateException(String.format(\"Duplicate key %s\", u));\n },\n LinkedHashMap::new));\n\n### Extracting the Text In an Image\n\nThe code sample below describes another common operation of extracting the text\nfrom an image. \n\n String textFromImage =\n this.cloudVisionTemplate.extractTextFromImage(this.resourceLoader.getResource(imageUrl));\n return \"Text from image: \" + textFromImage;\n\nWhat's next\n-----------\n\n- [Get started with\n Spring Cloud Google Cloud](https://googlecloudplatform.github.io/spring-cloud-gcp/4.1.3/reference/html/index.html#getting-started).\n- Learn more about [using Spring Cloud Vision in your\n applications](https://googlecloudplatform.github.io/spring-cloud-gcp/4.1.3/reference/html/index.html#cloud-vision).\n- [File a GitHub issue](https://github.com/GoogleCloudPlatform/spring-cloud-gcp/issues) to report a bug or ask a question about the module.\n- Get more information about [Spring Framework support on\n Google Cloud](/java/docs/reference/spring).\n- Try a codelab to [deploy and run an application that uses\n Spring Cloud Google Cloud](https://codelabs.developers.google.com/spring/)."]]