Android 如何通过http post将位图发送到face detect azure api
在我的Android应用程序中,用户通过摄像头拍照。然后,它以位图的形式提供: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
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);
希望能有帮助