现在还只是初学阶段,能做的还只是比较普通的阶段,就是验证码只是普通的一些噪点,验证码的文字没有出现扭曲或者是出现旋转的这些情况。

下面直接上代码,主代码

<?php
header('Content-type:text/html;charset=utf-8');

define('WORD_SPACING',9);//字符间距
define('LEFT',10);
define('TOP',9);
define('HEIGHT',15);
define('WIDTH',10);
define('COLOR', 215);//二值化阀值

//$filePath = $_GET&#91;'p'&#93;.'.png';
$filePath = './1.png';
list($width,$height) = getimagesize($filePath);
$rs = imagecreatefrompng($filePath);
$sourceData =array();

//取特征值
for ($i=0; $i < $height; $i++) { 
	for ($j=0; $j < $width; $j++) { 
		$index = imagecolorat($rs, $j, $i);
		$rgb = imagecolorsforindex($rs, $index);
		//print_r($rgb);
		if ($rgb&#91;'red'&#93;>COLOR && $rgb['blue']>COLOR && $rgb['green']>COLOR) {
			//echo '1';
			$sourceData[$i][$j]=0;	
		}else{
			//echo '0';
			$sourceData[$i][$j]=1;
		}
	}
}

//去噪点 阀值5
function clear($sourceData){
	$desData = array();
	$h =count($sourceData,0);
	$w =count($sourceData[0]);

	for ($i=1; $i < $h-1; $i++) { 
		for ($j=1; $j < $w-1; $j++) { 
			$value = $sourceData&#91;$i-1&#93;&#91;$j&#93;+$sourceData&#91;$i+1&#93;&#91;$j&#93;+$sourceData&#91;$i&#93;&#91;$j-1&#93;+$sourceData&#91;$i&#93;&#91;$j+1&#93;
                +$sourceData&#91;$i-1&#93;&#91;$j-1&#93;+$sourceData&#91;$i+1&#93;&#91;$j+1&#93;+$sourceData&#91;$i-1&#93;&#91;$j+1&#93;+$sourceData&#91;$i+1&#93;&#91;$j-1&#93;;
			if ($value>=5) {
				$desData[$i-1][$j-1] = 1;
			}else{
				$desData[$i-1][$j-1] = 0;
			}
		}
	}
	return $desData;
}

//字符分割
function getCH($data){

	//第一个左上角坐标
	$x = LEFT -1;
	$y = TOP -1;

	$ch=0;
	for ($i=$y; $i < $y + HEIGHT; $i++) {
		for ($j=$x; $j < $x + WIDTH; $j++) { 
			$chData&#91;0&#93;&#91;$ch&#93; = $data&#91;$i&#93;&#91;$j&#93;;
			$ch++;
		}
	}

	$ch=0;
	for ($i=$y; $i < $y + HEIGHT; $i++) {
		for ($j=$x + WIDTH + WORD_SPACING; $j < $x + 2 * WIDTH + WORD_SPACING; $j++) { 
			$chData&#91;1&#93;&#91;$ch&#93; = $data&#91;$i&#93;&#91;$j&#93;;
			$ch++;
		}
	}

	$ch=0;
	for ($i=$y; $i < $y + HEIGHT; $i++) {
		for ($j=$x + 2*(WIDTH + WORD_SPACING); $j < $x + 3 * WIDTH + 2 * WORD_SPACING; $j++) { 
			$chData&#91;2&#93;&#91;$ch&#93; = $data&#91;$i&#93;&#91;$j&#93;;
			$ch++;
		}
	}

	$ch=0;
	for ($i=$y; $i < $y + HEIGHT; $i++) {
		for ($j=$x + 3*(WIDTH + WORD_SPACING); $j < $x + 4 * WIDTH + 3 * WORD_SPACING; $j++) { 
			$chData&#91;3&#93;&#91;$ch&#93; = $data&#91;$i&#93;&#91;$j&#93;;
			$ch++;
		}
	}

	return $chData;
}

