thinkphpは、多次元配列を2次元配列に取り出すBaiduocr関数を追加します

 

お客様がアップロードした写真の韓国語の文字を取り出すように要求しました

 


    public function index()
    {
		//if(isset($_POST['sub']))
       // {
            $token=$this->curl("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=xxxxxxx&client_secret=xxxxxxxx");
            $token=json_decode($token,true);
            //$token=$token['access_token'];      //获取token
            $image = '    ';                  //用户上传的图片
            $app_id="xxxx";         //你的appid
            $api_key="xxxx";       //你的api_key
            $secret_key="xxxxN";         //你的secret_key
            vendor('Ocr.AipOcr');       //类库引入
            $client = new \AipOcr($app_id,$api_key,$secret_key);        //实例化AipOcr类
            // 如果有可选参数
            $options = array();
	
            //$options["language_type"] = "CHN_ENG";
			$options["language_type"] = "KOR";
            $options["detect_direction"] = "false";
            $options["detect_language"] = "false";
            $options["probability"] = "false";
            $options["access_token"]=$token;            //获取token
            // 带参数调用通用文字识别, 图片参数为远程url图片
            $data = $client->basicGeneralUrl($image, $options);        //调用通用文字识别接口  
		    
			// 把$data 转换成二维数组
			$i= 0;
			$j= $data['words_result_num'];
			$hanwen=array();
			for($i=0;$i<$j;$i++){
				$hanwen[$i]=$data['words_result'][$i]['words'];
			}
			$this->assign('hanwen',$hanwen);
			return $this->fetch('index');
    }

    public function curl($url,$postData=[],$headers=[]){
        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);      //要访问的地址
        curl_setopt($ch,CURLOPT_HEADER,0);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);     //执行结果是否被返回,0返,1不返
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
        if($postData){
            curl_setopt($ch,CURLOPT_TIMEOUT,60);
            curl_setopt($ch,CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$postData);
        }
        if(curl_exec($ch)==false){
            $data='';
        }
        else{
            $data=curl_multi_getcontent($ch);
        }
        curl_close($ch);javascript:;
        return $data;

テンプレートファイル:テンプレート呼び出しコード

<style> 
body{ text-align:center} 
.div{ margin:0 auto; width:400px; height:100px; border:1px solid #F00 font-size:90px; } 
</style> 
<div id="hanwen">

{foreach $hanwen as $key=>$wo}
{$wo}
&nbsp;&nbsp;&nbsp;
{/foreach}
</div>

 

おすすめ

転載: blog.csdn.net/hsu282/article/details/112374084