Let’s talk about Google Vision API and how to write a simple java program to extract Text from an Image

By registering for Google Cloud’s vision API you can access just that with an API key.

<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">





import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.vision.v1.*;
import com.google.cloud.vision.v1.Feature.Type;
import com.google.common.collect.Lists;
import com.google.protobuf.ByteString;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TextExtractionDemo {
public static void main(String... args) throws Exception {

GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("<PATH_TO_API_KEY_FILE>"))
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

detectDocumentText("<PATH_TO_IMAGE_FILE>", credentials);

public static void detectDocumentText(String filePath, GoogleCredentials credentials) throws IOException {
List<AnnotateImageRequest> requests = new ArrayList<>();

ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));

Image img = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Type.DOCUMENT_TEXT_DETECTION).build();
AnnotateImageRequest request =

ImageAnnotatorSettings imageAnnotatorSettings =

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (ImageAnnotatorClient client = ImageAnnotatorClient.create(imageAnnotatorSettings)) {
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
System.out.format("Error: %s%n", res.getError().getMessage());

// For full list of available annotations, see http://g.co/cloud/vision/docs
TextAnnotation annotation = res.getFullTextAnnotation();
for (Page page : annotation.getPagesList()) {
String pageText = "";
for (Block block : page.getBlocksList()) {
String blockText = "";
for (Paragraph para : block.getParagraphsList()) {
String paraText = "";
for (Word word : para.getWordsList()) {
String wordText = "";
for (Symbol symbol : word.getSymbolsList()) {
wordText = wordText + symbol.getText();
blockText = blockText + paraText;
pageText = pageText + blockText;
System.out.println("Complete annotation:");
  • Face detection
  • Landmark detection
  • Label detection
  • OCR for handwritten text




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A little caution never hurt anyone. So double-check these things before your chatbot launch.


Figma plugin sneak peek

Top 3 Skills you need to learn for Cloud

#CSENotes 4. Software Engineering

Parse JSON response in Golang

How to run queries faster in Google’s BigQuery without changing queries or schema ?

Python Operators — Certainly Not Very Interesting, But Very Important

Create Windows Services In C#

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ritesh Shergill

Ritesh Shergill

More from Medium



Aha Project Management Vs Miro Project Manager Comparison

An Introduction to Search Engine Optimization (SEO).