gpt4 book ai didi

java - Azure Face API 无法使用本地文件

转载 作者:行者123 更新时间:2023-11-30 02:14:01 24 4
gpt4 key购买 nike

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

我的代码如下。我有一个带有此方法的 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());
}
}

最佳答案

我用以下代码解决了这个问题:

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

关于java - Azure Face API 无法使用本地文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49184447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com