一、准备工作
1、百度智能云申请一个文字识别的api
里面每个月有1000次调用测试应该够用
2、一个springboot项目
二、主要代码
①、pom依赖引入
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.16.5</version>
<exclusions>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-simple</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.baidubce</groupId>
<artifactId>api-explorer-sdk</artifactId>
<version>1.0.3.1</version>
<exclusions>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.10.0</version>
</dependency>
②、控制层
@ApiOperation("识别发票")
@ResponseBody
public ResponseEntity<InvoiceInfo> identifyInvoice(MultipartFile file) throws BizException {
return new ResponseEntity<>(invoiceInfoService.identifyInvoice(file), HttpStatus.OK);
}
③、实现层
public InvoiceInfo identifyInvoice(MultipartFile file) {
InvoiceInfo invoiceInfo = new InvoiceInfo();
// 请求url
String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice";
try {
byte[] fileData = file.getBytes();
String originalFilename = file.getOriginalFilename();
boolean isPdf = originalFilename.endsWith("pdf") || originalFilename.endsWith("PDF");
String fileStr = Base64Util.encode(fileData);
String fileParam = URLEncoder.encode(fileStr, "UTF-8");
String param = (isPdf ? "pdf_file" : "image") + "=" + fileParam;
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
String accessToken = AuthService.getAuth();
String result = HttpUtil.post(url, accessToken, param);
JSONObject jsonObject = JSON.parseObject(result);
if (jsonObject.getInteger("error_code") != null) {
String errorMsg = BaiduOcrConstant.ERROR_CODE_MAP.get(jsonObject.getInteger("error_code"));
System.out.println("票据识别失败,错误信息:" + errorMsg);
} else {
JSONObject words_result = jsonObject.getJSONObject("words_result");
invoiceInfo.setInvoiceType(words_result.getString("InvoiceTypeOrg"));
invoiceInfo.setImportType(isPdf ? 0L : 1L);
invoiceInfo.setCheckFlag(0L);
invoiceInfo.setSupplierName(words_result.getString("SellerName"));
invoiceInfo.setSupplierNumber(words_result.getString("SellerRegisterNum"));
invoiceInfo.setSupplierDescription(words_result.getString("SellerAddress"));
invoiceInfo.setBankNumber(words_result.getString("SellerBank"));
invoiceInfo.setPurchaserName(words_result.getString("PurchaserName"));
invoiceInfo.setPurchaserNumber(words_result.getString("PurchaserRegisterNum"));
invoiceInfo.setBillingDate(words_result.getDate("InvoiceDate"));
invoiceInfo.setInvoiceCode(words_result.getString("InvoiceCodeConfirm"));
invoiceInfo.setInvoiceNumber(words_result.getString("InvoiceNum"));
invoiceInfo.setCheckCode(words_result.getString("CheckCode"));
invoiceInfo.setAmountTotal(words_result.getBigDecimal("AmountInFiguers"));
invoiceInfo.setAmount(words_result.getBigDecimal("TotalAmount"));
if("*".equals(words_result.getString("TotalTax"))){
invoiceInfo.setTaxAmount(new BigDecimal("0.00"));
}else {
invoiceInfo.setTaxAmount(words_result.getBigDecimal("TotalTax"));
}
invoiceInfo.setRate(words_result.getBigDecimal("TotalAmount"));
invoiceInfo.setCreateTime(new Date());
}
return invoiceInfo;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
其次就是百度需要的utils包
最后直接调用就行