之前用票小秘的开发 后来转腾讯云,为了不修改前端代码所以后面的代码有返回结果的转换
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{
Credential cred = new Credential("AKIDVqvkIZZJmzyh8IXXXXXXXXXXXXXXXXXX", "IK6csqXx70dzf5YYYYYYYYYYYYYY");
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("ocr.tencentcloudapi.com");
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);
VatInvoiceOCRRequest req = new VatInvoiceOCRRequest();
req.setImageBase64(imageBase64);
resp = client.VatInvoiceOCR(req);
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);
}