近来在开发一个视力筛查电子报告系统的产品,这个产品的作用是自动提取视力筛查过程中得到的屈光检查数据,并结合数据自动生成通俗易懂且专业的电子报告,以方便家长可以通过公众号或H5链接查阅。
要实现这个需求,第一步是对验光设备里打印出来的纸质报告做OCR,图片识别接口返回的是二维数组,报告的原图是这样的:
OCR接口返回的数据是这样的
array(3) { [\"words_result\"]=> array(36) { [0]=> array(1) { [\"words\"]=> string(8) \"FA-6000A\" } [1]=> array(1) { [\"words\"]=> string(10) \"2022-09-16\" } [2]=> array(1) { [\"words\"]=> string(7) \"04:00\" } [3]=> array(1) { [\"words\"]=> string(8) \"SHOP:B\" } [4]=> array(1) { [\"words\"]=> string(7) \"NAME:\" } [5]=> array(1) { [\"words\"]=> string(3) \"<R>\" } [6]=> array(1) { [\"words\"]=> string(1) \"C\" } [7]=> array(1) { [\"words\"]=> string(1) \"A\" } [8]=> array(1) { [\"words\"]=> string(5) \"-1.50\" } [9]=> array(1) { [\"words\"]=> string(5) \"-0.25\" } [10]=> array(1) { [\"words\"]=> string(3) \"131\" } [11]=> array(1) { [\"words\"]=> string(5) \"-1.50\" } [12]=> array(1) { [\"words\"]=> string(7) \"-0,25\" } [13]=> array(1) { [\"words\"]=> string(3) \"122\" } [14]=> array(1) { [\"words\"]=> string(7) \"-1,50\" } [15]=> array(1) { [\"words\"]=> string(7) \"-0,25\" } [16]=> array(1) { [\"words\"]=> string(3) \"114\" } [17]=> array(1) { [\"words\"]=> string(5) \"-1.50\" } [18]=> array(1) { [\"words\"]=> string(7) \"-0,25\" } [19]=> array(1) { [\"words\"]=> string(3) \"122\" } [20]=> array(1) { [\"words\"]=> string(3) \"<L>\" } [21]=> array(1) { [\"words\"]=> string(1) \"C\" } [22]=> array(1) { [\"words\"]=> string(1) \"A\" } [23]=> array(1) { [\"words\"]=> string(5) \"-1.50\" } [24]=> array(1) { [\"words\"]=> string(4) \"+0.0\" } [25]=> array(1) { [\"words\"]=> string(5) \"-1.25\" } [26]=> array(1) { [\"words\"]=> string(7) \"-0,25\" } [27]=> array(1) { [\"words\"]=> string(3) \"158\" } [28]=> array(1) { [\"words\"]=> string(5) \"-1.00\" } [29]=> array(1) { [\"words\"]=> string(5) \"-0.25\" } [30]=> array(1) { [\"words\"]=> string(3) \"100\" } [31]=> array(1) { [\"words\"]=> string(1) \"*\" } [32]=> array(1) { [\"words\"]=> string(5) \"-1.25\" } [33]=> array(1) { [\"words\"]=> string(4) \"+0.0\" } [34]=> array(1) { [\"words\"]=> string(5) \"U0=12\" } [35]=> array(1) { [\"words\"]=> string(5) \"PD=58\" } } [\"words_result_num\"]=> int(36) [\"log_id\"]=> int(1455742838110100386) }
而系统的需求是提取两个号后面的两个数字,那肯定是对上述数组做遍历处理,然后遇到号便提取接下来的两个元素,但在foreach里面,如果做标记,等下次进来时再提取数据比较麻烦,能不能在遇到*号字符串后,直接提取接下来的两个字符串呢,这时我的脑海里出现了迭代器的概念,可能是之前用python或java开发时接触到的吧,于是搜索了一下,果然PHP也是有迭代器的!!!
接下来简单看了一下PHP文档中的示例,就开始干了,很顺利,5分钟完工,下面把代码贴出来并辅以简单的注释帮助大家理解:
$usefulNumList = []; $wordsResult = new \\ArrayIterator($wordsResult);//初始化数组迭代器,传入数组变量 foreach($wordsResult as $item){ $tempWords = $item[\'words\']; if(strpos($tempWords, \'*\') !== false){ if($tempWords === \'*\'){//有时候,*号会单独识别成一个字符串,有时候会和后面的数字识别到一起,如果是单独识别出来的,要把指针向后挪一位 $wordsResult->next();//实现方法是: 数组变更名->next()方法 } //注意,调用了next()方法后,不能再用$item去取数组元素值,要用current()方法才能取到\"下一个值\" array_push($usefulNumList, $this->getCleanNum($wordsResult->current()[\'words\'])); $wordsResult->next(); array_push($usefulNumList, $this->getCleanNum($wordsResult->current()[\'words\'])); } }
需注意的地方请看一下代码注释,本身封装得很好,很容易理解和调用的
总结
© 版权声明
THE END
暂无评论内容