如何通过 http post 将位图发送到人脸检测 azure api
How to send bitmap to face detect azure api via http post
在我的 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)
如何使用Bitmap照片发送到azure?
根据 the API reference of Azure Face Detect,您可以使用具有 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);
希望对您有所帮助。
在我的 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)
如何使用Bitmap照片发送到azure?
根据 the API reference of Azure Face Detect,您可以使用具有 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);
希望对您有所帮助。