回复 13# 卫和谐
源代码中有一段这样的代码,你看下
Func LoadPicClick() ;加载图片
chushihua()
$message2 = "选择识别码."
$Open2 = FileOpenDialog($message2, "", "验证码 (*.gif;*.jpg;*.bmp)", 1 )
If @error Then
_GUICtrlListBox_AddString($LogList,"加载图片文件失败,原因:没有选择文件!")
Else
$Open2 = StringSplit($Open2, "|", @CRLF)
If $Open2[0] = 1 Then GUICtrlSetImage ($MainPic,$Open2[1])
_GUICtrlListBox_AddString($LogList,"加载图片文件:"&$Open2[1])
;以数组中的坐标信息为拆分单元,分割图片,并将图片显示到子图片框.
myReadImageToArray(0,$Open2[1],$ResPicPos[0][0],$ResPicPos[0][1],$ResPicPos[0][2],$ResPicPos[0][3],False) ;拆分
;myReadImageToArray($Num,$ImageFile,$ix0,$iy0,$iX,$iY,$b_Array2d=False)
GUICtrlSetImage($FirstPic,@ScriptDir&"\0.bmp")
myReadImageToArray(1,$Open2[1],$ResPicPos[1][0],$ResPicPos[1][1],$ResPicPos[1][2],$ResPicPos[1][3],False)
GUICtrlSetImage($SecondPic,@ScriptDir&"\1.bmp")
myReadImageToArray(2,$Open2[1],$ResPicPos[2][0],$ResPicPos[2][1],$ResPicPos[2][2],$ResPicPos[2][3],False)
GUICtrlSetImage($ThirdPic,@ScriptDir&"\2.bmp")
myReadImageToArray(3,$Open2[1],$ResPicPos[3][0],$ResPicPos[3][1],$ResPicPos[3][2],$ResPicPos[3][3],False)
GUICtrlSetImage($FourthPic,@ScriptDir&"\3.bmp")
EndIf
EndFunc
里面有4个关于
myReadImageToArray($Num,$ImageFile,$ix0,$iy0,$iX,$iY,$b_Array2d=False)
这个函数的调用,查看这个函数的代码如下:
Func myReadImageToArray($Num,$ImageFile,$ix0,$iy0,$iX,$iY,$b_Array2d=False);成功则返回数组.失败返回0.$b_Array2d=True返回二维数组,=False返回一维数组
Local $hBitmap, $BitmapData, $i_width, $i_height, $Scan0, $pixelData, $s_BMPData, $i_Stride
Local $hClone, $hImage
_GDIPlus_Startup()
$hBitmap = _GDIPlus_BitmapCreateFromFile($ImageFile)
$i_width = _GDIPlus_ImageGetWidth($hBitmap)
$i_height = _GDIPlus_ImageGetHeight($hBitmap)
$Row=$i_width
$Cro=$i_height
$hClone = _GDIPlus_BitmapCloneArea($hBitmap, $ix0, $iy0, $iX, $iY)
_GDIPlus_ImageSaveToFile($hClone, @ScriptDir&"\"&$Num &".bmp")
If @error Then
MsgBox(0,"","wrong")
_GDIPlus_ShutDown ()
Return 0;无效的图形文件或未找到该文件
EndIf
$BitmapData = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $i_width, $i_height, $GDIP_ILMREAD, $GDIP_PXF24RGB)
$i_Stride = DllStructGetData($BitmapData, "Stride");Stride - Offset, in bytes, between consecutive scan lines of the bitmap. If the stride is positive, the bitmap is top-down. If the stride is negative, the bitmap is bottom-up.
$Scan0 = DllStructGetData($BitmapData, "Scan0");Scan0 - Pointer to the first (index 0) scan line of the bitmap.
$pixelData = DllStructCreate("ubyte lData[" & (Abs($i_Stride) * $i_height) & "]", $Scan0)
$s_BMPData = DllStructGetData($pixelData, "lData")
$s_BMPData = StringTrimLeft($s_BMPData,2);去掉头部"0x"
_GDIPlus_BitmapUnlockBits($hBitmap, $BitmapData)
_GDIPlus_ImageDispose($hBitmap)
_GDIPlus_ImageDispose($hClone)
_WinAPI_DeleteObject ($hBitmap)
_GDIPlus_Shutdown()
If $b_Array2d Then;要求返回二维数组
Local $a_return[$i_width][$i_height], $x, $y, $s
For $y=0 To $i_height-1
$s=StringMid($s_BMPData, $y*($i_Stride*2)+1, $i_width*6)
;不要使用一些例子中提出的$s=StringMid($s_BMPData, $y*($i_width*6)+1, $i_width*6),经实际测试,该方式无法正确处理:
;GIF冗余,带隐藏内容的图形文件(一种数据加密方式),或者多页图形文件,当然这些情形比较少见,下同
For $x= 0 To $i_width-1
$a_return[$x][$y]= Number("0x"&StringMid($s,$x*6+1 ,6))
Next
Next
Else;要求返回一维数组
Local $a_return[$i_height], $y
For $y=0 To $i_height-1
$a_return[$y]=StringMid($s_BMPData, $y*($i_Stride*2)+1, $i_width*6)
;见上说明
Next
EndIf
Return $a_return
EndFunc ;==>myReadImageToArray
其实,这个函数就是根据阿福写的那个图片到数组的函数改的,关键是
$hClone = _GDIPlus_BitmapCloneArea($hBitmap, $ix0, $iy0, $iX, $iY)
_GDIPlus_ImageSaveToFile($hClone, @ScriptDir&"\"&$Num &".bmp")
这两句,将图片中由$ix0, $iy0, $iX, $iY控制的区域复制出来,然后存成$Num .bmp文件,然后才处理。
我这样讲解下,你能明白如何只处理3个字符识别你的图片了吗? |