腾讯云增值税发票OCR

开通文字识别SDK

在这里插入图片描述

创建秘钥

在这里插入图片描述

引入sdk依赖

我的项目管理工具是Gradle Maven项目到Maven仓库复制就好

 implementation group: 'com.tencentcloudapi', name: 'tencentcloud-sdk-java', version: '3.1.296'

核心代码

public class VatInvoiceOCRUtils {

    private String secretId = "";

    private String secretKey = "";

    private String invoiceOcrUrl = "ocr.tencentcloudapi.com";

    public Map<String, String> vatInvoiceNo(String imgUrl) throws TencentCloudSDKException {
        // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
        // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取           
         Credential cred = new Credential(secretId, secretKey);
        // 实例化一个http选项,可选的,没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint(invoiceOcrUrl);
        // 实例化一个client选项,可选的,没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        VatInvoiceOCRRequest req = new VatInvoiceOCRRequest();
        req.setIsPdf(true);
         // 实例化要请求产品的client对象,clientProfile是可选的
        OcrClient client = new OcrClient(cred, "ap-shanghai", clientProfile);
        req.setImageUrl(imgUrl);
        // 返回的resp是一个VatInvoiceOCRResponse的实例,与请求对象对应
        VatInvoiceOCRResponse resp = client.VatInvoiceOCR(req);
        TextVatInvoice[] invoiceInfos = resp.getVatInvoiceInfos();
        List<String> textNameArray = new ArrayList<>();
        Collections.addAll(textNameArray,"购买方名称", "购买方识别号","发票号码","发票代码","开票日期","金额");
        Map<String,String> resultMap = new HashMap<>();
        Arrays.stream(invoiceInfos).forEach(item->{
            if (textNameArray.contains(item.getName()) && StrUtil.isNotEmpty(item.getValue())){
                resultMap.put(item.getName(),item.getValue());
            }
        });
            return resultMap;
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值