Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何通过http post将位图发送到face detect azure api_Android_Azure_Http Post_Face Api - Fatal编程技术网

Android 如何通过http post将位图发送到face detect azure api

Android 如何通过http post将位图发送到face detect azure api,android,azure,http-post,face-api,Android,Azure,Http Post,Face Api,在我的Android应用程序中,用户通过摄像头拍照。然后,它以位图的形式提供: Bitmap photo = (Bitmap) data.getExtras().get("data"); 我想通过http post将其发送到Azure Face detect API。目前,我只能使用pic的给定URL: StringEntity reqEntity = new StringEntity("{\"url\":\"https://upload.wikimedia.org/wikipedia/com

在我的Android应用程序中,用户通过摄像头拍照。然后,它以位图的形式提供:

Bitmap photo = (Bitmap) data.getExtras().get("data");
我想通过http post将其发送到Azure Face detect API。目前,我只能使用pic的给定URL:

StringEntity reqEntity = new StringEntity("{\"url\":\"https://upload.wikimedia.org/wikipedia/commons/c/c3/RH_Louise_Lillian_Gish.jpg\"}");
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(request)
如何使用位图照片将其发送到azure?

根据,您可以使用带有
应用程序/octet-stream
内容类型的API将android位图作为二进制数据传递

作为参考,这里是我的示例代码

String url = "https://westus.api.cognitive.microsoft.com/face/v1.0/detect";
HttpClient httpclient = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
request.setHeader("Content-Type", "application/octet-stream")
request.setHeader("Ocp-Apim-Subscription-Key", "{subscription key}");

// Convert Bitmap to InputStream
Bitmap photo = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
InputStream photoInputStream = new ByteArrayInputStream(baos.toByteArray());
// Use Bitmap InputStream to pass the image as binary data
InputStreamEntity reqEntity = new InputStreamEntity(photoInputStream, -1);
reqEntity.setContentType("image/jpeg");
reqEntity.setChunked(true);

request.setEntity(reqEntity);
HttpResponse response = httpclient.execute(request);
希望能有帮助