Java &引用;解码错误,不支持图像格式“;在Microsoft Face API中

Java &引用;解码错误,不支持图像格式“;在Microsoft Face API中,java,api,face,Java,Api,Face,我正在尝试使用Microsoft Face API。为此,我有以下由Microsoft提供的代码作为示例(在本页末尾): 但我得到了以下错误: {"error":{"code":"InvalidImage","message":"Decoding error, image format unsupported."}} 我用于测试的图像如下所示: (通过快速搜索在互联网上找到) 它尊重微软设定的所有必要条件,大小和格式。。。如果我在网站上使用它,它就会工作 在base64中将字节数组转换为字符串

我正在尝试使用Microsoft Face API。为此,我有以下由Microsoft提供的代码作为示例(在本页末尾):

但我得到了以下错误:

{"error":{"code":"InvalidImage","message":"Decoding error, image format unsupported."}}
我用于测试的图像如下所示: (通过快速搜索在互联网上找到)

它尊重微软设定的所有必要条件,大小和格式。。。如果我在网站上使用它,它就会工作

在base64中将字节数组转换为字符串的
字符串体
也可以,我在这个网站上测试它:

错误消息很简单,但我看不到我在哪里工作。任何人都可能知道问题出在哪里

更新

变量
img

img = Files.readAllBytes(Paths.get(imgPath));

我做了如下改变。我发送的不是编码图像,而是图像的URL

request.setHeader("Content-Type", "application/json");
request.setHeader("Ocp-Apim-Subscription-Key", "{YOUR_FACES_API_KEY}");

StringEntity reqEntity = new StringEntity("{ \"url\":\"http://www.huntresearchgroup.org.uk/images/group/group_photo_2010.jpg\" }");
request.setEntity(reqEntity);
这将得到响应:

[{"faceRectangle":{"top":878,"left":2718,"width":312,"height":312},"faceAttributes":{"gender":"male","age":28.5}},{"faceRectangle":{"top":593,"left":573,"width":310,"height":310},"faceAttributes":{"gender":"male","age":27.5}},{"faceRectangle":{"top":1122,"left":1014,"width":294,"height":294},"faceAttributes":{"gender":"female","age":27.7}},{"faceRectangle":{"top":915,"left":1773,"width":277,"height":277},"faceAttributes":{"gender":"female","age":36.7}},{"faceRectangle":{"top":566,"left":1276,"width":269,"height":269},"faceAttributes":{"gender":"male","age":40.7}},{"faceRectangle":{"top":677,"left":2134,"width":257,"height":257},"faceAttributes":{"gender":"female","age":35.2}}]
将很快发送编码图像。我们将相应地更新这篇文章

编辑:

从URL下载图像

String base64Img = null;
byte[] bytes = null;
String imgBinaryString = null;
String base64ImgBinaryString = null;
try {
    URL url = new URL("http://www.businessstudynotes.com/wp-content/uploads/2015/09/Role-of-Group.jpg");
    //"http://www.huntresearchgroup.org.uk/images/group/group_photo_2010.jpg");
    BufferedImage image = ImageIO.read(url);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "jpg", baos);
    bytes = baos.toByteArray();
    StringBuilder sb = new StringBuilder();
    for (byte by: bytes)
        sb.append(Integer.toBinaryString(by & 0xFF));
    imgBinaryString = sb.toString();

    base64Img = Base64.getEncoder().encodeToString(bytes);
    byte[] base64Bytes = base64Img.getBytes("UTF-8");
    sb = new StringBuilder();
    for (byte by: base64Bytes) {
        sb.append(Integer.toBinaryString(by & 0xFF));
    }
    base64ImgBinaryString = sb.toString();

} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    System.out.println("Download issue");
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    System.out.println("ImageIO issue");
    e.printStackTrace();
}
imgBinaryString
包含图像的二进制表示
base64ImgBinaryString
包含图像的base64表示形式的二进制表示形式

request.setHeader("Content-Type", "application/json");
request.setHeader("Ocp-Apim-Subscription-Key", "{YOUR_FACES_API_KEY}");

StringEntity reqEntity = new StringEntity("{ \"url\":\"http://www.huntresearchgroup.org.uk/images/group/group_photo_2010.jpg\" }");
request.setEntity(reqEntity);
要上载此图像

URI uri = builder.build(); // builder = new URIBuilder("https://api.projectoxford.ai/face/v1.0/detect");
HttpPost request = new HttpPost(uri);
request.setHeader("Content-Type", "application/octet-stream");
request.setHeader("Ocp-Apim-Subscription-Key", "{YOUR_FACES_API_KEY");

StringEntity reqEntity = new StringEntity(base64ImgBinaryString);
request.setEntity(reqEntity);

HttpResponse response = httpclient.execute(request);
StringEntity
同时设置为
imgBinaryString
base64ImgBinaryString
会产生相同的响应

{“错误”:{“代码”:“无效图像”,“消息”:“解码错误,不支持图像格式”。}}

好东西。这是有效的…


其中
bytes
是图像的字节数组;但这种Base64表示法不起作用。有人确实需要更新文档。

我设法发现了问题。。。而不是:

String body = Base64.encodeBase64String(img);
StringEntity reqEntity = new StringEntity(body);
request.setEntity(reqEntity);
我需要做:

ByteArrayEntity reqEntity = new ByteArrayEntity(img, ContentType.APPLICATION_OCTET_STREAM);
request.setEntity(reqEntity);
我认为文档已经过时了…

您可以看看,一个开源库,它将处理与MS faces API的通信和交互。如果您不想使用这个库,那么您可以查看一下代码,看看RESTAPI需要什么


(披露——我是图书馆的作者)

您正在编码的
img
。好吗?我看到您正在使用Apache Commons Base64。一个小提示,Java 1.8有]。我不知道,我将代码更改为使用Java Base64,我遇到了相同的问题。我问你关于变量
img
。你还没有包含任何关于这方面的信息。如果我对一个错误的字符串进行Base64解码并发送它,它显然不会工作。这就是我所关心的。你是否正确初始化了img?是的,我的不好,我更新了我的帖子。是的,url工作正常,问题在于编码的图像。这是我第一次使用编码图像,所以经过一段时间的努力,我决定寻求帮助。我将等待您的更新,泰。我一直试图用这个工具使用二进制数据:但我遇到了同样的问题。@lulas是的!令人惊讶的是什么都不起作用,而规范本身也不清楚他们接受二进制数据的格式。我已经请我在微软认识的人了解这个问题。让我们看看这是怎么回事。最后……)@lulas我刚刚注意到你也添加了一个答案哈哈
ByteArrayEntity reqEntity = new ByteArrayEntity(img, ContentType.APPLICATION_OCTET_STREAM);
request.setEntity(reqEntity);
import okhttp3.*;

import java.io.File;
import java.io.IOException;


public class Main {


    public static void main(String[] args) {
        try {
            doRequest();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public static void doRequest() throws IOException {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(MediaType.parse("application/octet-stream"),
                new File(".//src//main//java//Archivo_001.png"));

        Request request = new Request.Builder()
                .url("https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise")
                .post(body)
                .addHeader("Ocp-Apim-Subscription-Key", "1d88f949af3443ea8cc16b7146bd7501")
                .addHeader("Content-Type", "application/json")
                .addHeader("cache-control", "no-cache")
                .build();
        Response response = client.newCall(request).execute();
        System.out.println(response.body().string());

    }


}