我试图使用Alloy Appcelerator 的Google vision API v1
我创建一个请求HTTPClient并调用APIhttps://vision.googleapis.com/v1/images:annotate?key=MY_APP_KEY
但我收到了来自谷歌的回复文本:
{
error = {
code = 400;
details = (
{
"@type" = "type.googleapis.com/google.rpc.BadRequest";
fieldViolations = ({
description = "Invalid JSON payload received. Unknown name "request": Cannot bind query parameter. Field 'request' could not be found in request message.";
});
}
);
message = "Invalid JSON payload received. Unknown name "request": Cannot bind query parameter. Field 'request' could not be found in request message.";
status = "INVALID_ARGUMENT";
};
}
还有我的代码使用Alloy 的HTTP请求
var requests =
{
"requests":[
{
"image":{
"content": "image_have_encodebase64",
},
"features":[
{
"type":"TEXT_DETECTION",
"maxResults":1
}
]
}
]
};
var xhr = Titanium.Network.createHTTPClient();
xhr.open("POST", 'https://vision.googleapis.com/v1/images:annotate?key=MY_APP_KEY');
xhr.send(JSON.stringify(requests));
感谢您的帮助
通过设置Content-Length
和Content-Type
标头,它应该可以工作:
xhr.setRequestHeader("Content-Length", size);
xhr.setRequestHeader("Content-Type", "application/json");
此外,需要注意的是,谷歌建议将您的图像大小调整为1024x768——您可以使用调整图像大小
img = img.imageAsResized(1024,768);
在对我的代码进行这些更改后,我的一切都正常工作了。