thinkphp6+框架腾讯云ocr接入,包括身份证、营业执照、银行卡的识别

<?php

namespace app\common\controller;

use TencentCloud\Common\Credential;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Ocr\V20181119\Models\BankCardOCRRequest;
use TencentCloud\Ocr\V20181119\Models\BizLicenseOCRRequest;
use TencentCloud\Ocr\V20181119\Models\IDCardOCRRequest;
use TencentCloud\Ocr\V20181119\OcrClient;
use think\facade\Db;
use think\facade\Request;

class Ocr
{
    protected $client;
    protected $domain;
    public function __construct()
    {
        $setting=Db::name('rely_setting')->find(1);

        $this->cred = new Credential($setting['tencent_id'], $setting['tencent_key']);
        $httpProfile = new HttpProfile();
        $httpProfile->setEndpoint("ocr.tencentcloudapi.com");

        $clientProfile = new ClientProfile();
        $clientProfile->setHttpProfile($httpProfile);
        $this->client = new OcrClient($this->cred, "ap-beijing", $clientProfile);

        $this->domain=Request::domain();
    }
    public function id_card($url)
    {
        $req = new IDCardOCRRequest();
        $req->fromJsonString(json_encode(['ImageUrl'=>$this->domain.$url,]));
        try{
            $resp = $this->client->IDCardOCR($req);
            return json_decode($resp->toJsonString(),true);
        }catch (TencentCloudSDKException $e){
            return $e->getMessage();
        }
    }

    public function license($url)
    {
        $this->url=$url;
        $req = new BizLicenseOCRRequest();
        $req->fromJsonString(json_encode(['ImageUrl'=>$this->domain.$url,]));
        try {
            $resp = $this->client->BizLicenseOCR($req);
            return json_decode($resp->toJsonString(),true);
        }
        catch(TencentCloudSDKException $e) {
            return $e->getMessage();
        }
    }

    public function bank_card($url)
    {
        $this->url=$url;
        $req = new BankCardOCRRequest();
        $req->fromJsonString(json_encode(['ImageUrl'=>$this->domain.$url,]));
        try {
            $resp = $this->client->BankCardOCR($req);
            return json_decode($resp->toJsonString(),true);
        }
        catch(TencentCloudSDKException $e) {
            return $e->getMessage();
        }
    }

}

给到的是一张图片的地址,就是每个方法的$url,这个图片是保存在服务器里的,所以传递的时候要加上域名。
返回值有两种,arr返回说明已经解析成功了,返回来的是识别出来的数据,一个数组。string说明识别失败了,是失败原因。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值