如何通过HTTP发布将位图发送到面部检测Azure API

编程入门 行业动态 更新时间:2024-10-27 15:28:57
本文介绍了如何通过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\":\"upload.wikimedia/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 = "westus.api.cognitive.microsoft/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(); photopress(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

本文发布于:2023-10-11 22:17:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1482996.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位图   发送到   面部   HTTP   Azure

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!