[OCR开发]-票小秘单据识别

一、选择发票图片后 调用该方法

	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+"&timestamp="+timestamp+"&token="+token;//+"&image_url="+image
		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 {
	  // OCR webapi 接口地址
		public static final String WEBOCR_URL = "http://webapi.xfyun.cn/v1/service/v1/ocr/invoice";
		// 应用ID
		public static final String APPID = "123456789";
		// 接口密钥
		public static final String API_KEY = "123456789963258";
		// 引擎类型
		public static final String ENGINE_TYPE = "invoice";
		
		//设置文字识别APPID/AK/SK
		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";
		/**
		 * OCR WebAPI 调用示例程序
		 * 
		 * @param args
		 * @throws IOException
		 */
		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);
		}

		/**
		 * 组装http请求头
		 */
		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;
		}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

司徒剑南

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值