2017-09-09 10 views
0

私のAndroidアプリでは、ユーザーはカメラで写真を撮ります。それはビットマップとして利用できます:顔にビットマップを送信するにはhttp投稿経由でazure apiを検出する

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

これは、http投稿経由で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に送信するにはどうすればよいですか?

答えて

1

the API reference of Azure Face Detectによれば、コンテンツタイプがapplication/octet-streamのAPIを使用して、アンドロイドビットマップをバイナリデータとして渡すことができます。

参考として、ここに私のサンプルコードを示します。

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

希望します。

関連する問題