2017-09-09 58 views
0

在我的Android應用程序中,用戶通過相機拍攝照片。它然後可作爲位圖使用:如何通過http發送位圖到臉部檢測azure api文章

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) 

如何使用位圖照片將其發送到天藍色?

回答

1

根據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); 

希望它有幫助。