<pre><?php
define('WORD_WIDTH',9);
define('WORD_HEIGHT',13);
define('OFFSET_X',7);
define('OFFSET_Y',3);
define('WORD_SPACE',4);
define('WORD_COUNT',4);
//事先定义好的数字模型,用来与二进制验证码对比
$key = array(
'0'=>'000111000011111110011000110110000011110000011110000011110000011110000011110000011110000011011000110011111110000111000',
'1'=>'000111000011111000011111000000011000000011000000011000000011000000011000000011000000011000000011000011111111011111111',
'2'=>'011111000111111100100000110000000111000000110000001100000011000000110000001100000011000000110000000011111110111111110',
'3'=>'011111000111111110100000110000000110000001100011111000011111100000001110000000111000000110100001110111111100011111000',
'4'=>'000001100000011100000011100000111100001101100001101100011001100011001100111111111111111111000001100000001100000001100',
'5'=>'111111110111111110110000000110000000110000000111110000111111100000001110000000111000000110100001110111111100011111000',
'6'=>'000111100001111110011000010011000000110000000110111100111111110111000111110000011110000011011000111011111110000111100',
'7'=>'011111111011111111000000011000000010000000110000001100000001000000011000000010000000110000000110000001100000001100000',
'8'=>'001111100011111110011000110011000110011101110001111100001111100011101110110000011110000011111000111011111110001111100',
'9'=>'001111000011111110111000111110000011110000011111000111011111111001111011000000011000000110010000110011111100001111000',
);
function DisplayBinary($d,$width,$height)
{
for($i = 0;$i < $height;$i++)
{
for($j = 0;$j < $width;$j++)
{
echo $d[$i][$j];
}
echo "<br>";
}
}
/**
* 根据图片生成一个二维数组
*/
function ImageBinary($path)
{
$pixels = array();
$size = getimagesize($path);
$imagewidth = $size[0];$imageheight = $size[1];
$resource = imagecreatefromjpeg($path);
for($i = 0;$i < $imageheight;$i++)
{
for($j = 0;$j < $imagewidth;$j++)
{
$rgb = imagecolorat($resource,$j,$i);
$rgbarray = imagecolorsforindex($resource,$rgb);
if($rgbarray['red'] < 125 || $rgbarray['green'] < 125 || $rgbarray['blue'] < 125)
$pixels[$i][$j] = 1;
else
$pixels[$i][$j] = 0;
}
}
DisplayBinary($pixels,$imagewidth,$imageheight);
return $pixels;
}
/**
* 对二维数组 遍历,得到每一个数字对应的0,1字符串
*/
function getNumDate($a)
{
$data = array();
for($i = 0;$i < WORD_COUNT;$i++)
{
$x = ($i * (WORD_WIDTH+WORD_SPACE) + OFFSET_X);
$y = OFFSET_Y;
for($j = $y;$j < (OFFSET_Y+WORD_HEIGHT);$j++)
{
for($k = $x;$k < $x + WORD_WIDTH;$k++)
{
$data[$i] .= $a[$j][$k];
}
}
}
return $data;
}
/**
* 把遍历得到的字符串与给定的标准字符进行匹配,找到相似度最高的确定其数字
*/
function getResult($data)
{
global $key;
$result = "";
foreach($data as $numk => $str)
{
$index = 0;
$best = 0;
foreach($key as $k => $v)
{
$n = 0;
$n = similar_text($v,$str,$percent);
if($n > $best)
{
$best = $n;
$index = $k;
if($n / strlen($v) > 0.95)
break;
}
}
$result .= $index;
}
return $result;
}
//$path = "http://www.gdgajj.com/cx/servlet/ImageServlet";
$path = 'c.jpg';
$pixels = ImageBinary($path);
$data = getNumDate($pixels);
$r = getResult($data);
echo $r;
?>
<img src = <?=$path?> /></pre>
哪位大哥帮忙翻译成AU3代码,谢谢! |