如何授权google云视APIandroid

How to authorize google cloud vision API android

我正在从这个 link 实施 google 云视觉 API。

下面是我遇到异常的代码:

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();
    }

我遇到错误

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.

我的毕业典礼

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'
}

如错误消息所述,应用程序默认凭据在 Android 上不可用。我建议使用 API 密钥来轻松验证您对 Cloud Vision API 的请求。您将在 Github page.

上找到示例代码