【发布时间】:2018-07-17 02:15:02
【问题描述】:
我一直在尝试在 android 上使用 volley 向 Microsoft 计算机视觉 API 发出请求,但我想从手机上传图像,而不仅仅是发送 url。来自 API (https://westcentralus.dev.cognitive.microsoft.com/docs/services/56f91f2d778daf23d8ec6739/operations/56f91f2e778daf14a499e1fa) 的引用说将 Content-Type 放在 application/octet-stream 上,并且在正文中它只是说“[二进制图像数据]”。 我尝试将图像作为字节数组(byte [])发送,但我不断收到响应 400(代表 InvalidImageFormat 或 Size)。 如果我使用 url 方法,它可以正常工作,但我需要上传图片。
This is the only imformation that the documentation gives
这是我一直在使用的代码:
String URL = "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/analyze?visualFeatures=Categories&language=en";
StringRequest apiRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
RespuestaApi.setText("Respuesta: " + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
RespuestaApi.setText("Error: " + error.toString());
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/octet-stream");
headers.put("Ocp-Apim-Subscription-Key", SubKey);
return headers;
}
@Override
public byte[] getBody() throws AuthFailureError {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImgTemp.compress(Bitmap.CompressFormat.JPEG, 50, baos);
byte[] imageBytes = baos.toByteArray();
return imageBytes;
}
};
VolleySingleton.getInstancia(PruebaApi.this).agregarACola(apiRequest);
顺便说一下,我的位图工作正常。 这是logcat给我的错误:
E/Volley: [41310] BasicNetwork.performRequest: Unexpected response code 400 for https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/analyze?visualFeatures=Categories&language=en
This is the reference for the response
那么,最后,我必须做些什么才能发送 api 所需的正确图像格式?
提前谢谢你。
【问题讨论】:
标签: android computer-vision byte android-volley