<?php
/**
*图像识别
*/
class ocrAction extends baseAction
{
	/**
	  * @method get vin识别
	  * @http  POST
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  image[旧]          array  ['name'=>'ocr.jpg','data'=>base64_encode(file_get_contents('D:\www\ocr/ocr.jpg'))]
	  * @param  image[新]          string  base64_encode(file_get_contents('D:\www\ocr/ocr.jpg'))
	  * @param  url                string [image、url二选一] 图片URL
	  * @param  issmall           int [选填] 是否为裁剪后的图 1|0 默认0
	  * @author soul
	  * @copyright 2017/9/30
	  * @return 
	  {"status":true,"data": [],"code":""}
	  {"status":false,"data":'失败原因',"code":''}
	  */
	public function vin()
	{
		$MainOcr = new MainOcr();
		if(!empty($_POST['image']))
		{
			$imgdata = is_array($_POST['image']) && !empty($_POST['image']['data'])? $_POST['image']['data']: $_POST['image'];
		}
		else if(!empty($_POST['url']))
		{
			$LibHttp = new LibHttp();
			$imgdata = base64_encode($LibHttp->fetch($_POST['url']));
		}
		$res = $MainOcr->vin($imgdata, $_POST);
		echo json_encode($res);
	}

	/**
	  * @method 飞浆OCR
	  * @param  image     string  [必填] 图片内容base64
	  * @param  type      string  [选填] 空(默认)只返回文本  all:返回位置信息
	  * @param  rotate     int    [选填] 旋转角度 默认不旋转 0~360
	  * @author soul
	  * @copyright 2022/12/12
	  * @return
	  		{"status":true,"data":["仓库","车辆识别代号：LSGGA54Y4EH192450","整车型号：SGM7201EAAB","制造年月：14-05","克","乘坐人数：5","型号：LTD","最大允许总质量","2020kg","排量：1.998L","最大净功率：108kW","上海通用汽车有限公司制造","品一窗","可","Q","65%","?","回","C","中","L","CPU二度"],"code":0,"runtime":161}

			{"status":true,"data":[{"text":"仓库","match":"0.79227614","pos":[[640,264],[670,264],[670,282],[640,282]]},{"text":"车辆识别代号：LSGGA54Y4EH192450","match":"0.942183","pos":[[91,547],[470,544],[470,573],[91,575]]},{"text":"整车型号：SGM7201EAAB","match":"0.9753281","pos":[[97,583],[377,571],[378,603],[98,615]]},{"text":"制造年月：14-05","match":"0.9219615","pos":[[359,605],[487,596],[489,624],[361,633]]},{"text":"克","match":"0.9957141","pos":[[0,628],[26,628],[26,663],[0,663]]},{"text":"乘坐人数：5","match":"0.9403195","pos":[[363,635],[465,624],[467,652],[365,663]]},{"text":"型号：LTD","match":"0.94944257","pos":[[0,665],[140,652],[143,692],[0,705]]},{"text":"最大允许总质量","match":"0.88738835","pos":[[370,666],[491,649],[494,677],[373,694]]},{"text":"2020kg","match":"0.9865563","pos":[[377,694],[449,686],[452,716],[380,724]]},{"text":"排量：1.998L","match":"0.92205644","pos":[[0,712],[200,683],[204,719],[4,748]]},{"text":"最大净功率：108kW","match":"0.89953727","pos":[[4,758],[265,713],[270,745],[8,789]]},{"text":"上海通用汽车有限公司制造","match":"0.95367736","pos":[[80,791],[508,701],[514,737],[86,827]]},{"text":"品一窗","match":"0.575452","pos":[[8,964],[90,966],[90,991],[8,989]]},{"text":"可","match":"0.49454513","pos":[[162,972],[191,972],[191,996],[162,996]]},{"text":"Q","match":"0.73591346","pos":[[203,974],[228,974],[228,997],[203,997]]},{"text":"65%","match":"0.9973398","pos":[[248,978],[283,978],[283,998],[248,998]]},{"text":"?","match":"0.48960602","pos":[[303,978],[329,978],[329,1000],[303,1000]]},{"text":"回","match":"0.36363515","pos":[[341,978],[369,982],[368,1004],[340,1000]]},{"text":"C","match":"0.39547032","pos":[[397,982],[426,982],[426,1005],[397,1005]]},{"text":"中","match":"0.6727968","pos":[[479,984],[515,984],[515,1006],[479,1006]]},{"text":"L","match":"0.21274662","pos":[[514,989],[544,989],[544,1007],[514,1007]]},{"text":"CPU二度","match":"0.6933483","pos":[[588,1042],[632,1045],[632,1058],[588,1055]]}],"code":0,"runtime":128}
	  */
	public function paddle()
	{
		$MainOcr = new MainOcr();
		$ocrCfg = [
			'type'=>$_POST['type'],
			'rotate'=>$_POST['rotate'],
			'style'=>$_POST['style']
		];
		$res = $MainOcr->paddleOcr($_POST['image'], $ocrCfg);
		LibFc::EchoJson($res);
	}


