# 百度云人工智能文字识别接口测试代码
<?php
// 引入文字识别OCR SDK
require_once '../AipOcr.php';
// 定义常量
const APP_ID = '####423';
const API_KEY = '##########3KoGarePRb';
const SECRET_KEY = '#################7uZzHQ8cScvDHE';
// 初始化
$aipOcr = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
// 身份证识别
// echo json_encode($aipOcr->idcard(file_get_contents('idcard.jpg'), true), JSON_PRETTY_PRINT);
// 银行卡识别
// echo json_encode($aipOcr->bankcard(file_get_contents('bankcard.jpg')));
// 通用文字识别(含文字位置信息)
// echo json_encode($aipOcr->general(file_get_contents('general.png')));
// 通用文字识别(不含文字位置信息)
// echo json_encode($aipOcr->basicGeneral(file_get_contents('general.png')), JSON_PRETTY_PRINT);
// 网图OCR识别
// echo json_encode($aipOcr->webImage(file_get_contents('general.png')), JSON_PRETTY_PRINT);
// 生僻字OCR识别
// echo json_encode($aipOcr->enhancedGeneral(file_get_contents('general.png')), JSON_PRETTY_PRINT);
// 行驶证识别
// echo json_encode($aipOcr->vehicleLicense(file_get_contents('vehicleLicense.jpg')), JSON_PRETTY_PRINT);
// 驾驶证
// echo json_encode($aipOcr->drivingLicense(file_get_contents('drivingLicense.jpg')), JSON_PRETTY_PRINT);
// 车牌
// echo json_encode($aipOcr->licensePlate(file_get_contents('licensePlate.jpg')), JSON_PRETTY_PRINT);
//////////////////
function abslength($str)
{
if(empty($str)){
return 0;
}
if(function_exists('mb_strlen')){
return mb_strlen($str,'utf-8');
}
else {
preg_match_all("/./u", $str, $ar);
return count($ar[0]);
}
}
$file = $_FILES['file'];//得到传输的数据
//得到文件名称
$name = $file['name'];
$type = strtolower(substr($name,strrpos($name,'.')+1)); //得到文件类型,并且都转化成小写
$allow_type = array('jpg','jpeg','gif','png'); //定义允许上传的类型
//判断文件类型是否被允许上传
if(!in_array($type, $allow_type)){
//如果不被允许,则直接停止程序运行
return ;
}
//判断是否是通过HTTP POST上传的
if(!is_uploaded_file($file['tmp_name'])){
//如果不是通过HTTP POST上传的
return ;
}
$upload_path = "C:/wamp64/www/ocr/demo/"; //上传文件的存放路径
//开始移动文件到相应的文件夹
if(move_uploaded_file($file['tmp_name'],$upload_path.$file['name'])){
// echo "Successfully!";
// echo $file['name'];
$res=$aipOcr->webImage(file_get_contents($file['name']));
$txt='';
foreach($res as $k=>$v){
if(is_array($v))
foreach($v as $vv){
if(is_array($vv))
foreach($vv as $vvv){
$txt.=$vvv;
}
}
}
$len = abslength($txt);
//header("Content-Type: application/doc");
//header("Content-Disposition: attachment; filename=" . 'printtxt' . ".doc");
//$txt = iconv("UTF-8","GB2312//IGNORE",$txt);
echo "\r\n".'-----字数'.$len;
echo "\r\n\r\n\r\n\r\n\r\n";
echo $txt;
unlink($file['name']);
}else{
// echo "Failed!";
}
?>