|
|
@ -1,6 +1,9 @@ |
|
|
|
package com.ccsens.ptccsens.service; |
|
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil; |
|
|
|
import cn.hutool.core.collection.CollectionUtil; |
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
import com.alibaba.druid.sql.visitor.functions.If; |
|
|
|
import com.ccsens.ptccsens.bean.vo.OcrVo; |
|
|
|
import com.ccsens.ptccsens.util.BasicsConstant; |
|
|
|
import com.ccsens.util.RestTemplateUtil; |
|
|
@ -12,26 +15,38 @@ import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Propagation; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.text.ParseException; |
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
|
|
|
|
@Service |
|
|
|
@Slf4j |
|
|
|
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) |
|
|
|
public class OcrService implements IOcrService{ |
|
|
|
|
|
|
|
@Override |
|
|
|
public OcrVo.BillInfo identifyBill(String img) { |
|
|
|
public OcrVo.BillInfo identifyBill(String img) throws ParseException { |
|
|
|
// 图像识别
|
|
|
|
BaiDuDto.GeneralBasic basic = new BaiDuDto.GeneralBasic(); |
|
|
|
basic.setImage(img); |
|
|
|
// BaiDuVo.BillBasic words = BaiDuUtil.billBasic(BasicsConstant.BaiDu.APP_KEY, BasicsConstant.BaiDu.SECRET_KEY, basic);
|
|
|
|
// log.info("识别结果:{}",words);
|
|
|
|
String token = BaiDuUtil.getToken(BasicsConstant.BaiDu.INVOICE_APP_KEY, BasicsConstant.BaiDu.INVOICE_SECRET_KEY); |
|
|
|
String invoiceUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice?access_token={}"; |
|
|
|
String url = StrUtil.format(invoiceUrl, token); |
|
|
|
String result = RestTemplateUtil.postUrlEncode(url, basic); |
|
|
|
log.info("result:{}", result); |
|
|
|
BaiDuVo.BillBasic words = BaiDuUtil.billBasic(BasicsConstant.BaiDu.INVOICE_APP_KEY, BasicsConstant.BaiDu.INVOICE_SECRET_KEY, basic); |
|
|
|
log.info("识别结果:{}",words); |
|
|
|
// String token = BaiDuUtil.getToken(BasicsConstant.BaiDu.INVOICE_APP_KEY, BasicsConstant.BaiDu.INVOICE_SECRET_KEY);
|
|
|
|
// String invoiceUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice?access_token={}";
|
|
|
|
// String url = StrUtil.format(invoiceUrl, token);
|
|
|
|
// String result = RestTemplateUtil.postUrlEncode(url, basic);
|
|
|
|
// log.info("result:{}", result);
|
|
|
|
// 返回数据
|
|
|
|
OcrVo.BillInfo personMsg = new OcrVo.BillInfo(); |
|
|
|
OcrVo.BillInfo billInfo = new OcrVo.BillInfo(); |
|
|
|
if (CollectionUtil.isNotEmpty(words.getWordsResult())) { |
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd"); |
|
|
|
BeanUtil.copyProperties(words.getWordsResult().get(0),billInfo); |
|
|
|
billInfo.setTotalAmount(new BigDecimal(words.getWordsResult().get(0).getTotalAmount()).multiply(BigDecimal.valueOf(100)).longValue()); |
|
|
|
billInfo.setTotalTax(new BigDecimal(words.getWordsResult().get(0).getTotalTax()).multiply(BigDecimal.valueOf(100)).longValue()); |
|
|
|
billInfo.setInvoiceDate(sdf.parse(words.getWordsResult().get(0).getInvoiceDate()).getTime()); |
|
|
|
|
|
|
|
} |
|
|
|
// personMsg.toMsg(words.getWordsResult());
|
|
|
|
return personMsg; |
|
|
|
return billInfo; |
|
|
|
} |
|
|
|
} |
|
|
|