用java代码调用名片识别接口示例代码

接口地址:http://www.juhe.cn/docs/api/id/139

接口名称:名片识别

调用语言:java

文档参数:

请求参数:

  名称 类型 必填 说明
  image string 名片图像的base64串
  lang string 语言种类,简体:chns,繁体:chnt,英文:en,默认chns
  color stirng 图像类型,gray:进行了灰度处理 ,original:原图,建议进行灰度处理
  key string 应用APPKEY(应用详细页查询)
  dtype string 返回数据的格式,xml或json,默认json

    根据名片识别接口的文档要求可以看出key是调用聚合平台的所有接口的一个令牌,所以必须先注册登录然后申请该数据,这里不做赘述,主要示例调用的代码。另一个必须参数是image,根据说明可以看出,这个参数是读取图片的char数组转为string类型后再经过加密的字符串,所以必须用到base64这个加密工具类。建议使用org.apache.commons.codec.binary.Base64这个类,,下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi

下面是网络访问工具类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package  org.silk.net;
 
import  java.io.BufferedReader;
import  java.io.BufferedWriter;
import  java.io.InputStream;
import  java.io.InputStreamReader;
import  java.io.OutputStream;
import  java.io.OutputStreamWriter;
import  java.net.HttpURLConnection;
import  java.net.URL;
import  java.util.Map;
import  java.util.zip.GZIPInputStream;
/**
  * 网络访问工具类
  * @author silk
  *
  */
public  class  PureNetUtil {
     /**
      * get方法直接调用post方法
      * @param url 网络地址
      * @return 返回网络数据
      */
     public  static  String get(String url){
         return  post(url, null );
     }
     public  static  String get(String url,String charset){
         return  post(url, null ,charset);
     }
     /**
      * 设定post方法获取网络资源,如果参数为null,实际上设定为get方法
      * @param url 网络地址
      * @param param 请求参数键值对
      * @return 返回读取数据
      */
     public  static  String post(String url,Mapparam){
         return  post(url,param, null );
     }
     
     public  static  String post(String  url,Mapparam,String outCharset){
         if (outCharset== null ){
             outCharset= "UTF-8" ;
         }
         HttpURLConnection conn= null ;
         try  {
             URL u= new  URL(url);
             conn=(HttpURLConnection) u.openConnection();
             conn.setRequestProperty( "User-agent" , "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36" );
             StringBuffer sb= null ;
             if (param!= null ){ //如果请求参数不为空
                 sb= new  StringBuffer();
                 /*A URL connection can be used for input and/or output.  Set the DoOutput
                  * flag to true if you intend to use the URL connection for output,
                  * false if not.  The default is false.*/
                 //默认为false,post方法需要写入参数,设定true
                 conn.setRequestMethod( "POST" );
                 conn.setDoOutput( true );
                 //设定post方法,默认get
                 //获得输出流
                 OutputStream out=conn.getOutputStream();
                 //对输出流封装成高级输出流
                 BufferedWriter writer= new  BufferedWriter( new  OutputStreamWriter(out));
                 //将参数封装成键值对的形式
                 for (Map.Entry s:param.entrySet()){
                     sb.append(s.getKey()).append( "=" ).append(s.getValue()).append( "&" );
                 }
                 writer.write(sb.deleteCharAt(sb.toString().length()- 1 ).toString());
                 writer.close(); //如果忘记关闭输出流将造成参数未完全写入的情况
                 sb= null ;
             }
             conn.connect(); //建立连接
             sb= new  StringBuffer();
             //获取连接状态码
             int  recode=conn.getResponseCode();
             BufferedReader reader= null ;
             if (recode== 404 ){
                 System.out.println( "404===>" +url);
             }
             if (recode== 200 ){
                 //Returns an input stream that reads from this open connection
                 //从连接中获取输入流
                 InputStream in=conn.getInputStream();
                 String encoding=conn.getContentEncoding();
                 if  (encoding !=  null  && encoding.equalsIgnoreCase( "gzip" )) {
                     GZIPInputStream gis =  new  GZIPInputStream(in);
                     reader= new  BufferedReader( new  InputStreamReader(gis,outCharset));
                     for (String str=reader.readLine();str!= null ;str=reader.readLine()){
                         sb.append(str).append(System.getProperty( "line.separator" )); //原网页的换行加上
                     }
                 } else {
                     reader= new  BufferedReader( new  InputStreamReader(in,outCharset));
                     for (String str=reader.readLine();str!= null ;str=reader.readLine()){
                         sb.append(str).append(System.getProperty( "line.separator" )); //原网页的换行加上
                     }
                 }
                 //关闭输入流
                 reader.close();
                 if  (sb.toString().length() ==  0 ) {
                     return  null ;
                 }
                 return  sb.toString().substring( 0 ,
                         sb.toString().length() - System.getProperty( "line.separator" ).length());             
             }
         catch  (Exception e) {
             e.printStackTrace();
             return  null ;
         } finally {
             if (conn!= null ) //关闭连接
                 conn.disconnect();
         }
         return  null ;
     }
 
}

下面是调用代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package  org.silk.mingpianshibie;
 
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.InputStream;
import  java.util.HashMap;
import  java.util.Map;
 
import  org.apache.commons.codec.binary.Base64;
import  org.silk.net.PureNetUtil;
 
 
public  class  CardAPIDemo {
     private  static  final  String KEY= "您申请的appKey" ;
     private  static  final  String  URL= "http://op.juhe.cn/hanvon/bcard/query" ;
     private  static  final  String PATH= "D:\\1.jpg" ;
     public  static  void  main(String[] args) {
         System.out.println(invoke());
     }
     public  static  String invoke() {
         Mapparams= new  HashMap();
         File file= new  File(PATH);
         InputStream in;
         try  {
             in =  new  FileInputStream(file);
             int  i = in.available();  // 得到文件大小  
             byte  data[] =  new  byte [i];  
             in.read(data);  // 读数据  
             in.close();  
             params.put( "key" , KEY);
             Base64 base64= new  Base64( true );  
             params.put( "image" ,base64.encodeToString(data));
             return  PureNetUtil.post(URL, params);
         catch  (Exception e) {
             e.printStackTrace();
         }
         return  null ;
     }
 
 
}

打印结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{
     "reason" "successed" ,
     "result" : {
         "rotatedAngle" null ,
         "name" : [
             "刘耿臣"
         ],
         "title" : [],
         "tel" : [
             "037168267555"
         ],
         "mobile" : [
             "13283808566"
         ],
         "fax" : [
             "037168267976"
         ],
         "email" : [
             "hc9615@163.com"
         ],
         "comp" : [
             "中国粮油机械集团有限公司A"
         ],
         "dept" : [],
         "degree" : [],
         "addr" : [
             "ADD:郑州市经三路农业路交汇处银丰商务A座1426室"
         ],
         "post" : [],
         "mbox" : [],
         "htel" : [],
         "web" : [],
         "im" : [],
         "numOther" : [],
         "other" : [],
         "extTel" : []
     },
     "error_code" 0
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值