如何通过HTTP发布将位图发送到面部检测Azure API [英] How to send bitmap to face detect azure api via http post
本文介绍了如何通过HTTP发布将位图发送到面部检测Azure API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的Android应用中,用户通过相机拍摄照片.然后可以将其作为位图使用:
In my Android app the user takes a photo via camera. It is then available as bitmap:
Bitmap photo = (Bitmap) data.getExtras().get("data");
我想通过http发布将其发送到Azure人脸检测API.目前,我只能使用给定图片的URL来工作:
This I want to send to Azure Face-detect API via http post. Currently I get it only to work with a given URL to a pic:
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)
如何使用位图照片将其发送到天蓝色?
How to use the Bitmap photo to send it to azure?
推荐答案
根据 Azure Face Detect的API参考,您可以将API与 application/octet-stream
内容类型一起使用,以将Android位图作为二进制数据传递.
According to the API reference of Azure Face Detect, you can use the API with application/octet-stream
content type to pass the android bitmap as binary data.
作为参考,这是我的示例代码.
As reference, here is my sample code.
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);
希望有帮助.
这篇关于如何通过HTTP发布将位图发送到面部检测Azure API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文