【发布时间】:2016-07-07 08:30:12
【问题描述】:
我正在尝试使用 Microsoft Face API。为此,我有 Microsoft 提供的以下代码作为示例(在本页末尾https://dev.projectoxford.ai/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236):
HttpClient httpclient = HttpClients.createDefault();
try {
URIBuilder builder = new URIBuilder("https://api.projectoxford.ai/face/v1.0/detect");
builder.setParameter("returnFaceId", "false");
builder.setParameter("returnFaceLandmarks", "false");
builder.setParameter("returnFaceAttributes", "age,gender");
URI uri = builder.build();
HttpPost request = new HttpPost(uri);
request.setHeader("Content-Type", "application/octet-stream");
request.setHeader("Ocp-Apim-Subscription-Key", "...");
String body = Base64.encodeBase64String(img);
StringEntity reqEntity = new StringEntity(body);
request.setEntity(reqEntity);
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println(EntityUtils.toString(entity));
return JsonParser.parse(EntityUtils.toString(entity));
}
} catch (URISyntaxException | IOException | ParseException e) {
System.out.println(e.getMessage());
}
return null;
但我收到以下错误:
{"error":{"code":"InvalidImage","message":"Decoding error, image format unsupported."}}
我用于测试的图像是这个: http://www.huntresearchgroup.org.uk/images/group/group_photo_2010.jpg (在互联网上快速搜索找到它)
它尊重微软设定的所有要求、大小和格式...如果我在网站上使用它,它可以工作https://www.projectoxford.ai/demo/face#detection
我的字节数组转换成base64字符串的String body也可以,我在这个网站上测试:http://codebeautify.org/base64-to-image-converter
错误信息很简单,但我看不到我穿在哪里。有谁知道问题出在哪里?
更新
变量img:
img = Files.readAllBytes(Paths.get(imgPath));
【问题讨论】:
-
您正在编码的
img。一切都好吗?我看到您正在使用 Apache Commons Base64。仅供参考,Java 1.8 有 [Base64]。 -
不知道,我把代码改成使用Java Base64,我也遇到了同样的问题。
-
我在问你变量
img。您没有提供任何相关信息。 -
如果我 Base64 解码一个不正确的字符串并发送它,它显然是行不通的。这就是我所关心的。您是否正确初始化
img? -
是的,我的错,我更新了我的帖子。