	/**
	  * @method 获取图片的角度
	  * @param  image     string  [必填] 图片内容base64
	  * @author soul
	  * @copyright 2024/8/16
	  * @return
	  */
	public function angle()
	{
		$MainOcr = new MainOcr();
		$res = $MainOcr->getAngle($_POST['image']);
		LibFc::EchoJson($res);
	}

	/**
	  * @method 获取vin图片的类别
	  * @param  image     string  [必填] 图片内容base64
	  * @author soul
	  * @copyright 2024/8/16
	  * @return
		"mp"=>'铭牌',
		"xsz"=>"行驶证",
		"vin"=>'仅VIN',
		"cp"=>'车牌',
		"hgz"=>'合格证',
		"djzs"=>'登记证书',
		"fp"=>'发票',
		"tool"=>'应用工具',
		"oth"=>'其它',
	  */
	public function vinCls()
	{
		$MainOcr = new MainOcr();
		$res = $MainOcr->vinCls($_POST['image']);
		LibFc::EchoJson($res);
	}


	/**
	  * @method get 通用文字识别
	  * @http  POST
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  image              array  [image、url二选一] ['name'=>'ocr.jpg','data'=>base64_encode(file_get_contents('D:\www\ocr/ocr.jpg'))]
	  * @param  url                string [image、url二选一] 图片URL
	  * @param  type               int   默认1普通  2高精度
	  * @author soul
	  * @copyright 2017/9/30
	  * @return 
	  {"status":true,"data": [],"code":""}
	  {"status":false,"data":'失败原因',"code":''}
	  */
	public function general()
	{
		$MainOcr = new MainOcr();
		$res = $MainOcr->general($_POST['image'], $_POST['url'], $_POST['type']);
		echo json_encode($res);
	}
	

	/**
	  * @method 易车网图片识别
	  * @http  POST
	  * @param  token              string [必填] 调用接口凭证 (post|get)
	  * @param  image              array  [image、url二选一] ['name'=>'ocr.jpg','data'=>base64_encode(file_get_contents('D:\www\ocr/ocr.jpg'))]
	  * @param  url                string [image、url二选一] 图片URL
	  * @author soul
	  * @copyright 2017/9/30
	  * @return 
	  {"status":true,"data": [],"code":""}
	  {"status":false,"data":'失败原因',"code":''}
	  */
	public function yicheVinImg()
	{
		$arr = [];
		$MainOcr = new MainOcr();
		$res = $MainOcr->general($_POST['image'], $_POST['url']);
		$arr['word'] = $res['data'];
		$str = implode(' ', $arr['word']);
		if(stripos($str, '制造') !== false || stripos($str, '型号') !== false || stripos($str, '排量') !== false)
		{
			$arr['mingpai'] = 1;
			if(preg_match('/[0-9a-zA-Z]{17}/', $str, $mat))
			{
				$arr['vin'] = $mat[0];
				$YpwVin = new YpwVin();
				$YpwVin->collOcrVinImg($arr['vin'],$_POST['image']);
			}
			else
			{
				$res = $MainOcr->vin($_POST['image']['data']);
				$arr['vin'] = $res['data'];
			}
		}
		LibFc::ReturnJson(true, $arr);
	}

	
	/**
	  * @method 验证码识别
	  * @http  POST
	  * @param type  类型  http://www.fateadm.com/price.html
	  * @param imgdata  图片内容 base64_encode(file_get_contents('D:\www/kaptcha.jpg'))
	  * @author soul
	  * @copyright 2020/1/16
	  * @return 
	  */
	public function imgCodeOcr()
	{
		$OthCodeOcr = new OthCodeOcr();
		$res = $OthCodeOcr->getCode($_POST['type'], $_POST['imgdata']);
		echo json_encode($res);
	}

