登录
原创

通信行程卡OCR识别-JAVA示例

发布于 2022-06-15 阅读 1460
  • 后端
  • Java
原创
package cn.juhe;


import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import sun.misc.BASE64Encoder;

public class Txxck {
    // 天气情况查询接口地址
    public static String API_URL = "http://apis.juhe.cn/txxck/query";
    // 接口请求Key
    public static String API_KEY = "********";

    public static void main(String[] args) {
        String image = GetImageStr("1.png");
        QueryTxxck(image);
    }

    /**
     * 请求通信行程卡OCR接口
     *
     * @param image 图像内容
     */
    public static void QueryTxxck(String image) {
        Map<String, Object> params = new HashMap<>();//组合参数
        params.put("image", image);
        params.put("key", API_KEY);
        String queryParams = urlencode(params);

        String response = doPost(API_URL, queryParams);
        try {
            System.out.println(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * get方式的http请求
     *
     * @param httpUrl 请求地址
     * @return 返回结果
     */
    public static String doPost(String httpUrl, String queryParams) {
        HttpURLConnection connection = null;
        InputStream inputStream = null;
        BufferedReader bufferedReader = null;
        String result = null;// 返回结果字符串
        try {
            // 创建远程url连接对象
            // URL url = new URL(new StringBuffer(httpUrl).append("?").append(queryParams).toString());
            URL url = new URL(httpUrl);
            // 通过远程url连接对象打开一个连接,强转成httpURLConnection类
            connection = (HttpURLConnection) url.openConnection();
            // 设置连接方式:get
            connection.setRequestMethod("POST");
            // 设置连接主机服务器的超时时间:15000毫秒
            connection.setConnectTimeout(5000);
            // 设置读取远程返回的数据时间:60000毫秒
            connection.setReadTimeout(6000);

            connection.setDoOutput(true);
            connection.setDoInput(true);
            // 发送请求
            connection.connect();

            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
            writer.write(queryParams);
            writer.close();

            // 通过connection连接,获取输入流
            if (connection.getResponseCode() == 200) {
                inputStream = connection.getInputStream();
                // 封装输入流,并指定字符集
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
                // 存放数据
                StringBuilder sbf = new StringBuilder();
                String temp;
                while ((temp = bufferedReader.readLine()) != null) {
                    sbf.append(temp);
                    sbf.append(System.getProperty("line.separator"));
                }
                result = sbf.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (null != bufferedReader) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                connection.disconnect();// 关闭远程连接
            }
        }
        return result;
    }

    /**
     * 将map型转为请求参数型
     *
     * @param data
     * @return
     */
    public static String urlencode(Map<String, ?> data) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, ?> i : data.entrySet()) {
            try {
                sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue() + "", "UTF-8")).append("&");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        String result = sb.toString();
        result = result.substring(0, result.lastIndexOf("&"));
        return result;
    }

    /**
     * 获取图片内容
     * @param imgFile
     * @return
     */
    public static String GetImageStr(String imgFile)
    {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try
        {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        //对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);//返回Base64编码过的字节数组字符串
    }
}

评论区

邋遢
0粉丝

邋遢邋遢。

0

0

0

举报