本文主要是在一张图片正找到目标图片,并返回对应的像素点坐标
首先要学会如何使用VB6.0生成直接调度的DLL文件,使用的工具为 VB6.0_DLL制作工具(Ver2.05)
具体方法在工具包中有说明文件,对应的DLL例程在 ZHYE-DLL.rar,调度例程在DLL_TEST.rar
运行文件夹中的DLL_TEST.exe, 主要实现的功能为在图片"1.bmp"中找到图片 "便捷任务.bmp",点击按钮 可以发现图片已找到
以下是找图代码:
Private Sub Command1_Click()
Dim W1 As Long, H1 As Long, W2 As Long, H2 As Long
Dim zPic() As Byte, fPic() As Byte
Dim Pic_XY As POINTAPI
Dim p1 As Picture, P1W, P1H, P1Handle
Set p1 = LoadPicture("1.bmp")
P1W = p1.Width
P1H = p1.Height
P1Handle = p1.Handle
Dim p2 As Picture, P2W, P2H, P2Handle
Set p2 = LoadPicture("便捷任务.bmp")
P2W = p2.Width
P2H = p2.Height
P2Handle = p2.Handle
'1 获得图片1数据
W1 = ScaleX(P1W, vbHimetric, vbPixels)
H1 = ScaleY(P1H, 8, 3)
With BI1.bmiHeader
.biSize = Len(BI.bmiHeader)
.biWidth = W1
.biHeight = -H1
.biBitCount = 32
.biPlanes = 1
End With
ReDim fPic(3, W1 - 1, H1 - 1)
Call GetDIBits(HDC, P1Handle, 0, H1, fPic(0, 0, 0), BI1, 0)
'1 获得图片2数据
W2 = ScaleX(P2W, vbHimetric, vbPixels)
H2 = ScaleY(P2H, 8, 3)
With BI.bmiHeader
.biSize = Len(BI.bmiHeader)
.biWidth = W2
.biHeight = -H2
.biBitCount = 32
.biPlanes = 1
End With
ReDim zPic(3, W2 - 1, H2 - 1)
Call GetDIBits(HDC, P2Handle, 0, H2, zPic(0, 0, 0), BI, 0)
Set p1 = Nothing
Set p2 = Nothing
Pic_XY = FindPic(fPic(), zPic(), W1, H1, W2, H2, 0.95)
Pic_X = Pic_XY.x
Pic_y = Pic_XY.y
Text1.Text = "图片已找到" + CStr(Pic_X) + " | " + CStr(Pic_y)
End Sub
以下是DLL文件中的函数代码
Public Function FindPic(SrPic() As Byte, DrPic() As Byte, SPic_W As Long, SPic_H As Long, DPic_W As Long, DPic_H As Long, PicSimilar As Single) As POINTAPI
Dim Pic_j As Long, Pic_i As Long
Dim Pic_Cnt As Long
Dim Pic_Cnt_Max As Long
Dim RGB_Similar As Single
Dim RGB_Similar_Rec As Single
For Pic_j = 0 To SPic_H - DPic_H - 1
For Pic_i = 0 To SPic_W - DPic_W - 1
If DPic_W > DPic_H Then
Pic_Cnt_Max = DPic_H
Else
Pic_Cnt_Max = DPic_W
End If
For Pic_Cnt = 0 To Pic_Cnt_Max - 2
RGB_Similar = RGB颜色比对(CInt(SrPic(2, Pic_i + Pic_Cnt, Pic_j + Pic_Cnt)), CInt(SrPic(1, Pic_i + Pic_Cnt, Pic_j + Pic_Cnt)), CInt(SrPic(0, Pic_i + Pic_Cnt, Pic_j + Pic_Cnt)), CInt(DrPic(2, Pic_Cnt, Pic_Cnt)), CInt(DrPic(1, Pic_Cnt, Pic_Cnt)), CInt(DrPic(0, Pic_Cnt, Pic_Cnt)))
RGB_Similar_Rec = RGB_Similar_Rec + RGB_Similar
If RGB_Similar_Rec / (Pic_Cnt + 1) > (1 - PicSimilar) Then
RGB_Similar = 0
RGB_Similar_Rec = 0
GoTo ExitLine:
End If
Next Pic_Cnt
FindPic.x = Pic_i
FindPic.y = Pic_j
ExitLine:
Next Pic_i
Next Pic_j
Pic_i = 0
Pic_j = 0
End Function
以上源码文件链接 https://download.csdn.net/download/zhye3690592/89123670