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

发布时间 2023-09-30 18:05:12作者: ®Geovin Du Dream Park™

 

/**
 * 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;
    }


}

  

    /**
     * 
     * 
     * */
    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;

    }

  

调用:

 SortingExmaple.baiduOCR();