/*显示
foreach ($desData as $value) {
	foreach ($value as $v) {
		if($v==1){
			echo '■';
		} else{
			echo '□';
		}//echo $v;
	}
	echo "<br />";
}*/

/*学习功能 制作字模库
foreach ($chData as $value) {
	echo "'";
	foreach ($value as $v) {
		echo $v;
	}
	echo "',<br />";
}*/

//识别验证码
function vertifyCode($chData){
	//字模
$typehead = require './zimo.php';

	$ch = array();
	$w = count($chData[0]);
	/*单个字符识别
	for ($i=0; $i < 10; $i++) { 
		$mount=0;
		for ($j=0; $j < $w; $j++) { 
			if ($chData&#91;0&#93;&#91;$j&#93;==$typehead&#91;$i&#93;&#91;$j&#93;) {
				$mount++;
			}
		}
		if ($w-$mount<15) {
			$ch&#91;0&#93;=$i;
			break;
		}
	}*/

	for ($i=0; $i < 4; $i++) { 
		$result = array();
		for ($k=0; $k < 10; $k++) { 
			$mount = 0;
		if (!is_array($typehead&#91;$k&#93;)) {
			for ($j=0; $j < $w; $j++) {
				/*
				if (is_array($typehead&#91;$k&#93;)) {
					$subMount = 0;
					foreach ($typehead&#91;$k&#93; as $v) {
						for ($m=0; $m < $w; $w++) { 
							if ($chData&#91;$i&#93;&#91;$m&#93; == $v&#91;$m&#93;) {
								$subMount++;
							}
						}
						$sub&#91;&#93;=$subMount;
					}
					$mount = max($sub);

				} else {*/
					if ($chData&#91;$i&#93;&#91;$j&#93;==$typehead&#91;$k&#93;&#91;$j&#93;) {
						$mount++;
					}
				//}
			}
		}else{
			foreach ($typehead&#91;$k&#93; as $v) {
				$subMount = 0;
				$sub = array();
				for ($m=0; $m < $w; $m++) { 
					if ($chData&#91;$i&#93;&#91;$m&#93; == $v&#91;$m&#93;) {
						$subMount++;
					}
				}
				$sub&#91;&#93;=$subMount;
			}
			$mount = max($sub);
		}

			if ($w-$mount<3) {
				$ch&#91;$i&#93;=$k;
				break;
			}else{
				$result&#91;$k&#93;=$w-$mount;
			}
		}
		//未匹配成功则取最优结果
		if (!isset($ch&#91;$i&#93;)) {
			$ch&#91;$i&#93;=current(array_keys($result,min($result)));
		}
	}

	return $ch;
}

$desData = clear($sourceData);
$chData = getCH($desData);
$digtal = vertifyCode($chData);

$resul = '';
foreach ($digtal as $v) {
	$resul .= $v;
}

//输出识别结果
echo '识别结果:',$resul;

/*test
foreach ($desData as $value) {
	foreach ($value as $v) {
		echo $v;
	}
	echo "<br />";
}
*/

/*test
$im = imagecreate($width-OFFSET_X-RIGHT, $height - OFFSET_Y - BOTTOM);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
$h = count($desData,0);
$w = count($desData[0]);

for ($i=0; $i < $h; $i++) { 
	for ($j=0; $j < $w; $j++) { 
		if ($desData&#91;$i&#93;&#91;$j&#93;==1) {
			imagesetpixel($im, $j, $i, $white);
		}
	}
}
header("Content-type: image/jpeg");
imagejpeg($im);
*/

?>

这个是主效果,然后下面这个是字模的页面。

<?php
return array(
	//0
	array(
	'000111100001111111101111001110111000011111000000111100000011110000001111000000111100000011110000011111000001111111000111011101111001111111100001111110',
	'010111110001111111101111001111111100011111000000111100000011110000001111000000111100000011110000011111000001111110000111011100111001111111100001111100',
	'000111110001111111100111001110111000011111000001111100000011110000001111100000111100000011111000011111000000111110000111011100111001111111100111111000',
	'000111110001111111100111001110111000011111000000111100000011110000001111000000111100000011110000011111000001111110000111111100111101111111100001111000',
	'000111100001111111100111001110111000011111000000111100000011110000001111000000111100000011110000011111100001111110000111111110111011111111100001111000',
	),
	//1
	array(
'000011000000001110000001111000001111100001101110000000011000000001100000000110000000011000000001100000000110000000011000000001100000000110000000000000',
'000000000000001110000001111000001111100000111110000000011000000001100000000110000000011000000011100000000110000000011000000001100000000110000000001000',
'000000000000001110000001111100001111100000101110000000011000000011100000000110000000111000000001100000000110000000011000000001100000000110000000001100',
'000001000000001110000001111000001111100001101110000000011000000001100000000110000000011000000001100000000110000000011000000001100000000110000000000000',
'000000000000001110000001111000001111100000101110000000011000000001110100000110000000011000000001100000000110000000011000000001100000000110000000010000',
'000000000000001110000001111000001111100000101110000000011000000001100000000110000000011000000001100000000111000000011100000001110000000111000000000000',
	),
	//2
	array(
'000111100001111111101110111110000000111100000001110000000011000000011100000011100000011100000011100000011100000011100000011110001001111111100111111110',
'001111100001111111100110111111000000011100000001110000000011000000011100000011100000011100000011100000011100000011100000011110101011111111100111111110',
'000111100001111111000111011110010000011100000001100000000010000000111000000011100000011100000011100000011100000011110000011111000001111111100111111110',
		),
	//3
	array(
'001111000001111111001111001110010000011000000011100000001100000001111000000111100000011110000000011100000011110100001111011101111001111111100001111000',
'000111100001111111001111001110110000111000000011100000011100000001111000000111100000001110000000001100100001110100000111011101111001111111110001111110',
'000111000001111111001111001100011100011000000011100000111110000001111000000111100000001110000000011100000001110100001111111100111001111111100011111000',
'000111000001111111000111111100011000011000000011100000001100000001111000000111100000001110000000001100000001110100000111011101111001111111100001111000',
'001111000001111111000111111100011000011000000011100000001100000001111000000111100000001110000000011100000001111100000111111100111001111111100001111000',
		),
	//4
	array(
'000000000000000011000000111100000011110000011111000001111100001100110001100011001111111110111111111001111111110000011110000000110000000011000000000000',
'000000000000000011000000011100000011110000011111000001111100001100110001100011000111011110111111111001111111100000011110000000110000000011000000000000',
'000000100000000011000000011100000011110000011111000001111100001100110001100011000111011110111111111001111111100101011110000000110000000011000000000000',
'000000000000000011000000011100000011110000011111000001111100001100110001100011100111011110111111111101111111100000011110000000111000000011000000000000',
'000000000000000011100000011100000011111000011111000011111100011111110001100011000111011110111111111001111111100000011110000000110000000011000000000000',
'000000000000000011000000011100000011110000011111100001111100001111111001110011001111111110111111111001111111100000011110000000110000000011000000000100',
'000000000000000111000000011100000011110000011111000001111100001100110001100011000111011110111111111001111111100000011110000000110000000011000000000000',
		),
	//5
	array(
'000111110100111111000111100000011000000001111000000111111000111111111001110111100101000111000000001100000001110100000111011001111001111111100001111000',
	),
	//6
	array(
'010111111001111111100111111110011100001011110101101111111100111111111011110011111110000111110000001111000000110110000111011101111000111111100001111000',
'000011100000111111101111011111011100001011100000001111111000111111111011110011101110000111111000011111000001110110001111011110111100111111100001111000',
'001111100000111111100111101110011000001011100000001111111000111111111011110011101110000111110000011111000000110110000111011100111100111111100001111000',
'000011100000111111100111001110011000011011100001001111111100111111111011111011101110000111111000001111000000111111000111111100111001111111100001111000',
'000011100000111111100111101111111100001011111000001111111000111111111011110111101110000111110000011111000001111110000111111110111001111111100011111000',
'000011100000111111101111001111111100001011110000001111111000111111111011110011101110000111111000011111100000110110001111011100111000111111100001111100',
		),
	//7
	array(
'011111111011111111110000001111000000111000000011100000011100000001100000001110000000110000000110000000011000000001100000001110000000111000000000000000',
'011111111001111111110101011111000000111000000011000000111000000011100000001100000011110000000110000000111000000001100000001110000000110000000000000000',
'011111111001111111110000001111000000011000000111100000111000000001100000001100000000110000000110000000011000000001100000001110000000110000000001000000',
'011111111001111111110000101111000000111000000011000000011000000001100000001100000000110000000110000000111000000001100000001110000000111000000000000000',
'011111111001111111110000101110000000111000000011000000011000000011100000011110000000111000000111000000011100000011100000001110000001110000000001000000',
'011111111001111111110000011111000000011000000111100000011100000011100000001110000000110000000110000000011000000011100000001110000001111000000001100000',
		),
	//8
	array(
'000111100000111111001111001110011000011001100001100111001100011111111001111111100111101111111000111111100001111110001111011111111001111111100001111000',
'000111110000111111100111001110011000011001100001100011001100011111111001111111100111001111111000011111000001111110000111011000111001111111100001111000',
'000111100000111111100111001110011000011001100001100111001100011111111001111111101111001110111000011111000001111110000111111001111001111111100111111000',
'000111110000111111100111111110011100011001110001100011001110011111111001111111110111001111111000011111000001111110000111011111111001111111100011111000',
'011111100001111111000111101110011000011001110001110011001110011111111001111111100111001111111000111111000001111110001111011000111001111111100011111000',
'000111110000111111000111101110011000011001110001100111001100011111111001111111100111111110111100011111110001111110000111011000111001111111100001111000',
		),
	//9
	array(
'001111100001111111001111111111111000011111000000111110000011111000011101111011110111111111001111111100100001110110000110011100111001111111000011110000',
'001111100001111111100111001111111000011011100000111100000011111000011111110011110111111111000111111100000001110100000110111100111001111111000001111000',
'001111110001111111000111111110111000011111000000111100000111111000011101110111110111111111001111111100100001110110000110011100111001111111000001111000',
'000111100001111111000111101110111000011011100001111100000011111000111101110011110111111111001111111101010001110110000110111100111001111111000111111000',
'001111100001111111000111001110011000011001100001100011001100011111111001111111100111011110111000011111000001111110000111011000111001111111100011111000',
'001111100011111111001111111110111000011011000000111100000011111000011101110011110111111111000111111100010001110100000110011100111001111111000001111000',
'000111100001111111000111111111111000011011000000111100000111111000011101110111110111111111011111111111100001111110001110111100111001111111000001111000',
'000111111001111111100111101111111000011011100001111100000011111000111111111011110111111111011111111101100001110100000110011100111001111111000001110000',
		),
	);
?>

字模内的最外层的数组有十个,这十个就是对应的十个数字。然后因为验证码里面的每个字的大小规格是width是10个像素,然后height是15个像素,所以最里层的字符串是150个字符长,也就是每一行就是一个字模,然后每个数组里面不是有多组字符串吗?是的,每个数组内有不定的多组字符串,这个主要是每个验证码的字符对应的字模有时候会出现小程度的变化如旋转或者变小等,所以每个字的字模可以对应多个字符串。这个可以直接用一个程序进行转变,就是每个字符串弄成10*15的规格打印到页面上,这个二值化之后零的可以隐藏掉,为什么还要打出来呢?这个主要是为了占用一个和1一样的位置,当然打印空格也是可以的!弄出来就知道为什么了,根据这个还可以进行用php进行对应的图片打印出来字模,用于以后的进行图片识别或者是有些户外的LED等的设计,因为屏幕比较大,打印出来的字需要字模,也是可以利用这个进行设计的!过一段事件就贴上取字模的代码!

分享该文章:

相关文章:

  • 暂无相关文章