搜索
首页php教程PHP源码名片识别接口的调用示例

php代码

/*********************************这是请求的参数处理*************************************************/
 
//示例里面需要导入的包如果你没有,请自行去网上下载,其他异常可留言或在群里提问
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
 
import net.sf.json.JSONObject;
 
import org.apache.commons.codec.binary.Base64;
import org.silk.net.PureNetUtil;
public class CardAPIDemo {
    private static final String KEY="****f6e8cda2876336a7f3cd2dcc****";//这里替换成你的key
    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) {
        JSONObject obj=JSONObject.fromObject(invoke());
        System.out.println("result==>"+obj.getString("result"));
    }
    public static String invoke() {
        Mapparams=new HashMap();
        File file=new File(PATH);
        InputStream in;
        String html=null;
        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));//image参数可不是你的图片地址
            html= PureNetUtil.post(URL, params);//这里用到了一个我自己封装的网络请求类,见下文
        } catch (Exception e) {
            e.printStackTrace();
        }
        return html;
    }
}
 
 
/***********************************这是上面用到的网络请求工具类*************************************/
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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;
 
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
 
 
public class PureNetUtil {
    /**
     * 用来输出现在时间
     * @return
     */
    public static String currentTime(){
        SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
        return sdformat.format(new Date());
    }
    /**
     * get方法直接调用post方法
     * @param url 网络地址
     * @return 返回网络数据
     */
    public static String get(String url){
        return post(url,null,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,String outCharset){
        if(outCharset==null||outCharset.equals("")){
            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");
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            conn.setRequestProperty("Accept", "application/json;charset=utf-8");
            conn.setConnectTimeout(60000);
            conn.setReadTimeout(60000);
            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.Entrys:param.entrySet()){
                    sb.append(s.getKey()).append("=").append(s.getValue()).append("&");
                }
                System.out.println("para:"+sb.deleteCharAt(sb.toString().length()-1).toString());
                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){
            }
            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;
    }
     
    public static String  post(String url,Mapparam){
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        StringBuffer sb=null;
        try {
            ListnameValuePairs = new ArrayList(param.size());
            if(param!=null){
                for(Map.Entry map:param.entrySet()){
                    nameValuePairs.add(new BasicNameValuePair(map.getKey().toString(), map.getValue().toString()));
 
                }
            }
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response=httpclient.execute(httppost);
            InputStream in=response.getEntity().getContent();
            sb=new StringBuffer();
            BufferedReader reader=new BufferedReader(new InputStreamReader(in));
            for(String str=reader.readLine();str!=null;str=reader.readLine()){
                sb.append(str).append(System.getProperty("line.separator"));//原网页的换行加上
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
    /**
     * 这个方法主要是用来直接向服务器传输参数,比如已经加密的数据,直接传到服务器
     * @param url
     * @param data
     * @return
     */
    public static String post(String  url,byte[] data ){
        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;
            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));
            //将参数封装成键值对的形式
            writer.write(new String(data));
            writer.close();//如果忘记关闭输出流将造成参数未完全写入的情况
            conn.connect();//建立连接
            //获取连接状态码
            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));
                    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));
                    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;
    }
}

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器