<?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说明识别失败了,是失败原因。