1

我一直在尝试将图像从我的计算机发送到此 API,但我只收到以下错误:{"error":{"code":"InvalidImageSize","message":"Image size is too small."}}

我的代码如下。我有一个使用这种方法的 PostRequestClass:

public void sendImageRequest(String imagePath) {
    try {
        HttpClient httpClient = new DefaultHttpClient();
        File file = new File(imagePath);
        FileEntity reqEntity = new FileEntity(file, ContentType.APPLICATION_OCTET_STREAM);
        reqEntity.setChunked(false);
        HttpResponse response = httpClient.execute(request);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            this.responseResult = EntityUtils.toString(entity);
        }
    } catch(Exception e) {
        System.out.println(e.getMessage());
    }   
}

在我的主要是这个:

public class Test {
    public static void main(String[] args) throws URISyntaxException {
        PostRequest p = new PostRequest(
          "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceAttributes=emotion"
        );
        p.addHeader("Content-Type", "application/octet-stream");
        p.addHeader("Ocp-Apim-Subscription-Key", "my-api-key");
        p.sendImageRequest("/Users/user/Desktop/image.jpg");
        System.out.println(p.getResponseResult());           
    }
}
4

2 回答 2

1

我用以下代码解决了它:

public void sendImageRequest(String imagePath) {
    try {
        HttpClient httpClient = new DefaultHttpClient();

        File file = new File(imagePath);
        FileInputStream fileInputStreamReader = new FileInputStream(file);
        byte[] bytes = new byte[(int)file.length()];
        fileInputStreamReader.read(bytes);            
        ByteArrayEntity reqEntity = new ByteArrayEntity(bytes, ContentType.APPLICATION_OCTET_STREAM);
        request.setEntity(reqEntity);

        HttpResponse response = httpClient.execute(request);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            this.responseResult = EntityUtils.toString(entity);
        }
    } catch(Exception e) {
        System.out.println(e.getMessage());
    }   
}
于 2018-03-09T00:33:43.593 回答
1
  1. 转到https://azure.microsoft.com/en-us/services/cognitive-services/face/并单击“API 参考”。

  2. 它将带您到人脸 API 参考页面https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236

人脸 API 文档说“支持 JPEG、PNG、GIF(第一帧)和 BMP 格式。允许的图像文件大小为 1KB 到 4MB。”

在“JSON 中返回的错误代码和消息”标题下,“InValidImageSize”表示“有效图像文件大小应大于或等于 1KB”。

于 2018-03-09T01:02:27.253 回答