javaandroidgoogle-cloud-platformgoogle-cloud-vision

How to authorize google cloud vision API android


I am implementing google cloud vision API from this link.

Below is my code where I am getting exception:

private void callCloudVision(final Bitmap bitmap) throws IOException {
    new AsyncTask<Object, Void, String>() {
        @Override
        protected String doInBackground(Object... params) {
            try {
                List<AnnotateImageRequest> requests = new ArrayList<>();
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
                byte[] bitmapdata = bos.toByteArray();
                ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
                ByteString imgBytes = ByteString.readFrom(bs);
                Image img = Image.newBuilder().setContent(imgBytes).build();
                Feature feat = Feature.newBuilder().setType(Feature.Type.WEB_DETECTION).build();
                AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
                requests.add(request);

                ***//Getting exception here at the time of execution...***
                try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
                    BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
                    List<AnnotateImageResponse> responses = response.getResponsesList();
                    for (AnnotateImageResponse res : responses) {
                        if (res.hasError()) {
                            System.out.printf("Error: %s\n", res.getError().getMessage());
                            return "";
                        }
                        System.out.println("\nPages with matching images: Score\n==");
                        for (WebDetection.WebPage page : annotation.getPagesWithMatchingImagesList()) {
                            System.out.println(page.getUrl() + " : " + page.getScore());
                        }
                    }
                }

                return "";

            }  catch (Exception e) {
                Log.d("LOG_TAG", "Request failed: " + e.getMessage());
                return "Cloud Vision API request failed.";
            }

        }

        protected void onPostExecute(String result) {

        }
    }.execute();
    }

I am getting error

java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.

My graddle

implementation 'com.android.support.constraint:constraint-layout:1.0.2'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:26.+'
**compile 'com.google.cloud:google-cloud-vision:1.21.0'**
compile group: 'com.google.apis', name: 'google-api-services-sqladmin', version: 'v1beta4-rev52-1.23.0'
compile('com.google.api-client:google-api-client-android:1.22.0') {
    exclude module: 'httpclient'
    exclude group: 'com.google.guava'
}
compile('com.google.http-client:google-http-client-gson:1.20.0') {
    exclude module: 'httpclient'
    exclude group: 'com.google.guava'
}

Solution

  • As stated by the error message, Application Default Credentials are not available on Android. I'd recommend to use an API key to easily authenticate your requests to the Cloud Vision API. You'll find sample code on this Github page.