So you want to use the Google Cloud Vision API from Android? Then this sample is for you. It's a minimal single-activity sample that shows you how to make a call to the Cloud Vision API with an image picked from your device's gallery.
- An API key for the Cloud Vision API (See the docs to learn more)
- An Android device running Android 5.0 or higher
- Android Studio, with a recent version of the Android SDK.
- Download the
CloudVisiondirectory from this repository. - In Android Studio, open the
CloudVisiondirectory as an existing Android Studio project. - Open
MainActivity.javaand set the constantCLOUD_VISION_API_KEYto the API key obtained above. - Run the sample.
-
As with all Google Cloud APIs, every call to the Vision API must be associated with a project within the Google Cloud Console that has the Vision API enabled. This is described in more detail in the getting started doc, but in brief:
- Create a project (or use an existing one) in the Cloud Console
- Enable billing and the Vision API.
- Create an API key, and save this for later.
-
Download the
CloudVisiondirectory from this repository.The easiest way to do this from GitHub is to fetch the entire repository. If you have
gitinstalled, you can do this by executing the following command:$ git clone https://github.com/GoogleCloudPlatform/cloud-vision.gitThis will download the repository of samples into the directory
cloud-vision.Otherwise, GitHub offers an auto-generated zip file of the
masterbranch, which you can download and extract.Either method will include the desired directory at
cloud-vision/android/CloudVision. -
Open Android Studio, and navigate to open an existing project. When prompted, open this project's root directory
cloud-vision/android/CloudVision. -
Within Android Studio, open the
MainActivityjava file within the app, and look for where the constantCLOUD_VISION_API_KEYis set. Replace the string value with the api key obtained from the cloud console above.This constant is the credential used in the
callCloudVisionmethod to authenticate all requests to the Vision API. Calls to the API are thus associated with the project you created above, for access and billing purposes. -
You are now ready to build and run the project. As per usual for Android Studio projects, you can run the project by clicking the green 'Play' button, or going to
Run|Run 'app'. The app should build and run on your connected Android device or emulator. -
You will be presented with the single-activity app.
- Click the floating red action button to select the image to send to the API.
- This executes the
onClickListenerthat's set inonCreate, which creates anIntentfor selecting the content to send to the API.
- This executes the
- Select an image from your device.
- Control is handed back to the
MainActivityand is handled byonActivityResult, which does a little bit of processing on the selected image and hands it to thecallCloudVisionmethod. - The
callCloudVisionmethod creates and executes a label detection request to the Vision API using thegoogle-api-services-visionjava client library. This library is declared as a dependency inapp/build.gradle, and is used to simplify making API calls to the Google Cloud Vision API. - Notice that the
CLOUD_VISION_API_KEYis set as part of this request, to authenticate the request and associate it with your project.
- Control is handed back to the
- When the API responds, the
convertResponseToStringmethod extracts the labels from the response object and constructs aStringto display. - The
onPostExecutecallback fires, with the constructed result, and populates theimage_detailsTextViewwith the labels returned by the Vision API.
- Click the floating red action button to select the image to send to the API.