0

在我的 Android 应用中,用户通过相机拍照。然后它可以作为位图使用:

Bitmap photo = (Bitmap) data.getExtras().get("data");

我想通过 http post 发送到 Azure Face-detect API。目前,我只能使用给定的图片 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?

4

1 回答 1

1

根据Azure Face Detect 的 API 参考,您可以使用具有application/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);

希望能帮助到你。

于 2017-09-11T07:52:48.353 回答