java: 用百度API读取增值税发票信息

 

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
 * encoding: utf-8
 * 版权所有 2023 涂聚文有限公司
 * 许可信息查看:https://github.com/Baidu-AIP/java-sdk/blob/master/src/main/java/com/baidu/aip/http/AipRequest.java
 * 描述:
 * # Author    : geovindu,Geovin Du 涂聚文.
 * # IDE       : IntelliJ IDEA 2023.1 Java 17
 * # Datetime  : 2023 - 2023/9/30 - 16:31
 * # User      : geovindu
 * # Product   : IntelliJ IDEA
 * # Project   : EssentialAlgorithms
 * # File      : BaiduAuthService.java  类
 * # explain   : 学习
 **/
 
package SortingAlgorithms;
 
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import com.baidu.aip.util.Base64Util;
import com.baidu.aip.http.AipHttpClient.*;
import com.baidu.aip.client.*;
import com.baidu.aip.client.BaseClient;
import com.baidu.aip.error.AipError;
import com.baidu.aip.http.AipRequest;
import com.baidu.aip.util.Base64Util;
import com.baidu.aip.util.ImageUtil;
 
import com.baidu.aip.util.Util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
 
import javax.imageio.stream.FileImageInputStream;
 
import org.json.JSONArray;
import org.json.JSONObject;
 
import com.baidu.aip.ocr.AipOcr;
 
import java.net.URLEncoder;
 
public class BaiduAuthService {
 
 
    /**
     *https://github.com/Baidu-AIP/java-sdk/blob/master/src/main/java/com/baidu/aip/ocr/AipOcr.java
     *
     * */
    public static String getAuth() {
        // 官网获取的 API Key 更新为你注册的
        String clientId = "QuXMNizc80gTmUznKDRqQX3D";
        // 官网获取的 Secret Key 更新为你注册的
        String clientSecret = "h6aHaGLssw51CYGtR3dvX1wGg6BBm0zi";
        return getAuth(clientId, clientSecret);
    }
    /**
     *
     *
     * */
    public static String getAuth(String ak, String sk) {
        // 获取token地址
        String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
        String getAccessTokenUrl = authHost
                // 1. grant_type为固定参数
                + "grant_type=client_credentials"
                // 2. 官网获取的 API Key
                + "&client_id=" + ak
                // 3. 官网获取的 Secret Key
                + "&client_secret=" + sk;
        try {
            URL realUrl = new URL(getAccessTokenUrl);
            // 打开和URL之间的连接
            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            // 定义 BufferedReader输入流来读取URL的响应
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String result = "";
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            Logger logger = LoggerFactory.getLogger(BaiduAuthService.class);
            JSONObject jsonObject = new JSONObject(result);
            String access_token = jsonObject.getString("access_token");
            logger.info("获取百度token成功,access_token:{}", access_token);
            return access_token;
        } catch (Exception e) {
            Logger logger = LoggerFactory.getLogger(BaiduAuthService.class);
            logger.error("获取百度token失败, {}", e);
            //throw  //logger.error("获取百度OCR token失败{}",e);
            //throw e.toString();
            return  "";
        }
    }
 
    /**
     *
     *
     * */
    public static String vatInvoice(String filePath) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice";
        try {
            // 本地文件路径
            //String filePath = "[本地文件路径]";
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");
 
            String param = "image=" + imgParam;
 
            // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
            String accessToken = getAuth();
 
            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     *
     * 这个有效
     * */
    public static byte[] image2byte(String path){
        byte[] data = null;
        FileImageInputStream input = null;
        try {
            //图片输入
            input = new FileImageInputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int numBytesRead = 0;
            while ((numBytesRead = input.read(buf)) != -1) {
                output.write(buf, 0, numBytesRead);
            }
            data = output.toByteArray();
            output.close();
            input.close();
 
        catch (FileNotFoundException ex1) {
            ex1.printStackTrace();
        }
        catch (IOException ex1) {
            ex1.printStackTrace();
        }
        return data;
    }
 
 
}

  

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
/**
 *
 *
 * */
public static String baiduOCR()
{
    String result="";
    //result= SortingAlgorithms.BaiduAuthService.vatInvoice("C:\\Users\\geovindu\\IdeaProjects\\EssentialAlgorithms\\src\\fapiao.png");
    //System.out.println(result);
 
 
    JSONObject jsonObject=new JSONObject();
    //以token信息创建api调用对象
    com.baidu.aip.ocr.AipOcr aipOcr=new AipOcr("40226401", "QuXMNizc80gTmUznKDRqQX3D", "h6aHaGLssw51CYGtR3dvX1wGg6BBm0zi");
   // com.baidu.aip.ocr.AipOcr aipOcr=new AipOcr("10728591", "k1qEDIj16cfpEQU1FUGYEXIG", "NGZqqvWlaoS8Ydqfz0EtLYKu7ebkiQMW");
    //定义装图片信息的byte数组
    byte[] img=null;
    //图片本地地址
    String path="C:\\Users\\geovindu\\IdeaProjects\\EssentialAlgorithms\\src\\fapiao.png";
    //转换为byte数组
    img=SortingAlgorithms.BaiduAuthService.image2byte(path);
    System.out.println(img);
 
    //调用Api可选参数,把返回文字外接多边形顶点位置设为true
    HashMap<String, String> options=new HashMap<String, String>();
    options.put("vertexes_location", "true");
 
    System.out.println("SDK:");
    //请求百度接口识别图片
    jsonObject=aipOcr.general(img,null);
    // int logid=jsonObject.getInt("log_id");
    //  System.out.println("log_id"+logid);
    JSONArray jsonArray=jsonObject.getJSONArray("words_result");
    for(int i=0;i<jsonArray.length();i++) {
        JSONObject object = (JSONObject) jsonArray.get(i);
        String words=object.getString("words");
        JSONObject location=(JSONObject) object.get("location");
        System.out.println("words  "+words);
        System.out.println("location"+location);
    }
    return  result;
 
}

  

调用:

1
SortingExmaple.baiduOCR();

  

 

posted @   ®Geovin Du Dream Park™  阅读(172)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2022-09-30 CSharp: State Pattern
2022-09-30 CSharp:Observer Pattern
2022-09-30 CSharp: Mediator Pattern
2022-09-30 Java: Visitor Pattern
2022-09-30 Java: State Pattern
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示