Java 调用Azure认知服务Demo--Computer API

说明

本文主要介绍使用Java代码,基于HTTP请求调用Microsoft Azure的认知服务。图片来源分别介绍了使用公网的URL和上传本地图片。

依赖的jar包下载地址
key的获取需要登录到Azure门户创建相应的服务获取;
中国版Cognitive Service开发参考链接

使用公网URL作为图片地址

// This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
package buct.edu.cn;

import java.net.URI;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class Describe {

    public static void main(String[] args) {
        
        HttpClient httpclient = HttpClients.createDefault();

        try
        {
            URIBuilder builder = new URIBuilder("https://api.cognitive.azure.cn/vision/v1.0/describe");

            builder.setParameter("maxCandidates", "1");
            builder.setParameter("language", "en");

            URI uri = builder.build();
            HttpPost request = new HttpPost(uri);
            request.setHeader("Content-Type", "application/json");
            request.setHeader("Ocp-Apim-Subscription-Key", "<key>");


            // Request body
            StringEntity reqEntity = new StringEntity("{\"url\":\"http://pic30.nipic.com/20130606/12948584_155615545180_2.jpg\"}"); //URL图片地址
            request.setEntity(reqEntity);

            HttpResponse response = httpclient.execute(request);
            HttpEntity entity = response.getEntity();

            if (entity != null) 
            {
                System.out.println(EntityUtils.toString(entity));
            }
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}

读取本地图片

// This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
package buct.edu.cn;

import java.io.FileInputStream;
import java.net.URI;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class DescribeStream {

    public static void main(String[] args) {
        
        HttpClient httpclient = HttpClients.createDefault();

        try
        {
            URIBuilder builder = new URIBuilder("https://api.cognitive.azure.cn/vision/v1.0/describe");

            builder.setParameter("maxCandidates", "1");
            //builder.setParameter("details", "{string}");
            builder.setParameter("language", "en");

            URI uri = builder.build();
            HttpPost request = new HttpPost(uri);
            request.setHeader("Content-Type", "application/octet-stream");
            request.setHeader("Ocp-Apim-Subscription-Key", "<key>");

            // Request body
            String pic_path = "D:\\test.jpg";  //本地图片地址
            
            System.out.println(pic_path);
            
            FileInputStream is = new FileInputStream(pic_path); 
            int i = is.available(); // 得到文件大小  
            byte data[] = new byte[i]; 
            is.read(data); // 读数据  将数据读到data中                
            is.close(); 
             
            ByteArrayEntity bae = new ByteArrayEntity(data);                                        
            request.setEntity(bae);  

            HttpResponse response = httpclient.execute(request);
            HttpEntity entity = response.getEntity();

            if (entity != null) 
            {
                System.out.println(EntityUtils.toString(entity));
            }
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}
posted @ 2017-03-07 20:43 taro_秋刀鱼 阅读(...) 评论(...) 编辑 收藏