	/**
	  * @method 验证码识别退款
	  * @http  get
	  * @param RequestId  string  
	  * @author soul
	  * @copyright 2020/1/16
	  * @return 
	  */
	public function refundImgCodeOcr()
	{
		$Params = mvc::$URL_PARAMS;
		$OthCodeOcr = new OthCodeOcr();
		$res = $OthCodeOcr->Justice($Params['RequestId']);;
		LibFc::EchoJson($res);
	}

	/**
	  * @method 验证码识别[新]
	  * @http  POST
	  * @param imgdata  图片内容 base64_encode(file_get_contents('D:\www/kaptcha.jpg'))
	  * @param type  类型 默认 3 数英混合
	  * @param site  类型 默认 ttshitu
	  * @author soul
	  * @copyright 2020/1/16
	  * @return {"status":true,"data":{"id":"cf115dc0c3a945d1b849ad2250a123c8","imgcode":"g4jfwh","site":"ttshitu"},"code":0,"runtime":155}
	  */
	function getCodeOcr()
	{
		$OthCodeOcr = new OthCodeOcr();
		$res = $OthCodeOcr->getCodeOcr($_POST['imgdata'], $_POST['type'], $_POST['site']);
		LibFc::EchoJson($res);
	}

	/**
	  * @method 验证码识别 报错退款[新]
	  * @http  get
	  * @param id   
	  * @param site  默认 ttshitu
	  * @author soul
	  * @copyright 2020/1/16
	  * @return 
	  */
	function refundCodeOcr()
	{
		$Params = mvc::$URL_PARAMS;
		$OthCodeOcr = new OthCodeOcr();
		$res = $OthCodeOcr->refundCodeOcr($Params['id'], $Params['site']);
		LibFc::EchoJson($res);
	}

	/**
	  * @method 验证码识别[新]
	  * @http  POST
	  * @param imgdata string 图片内容 base64_encode(file_get_contents('D:\www/kaptcha.jpg'))
	  * @param result string {"type":"cf115dc0c3a945d1b849ad2250a123c8","imgcode":"g4jfwh","site":"ttshitu"}
	  * @author soul
	  * @copyright 2020/1/16
	  * @return 
	  */
	function logCodeOcrError()
	{
		$OthCodeOcr = new OthCodeOcr();
		$res = $OthCodeOcr->logCodeOcrError($_POST['imgdata'], json_decode($_POST['result'], true));
		LibFc::EchoJson($res);
	}

	
	/**
	  * @method 百度本地识别
	  * @http  POST
	  * @param  image  [必填]  base64_encode(file_get_contents('D:\www\ocr/ocr.jpg'))
	  * @param  rotate [选填]  旋转角度 90 180 270
	  * @author soul
	  * @copyright 2021/12/4
	  * @return 
	  {"status":true,"data": [],"code":""}
	  {"status":false,"data":'失败原因',"code":''}
	  */
	public function baiduLocalOcr()
	{
		$OthBaiduOcr = new OthBaiduOcr();
		$res = $OthBaiduOcr->ocr($_POST);
		LibFc::EchoJson($res);
	}


	/**
	  * @method 车牌/行驶证/铭牌、车架号识别
	  * @http  POST
	  * @param  token          string [必填] 调用接口凭证 (post|get)
	  * @param  type           string [必填] numplate:车牌 driving:行驶证 vin:铭牌 车架号
	  * @param  image          string [image、url二选一] base64_encode(file_get_contents('D:\www\ocr/ocr.jpg'))
	  * @param  url            string [image、url二选一] 图片URL
	  * @author soul
	  * @copyright 2023/9/23
	  * @return 
		numplate:{"status":true,"data":{"color":"blue","numplate":"京NAV888"},"code":0,"runtime":418}
		driving:{"status":true,"data":{"numplate":"京AA5599","vin":"HHGHH555778802669","addr":"北京市石景山区","gxb":"讴歌牌GHA7150JAD5B","vtype":"小型轿车","owner":"王京","usetype":"非营运","engnum":"112244"},"code":0,"runtime":1082}
		vin:{"status":true,"data":{"vin":"HHGHH555778802669"},"code":0,"runtime":2268}
	  */
	public function vehicle()
	{
		$MainOcr = new MainOcr();
		$res = $MainOcr->vehicle($_POST['type'], $_POST['image'], $_POST['url']);
		LibFc::EchoJson($res);
	}
}