一、选择发票图片后 调用该方法
public static String invoiceOcr(String image) throws Exception {
String timestamp = System.currentTimeMillis() / 1000L + "";
String token = `DigestUtils`.md5Hex(Invoice.PXM_APP_KEY +"+"+ timestamp +"+"+ Invoice.PXM_APP_SECRET);
String url = Invoice.PXM_URL +"?app_key="+Invoice.PXM_APP_KEY+"×tamp="+timestamp+"&token="+token;
HashMap<String, String> header = new HashMap<String, String>();
byte[] imageByteArray = FileUtil.read(image);
String imageBase64 = new String(Base64.encodeBase64(imageByteArray), "UTF-8");
System.out.println(imageBase64);
return HttpUtils.doPost1(url, header,"image_data=" + URLEncoder.encode(imageBase64, "UTF-8"));
}
二、Invoice方法
public class Invoice {
public static final String WEBOCR_URL = "http://webapi.xfyun.cn/v1/service/v1/ocr/invoice";
public static final String APPID = "123456789";
public static final String API_KEY = "123456789963258";
public static final String ENGINE_TYPE = "invoice";
public static final String WZ_APP_ID = "123456789";
public static final String WZ_API_KEY = "123456789963258";
public static final String WZ_SECRET_KEY = "123456789963258";
public static final String WZ_TRAIN_ID = "https://aip.baidubce.com/rest/2.0/ocr/v1/train_ticket";
public static final String WZ_TAXI_ID = "https://aip.baidubce.com/rest/2.0/ocr/v1/taxi_receipt";
public static final String WZ_QUOTA_ID = "https://aip.baidubce.com/rest/2.0/ocr/v1/quota_invoice";
private static final String AUDIO_PATH = "";
public static final String PXM_URL = "https://fapiao.glority.cn/v1/item/get_item_info";
public static final String PXM_MULTI_URL = "https://fapiao.glority.cn/v1/item/get_multiple_items_info";
public static final String PXM_APP_KEY = "123456789963258";
public static final String PXM_APP_SECRET = "f73e3d774a37c8f45116cbc1dbcc8fdd";
public static void main(String[] args) throws IOException {
Map<String, String> header = buildHttpHeader();
byte[] imageByteArray = FileUtil.read(AUDIO_PATH);
String imageBase64 = new String(Base64.encodeBase64(imageByteArray), "UTF-8");
String result = HttpUtils.doPost1(WEBOCR_URL, header, "image=" + URLEncoder.encode(imageBase64, "UTF-8"));
System.out.println("WEB invoice 接口调用结果:" + result);
}
public static Map<String, String> buildHttpHeader() throws UnsupportedEncodingException {
String curTime = System.currentTimeMillis() / 1000L + "";
String param = "{\"engine_type\":\"" + ENGINE_TYPE + "\"}";
String paramBase64 = new String(Base64.encodeBase64(param.getBytes("UTF-8")));
String checkSum = DigestUtils.md5Hex(API_KEY + curTime + paramBase64);
Map<String, String> header = new HashMap<String, String>();
header.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
header.put("X-Param", paramBase64);
header.put("X-CurTime", curTime);
header.put("X-CheckSum", checkSum);
header.put("X-Appid", APPID);
return header;
}
}