我正在开发Android应用程序,该应用程序使用java中的谷歌视觉api和wolfram-alpha-api来显示数学方程的求解,但我认为错误是云视觉api请求失败。查看日志以了解详细信息。如何修复此错误。我的毕业设计需要你的帮助。请帮帮我。
private void callCloudVision(final Bitmap bitmap) throws IOException {
// Do the real work in an async task, because we need to use the network anyway
new AsyncTask<Object, Void, String>() {
@Override
protected String doInBackground(Object... params) {
try {
Log.e("Line 255:", "This line has been executed");
HttpTransport httpTransport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = GsonFactory.getDefaultInstance();
VisionRequestInitializer requestInitializer =
new VisionRequestInitializer(CLOUD_VISION_API_KEY) {
/**
* We override this so we can inject important identifying fields into the HTTP
* headers. This enables use of a restricted cloud platform API key.
*/
@Override
protected void initializeVisionRequest(VisionRequest<?> visionRequest)
throws IOException {
super.initializeVisionRequest(visionRequest);
String packageName = getPackageName();
visionRequest.getRequestHeaders().set(ANDROID_PACKAGE_HEADER, packageName);
String sig = PackageManagerUtils.getSignature(getPackageManager(), packageName);
visionRequest.getRequestHeaders().set(ANDROID_CERT_HEADER, sig);
}
};
Vision.Builder builder = new Vision.Builder(httpTransport, jsonFactory, null);
builder.setVisionRequestInitializer(requestInitializer);
Vision vision = builder.build();
BatchAnnotateImagesRequest batchAnnotateImagesRequest =
new BatchAnnotateImagesRequest();
batchAnnotateImagesRequest.setRequests(new ArrayList<AnnotateImageRequest>() {{
AnnotateImageRequest annotateImageRequest = new AnnotateImageRequest();
// Add the image
Image base64EncodedImage = new Image();
// Convert the bitmap to a JPEG
// Just in case it's a format that Android understands but Cloud Vision
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream);
byte[] imageBytes = byteArrayOutputStream.toByteArray();
// Base64 encode the JPEG
base64EncodedImage.encodeContent(imageBytes);
annotateImageRequest.setImage(base64EncodedImage);
// add the features we want
annotateImageRequest.setFeatures(new ArrayList<Feature>() {{
Feature textDetection = new Feature();
textDetection.setType("TEXT_DETECTION");
add(textDetection);
}});
// Add the list of one thing to the request
add(annotateImageRequest);
}});
Vision.Images.Annotate annotateRequest =
vision.images().annotate(batchAnnotateImagesRequest);
// Due to a bug: requests to Vision API containing large images fail when GZipped.
annotateRequest.setDisableGZipContent(true);
BatchAnnotateImagesResponse response = annotateRequest.execute();
return convertResponseToString(response);
} catch (GoogleJsonResponseException e) {
} catch (IOException e) {
}
return "Cloud Vision API request failed. Check logs for details.";
}
我好像错过了什么。我接受logcat中的这个错误。
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code": 403,
"errors": [
{
"domain": "global",
"message": "This API method requires billing to be enabled.
Please enable billing on project #285568034008 by visiting https://console.developers.google.com/billing/enable?project=285568034008 then retry.
If you enabled billing for this project recently, wait a few minutes for the action to propagate to our systems and retry.",
"reason": "forbidden"
}
],
"message": "This API method requires billing to be enabled. Please enable billing on project #285568034008 by visiting https://console.developers.google.com/billing/enable?project=285568034008 then retry. If you enabled billing for this project recently, wait a few minutes for the action to propagate to our systems and retry.",
"status": "PERMISSION_DENIED"
}
我也有同样的问题,我认为你必须启用计费帐户。