[OCR开发]-腾讯云发票单据识别

之前用票小秘的开发 后来转腾讯云,为了不修改前端代码所以后面的代码有返回结果的转换

	public static String invoiceTxy(String image) throws Exception {
		byte[] imageByteArray = FileUtil.read(image);
		String imageBase64 = new String(Base64.encodeBase64(imageByteArray), "UTF-8");
		System.out.println(imageBase64);
		VatInvoiceOCRResponse resp =new VatInvoiceOCRResponse();
		 JSONObject pxmObject=new JSONObject();
        try{
            // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential("AKIDVqvkIZZJmzyh8IXXXXXXXXXXXXXXXXXX", "IK6csqXx70dzf5YYYYYYYYYYYYYY");
            // 实例化一个http选项,可选的,没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");
            // 实例化一个client选项,可选的,没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            VatInvoiceOCRRequest req = new VatInvoiceOCRRequest();
            req.setImageBase64(imageBase64);
            // 返回的resp是一个VatInvoiceOCRResponse的实例,与请求对象对应
            resp = client.VatInvoiceOCR(req);
            // 输出json格式的字符串回包
            System.out.println(VatInvoiceOCRResponse.toJsonString(resp));       
            //下面开始 将腾讯云的格式转成票小秘的
            JSONObject txyObject=JSONObject.parseObject(VatInvoiceOCRResponse.toJsonString(resp));
           
            
            pxmObject.put("result", 1);
            
            JSONObject pxmresponse=new JSONObject();
            
            JSONObject pxmdata=new JSONObject();
            pxmdata.put("version", "4.8_20180226");
            pxmdata.put("result", 1);
            pxmdata.put("timestamp", 1092098766);
            pxmdata.put("message", "success");
            pxmdata.put("id", "a744aa6929b973511111111111111");
            pxmdata.put("sha1", "00a09af73198ab3ea81a6b522222222222222222");
            pxmdata.put("time_cost", "1000");
            
            JSONArray pxmidentify_results=new JSONArray();
            JSONObject pxmresObj=new JSONObject();
            pxmresObj.put("type", "10101");
            pxmresObj.put("orientation", "90");
            pxmresObj.put("image_size", "[1024,768]");
            pxmresObj.put("page", 0);
            
            JSONObject pxmresObjDetail=new JSONObject();
           
            JSONArray txyItems=txyObject.getJSONArray("Items");
            JSONArray txyVatInvoiceInfos=txyObject.getJSONArray("VatInvoiceInfos");
        	StringBuffer mmq=new StringBuffer();
            for(int i=0;i<txyVatInvoiceInfos.size();i++){
            	JSONObject info=txyVatInvoiceInfos.getJSONObject(i);
            	if(info.get("Name").equals("发票代码")){
            		pxmresObjDetail.put("code", info.get("Value"));
            	}
            	if(info.get("Name").equals("发票号码")){
            		pxmresObjDetail.put("number", info.get("Value").toString().replace("No", ""));
            	}
            	if(info.get("Name").equals("打印发票代码")){
            		pxmresObjDetail.put("code_confirm", info.get("Value"));
            	}
            	if(info.get("Name").equals("打印发票号码")){
            		pxmresObjDetail.put("number_confirm", info.get("Value"));
            	}
            	if(info.get("Name").equals("开票日期")){
            		pxmresObjDetail.put("date", info.get("Value"));
            	}
            	if(info.get("Name").equals("合计金额")){
            		pxmresObjDetail.put("pretax_amount", info.get("Value").toString().replace("¥", ""));
            	}
            	if(info.get("Name").equals("小写金额")){
            		String total=info.get("Value").toString();
            		total=total.replace("¥", "");
            		System.out.println("金额"+info.get("Value").toString().replace("¥", ""));
            		pxmresObjDetail.put("total", total);
            	}
            	if(info.get("Name").equals("价税合计(大写)")){
            		pxmresObjDetail.put("total_cn", info.get("Value"));
            	}
            	if(info.get("Name").equals("合计税额")){
            		pxmresObjDetail.put("tax", info.get("Value").toString().replace("¥", ""));
            	}
            	if(info.get("Name").equals("校验码")){
            		pxmresObjDetail.put("check_code", info.get("Value"));
            	}
            	if(info.get("Name").equals("机器编号")){
            		pxmresObjDetail.put("machine_code", info.get("Value"));
            	}
            	if(info.get("Name").equals("销售方名称")){
            		pxmresObjDetail.put("seller", info.get("Value"));
            	}
            	if(info.get("Name").equals("销售方识别号")){
            		pxmresObjDetail.put("seller_tax_id", info.get("Value"));
            	}
            	if(info.get("Name").equals("销售方地址、电话")){
            		pxmresObjDetail.put("seller_addr_tel", info.get("Value"));
            	}
            	if(info.get("Name").equals("销售方开户行及账号")){
            		pxmresObjDetail.put("seller_bank_account", info.get("Value"));
            	}
            	
            	if(info.get("Name").equals("购买方名称")){
            		pxmresObjDetail.put("buyer", info.get("Value"));
            	}
            	if(info.get("Name").equals("购买方识别号")){
            		pxmresObjDetail.put("buyer_tax_id", info.get("Value"));
            	}
            	if(info.get("Name").equals("购买方地址、电话")){
            		pxmresObjDetail.put("buyer_addr_tel", info.get("Value"));
            	}
            	if(info.get("Name").equals("购买方开户行及账号")){
            		pxmresObjDetail.put("buyer_bank_account", info.get("Value"));
            	}        
            	if(info.get("Name").equals("是否有公司印章")){
            		pxmresObjDetail.put("company_seal", info.get("Value"));
            		pxmresObjDetail.put("company_seal_mark", info.get("Value"));
            	}
            	if(info.get("Name").equals("联次")){
            		pxmresObjDetail.put("form_type", info.get("Value"));
            		pxmresObjDetail.put("form_name", info.get("Value"));
            	}
            	if(info.get("Name").equals("发票消费类型")){
            		pxmresObjDetail.put("kind", info.get("Value"));
            	}
            	if(info.get("Name").equals("密码区1")){
            		mmq.append(info.get("Value"));
            	}
            	if(info.get("Name").equals("密码区2")){
            		mmq.append(info.get("Value"));
            	}
            	if(info.get("Name").equals("密码区3")){
            		mmq.append(info.get("Value"));
            	}
            	if(info.get("Name").equals("密码区4")){
            		mmq.append(info.get("Value"));
            	}
            	pxmresObjDetail.put("ciphertext", mmq);
            	
            	if(info.get("Name").equals("车船税")){
            		pxmresObjDetail.put("travel_tax", info.get("Value"));
            	}
            	if(info.get("Name").equals("收款人")){
            		pxmresObjDetail.put("receiptor", info.get("Value"));
            	}  
            	if(info.get("Name").equals("复核")){
            		pxmresObjDetail.put("reviewer", info.get("Value"));
            	}  
            	if(info.get("Name").equals("开票人")){
            		pxmresObjDetail.put("issuer", info.get("Value"));
            	}  
            	if(info.get("Name").equals("省")){
            		pxmresObjDetail.put("province", info.get("Value"));
            	}  
            	if(info.get("Name").equals("市")){
            		pxmresObjDetail.put("city", info.get("Value"));
            	}   
            	if(info.get("Name").equals("备注")){
            		pxmresObjDetail.put("remark", info.get("Value"));
            	}  
            	if(info.get("Name").toString().contains("货物或应税劳务")){
            		pxmresObjDetail.put("service_name", info.get("Value"));
            	}  
            	if(info.get("Name").equals("是否代开")){
            		pxmresObjDetail.put("agent_mark", info.get("Value"));
            	}  
            	if(info.get("Name").equals("通行费标志")){
            		pxmresObjDetail.put("transit_mark", info.get("Value"));
            	}
            	if(info.get("Name").equals("成品油标志")){
            		pxmresObjDetail.put("oil_mark", info.get("Value"));
            	}  
            	if(info.get("Name").equals("发票名称")){
            		pxmresObjDetail.put("title", info.get("Value"));
            	}  


            }
       
            JSONArray pxmresObjDetailI=new JSONArray();
            for(int j=0;j<txyItems.size();j++){
            	JSONObject item=txyItems.getJSONObject(j);
            	JSONObject detailI=new JSONObject();
            	detailI.put("name", item.getString("Name"));
            	detailI.put("specification", item.getString("Spec"));
            	detailI.put("unit", item.getString("Unit"));
            	detailI.put("quantity", item.getString("Quantity"));
            	detailI.put("price", item.getString("UnitPrice"));
            	detailI.put("total", item.getString("AmountWithoutTax"));
            	detailI.put("tax_rate", item.getString("TaxRate"));
            	detailI.put("tax", item.getString("TaxAmount"));
            	pxmresObjDetail.put("invoice_taxRate", item.getString("TaxRate").toString().replace("¥", "").replace("%", ""));
            	pxmresObjDetailI.add(detailI);
            }
            
            pxmresObjDetail.put("items", pxmresObjDetailI);
            pxmresObj.put("details", pxmresObjDetail);
            pxmidentify_results.add(pxmresObj);
            pxmdata.put("identify_results", pxmidentify_results);
            pxmresponse.put("data", pxmdata);
            pxmObject.put("response", pxmresponse);


         
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return JSONObject.toJSONString(pxmObject);
		
	}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

司徒剑南

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

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

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

打赏作者

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

抵扣说明:

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

余额充值