64,651
社区成员
发帖
与我相关
我的任务
分享
'接上帖
Private Function recognize() As String
t = 2 '上面3行空白
l = 1 '左边1列空白
w = 11 '每个数字宽11像素
h = 13 '每个数字高13像素
g = 11 '两个数字相距11像素
r = "xxxx"
On Error GoTo RERR
Picture1.Picture = LoadPicture(FPFN)
For n = 0 To 3
s = ""
For y = 0 To h - 1
For x = 0 To w - 1
'Debug.Print " "; Right("00000000" + Hex(Picture1.Point(n * w + x, t + y)), 8);
b = ColorDistance(Picture1.Point(l + n * g + x, t + y), &HE6AFBE)
'Debug.Print " "; Right(" " + CStr(b), 2); " ";
If b < 30 Then
Debug.Print "-";
s = s + "-"
Else
Debug.Print "O";
s = s + "O"
End If
Picture1.PSet (l + n * g + x, t + y), RGB(255, 0, 0)
Next
Debug.Print
Next
Debug.Print
For i = 0 To 9
a = 0
For j = 0 To h - 1
For k = 0 To w - 1
c1 = Mid(s, 1 + j * w + k, 1)
c2 = Mid(nm(i), 1 + j * w + k, 1)
If c1 = c2 Then
a = a + 1
End If
Next
Next
aa(i) = a
Debug.Print i, a
Next
maxi = -1
maxa = 0
For i = 0 To 9
If maxa < aa(i) Then
maxi = i
maxa = aa(i)
End If
Next
If maxa >= 100 Then
Mid(r, n + 1, 1) = CStr(maxi)
End If
Next
RERR:
recognize = r
Label1.Caption = r
Exit Function
End Function
Private Sub Timer1_Timer()
Dim f As Integer
On Error Resume Next
If LCase(Right(FPFN, 4)) = ".bmp" Then
f = FreeFile()
Open FPFN + ".txt" For Output As #f
Print #f, recognize()
Close #f
End
Else
logtofile "[" + FPFN + "] is not .bmp file!"
End If
End Sub
Public Function FileExists(filename) As Boolean
Dim msg As String
On Error GoTo CheckError
FileExists = (Dir(filename) <> "")
Exit Function
CheckError:
Const mnErrDiskNotReady = 71, mnErrDeviceUnavailable = 68
If (Err.Number = mnErrDiskNotReady) Then
msg = "将软盘插入驱动器 "
msg = msg + ",然后关好驱动器门。"
If MsgBox(msg, vbExclamation + vbOKCancel) = vbOK Then
Resume
Else
FileExists = False
Exit Function
End If
ElseIf Err.Number = mnErrDeviceUnavailable Then
msg = "找不到: "
msg = msg + filename
MsgBox msg, vbExclamation
FileExists = False
Exit Function
Else
FileExists = False
Exit Function
'msg = "出现 #" + str(Err.Number)
'msg = msg + " 意外错误: " + Err.Description
'MsgBox msg, vbCritical
'End'Form
End If
FileExists = False
Exit Function
End Function
Private Sub logtofile(s As String)
Dim f As Integer
On Error Resume Next
f = FreeFile()
Open App.Path + "\numeye2.log" For Append As #f
Print #f, Format(Now, "YYYY-MM-DD hh:mm:ss") + " " + s
Close #f
End Sub
Private Function Minimum(ParamArray Vals())
Dim n As Integer, MinVal
On Error Resume Next
MinVal = Vals(0)
For n = 1 To UBound(Vals)
If Vals(n) < MinVal Then MinVal = Vals(n)
Next n
Minimum = MinVal
End Function
Private Function Maximum(ParamArray Vals())
Dim n As Integer, MaxVal
On Error Resume Next
MaxVal = Vals(0)
For n = 1 To UBound(Vals)
If Vals(n) > MaxVal Then MaxVal = Vals(n)
Next n
Maximum = MaxVal
End Function
Private Sub c2hsb(ByVal clr As Long)
Dim MyR As Single, MyG As Single, MyB As Single
Dim Max As Single, Min As Single
Dim MyS As Single
Dim Delta As Single, MyVal As Single
Dim cc As String * 6
Dim r1, g1, b1 As Byte
On Error Resume Next
cc = Right("000000" + Hex$(clr), 6)
b1 = Val("&H" + Left(cc, 2))
g1 = Val("&H" + Mid(cc, 3, 2))
r1 = Val("&H" + Right(cc, 2))
MyR = r1 / 255: MyG = g1 / 255: MyB = b1 / 255
Max = Maximum(MyR, MyG, MyB)
Min = Minimum(MyR, MyG, MyB)
hsbB = Int(Max * 100)
If Max <> 0 Then
MyS = (Max - Min) / Max * 100
Else
MyS = 0
End If
hsbS = MyS
If hsbS = 0 Then
hsbH = 0
Else
Delta = Max - Min
Select Case Max
Case MyR
MyVal = (MyG - MyB) / Delta
Case MyG
MyVal = 2 + (MyB - MyR) / Delta
Case MyB
MyVal = 4 + (MyR - MyG) / Delta
End Select
MyVal = MyVal * 60
If MyVal < 0 Then MyVal = MyVal + 360
hsbH = MyVal
End If
' Debug.Print "hsb="; hsbH; " "; hsbS; " "; hsbB
End Sub
Private Function ColorDistance(ByVal c1 As Long, ByVal c2 As Long) As Long
Dim cd As Long
Dim h1, s1, b1, h2, s2, b2 As Single
On Error Resume Next
If c1 = -1 Or c2 = -1 Then
ColorDistance = 1000000
Exit Function
End If
c2hsb (c1)
h1 = hsbH / 360
s1 = hsbS
b1 = hsbB
c2hsb (c2)
h2 = hsbH / 360
s2 = hsbS
b2 = hsbB
cd = Abs(h1 - h2)
cd = cd + Abs(s1 - s2)
cd = cd + Abs(b1 - b2)
ColorDistance = cd
End Function
Option Explicit
Dim FPFN As String 'FullPathFileName
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim t As Integer
Dim l As Integer
Dim g As Integer
Dim n As Integer
Dim x As Integer
Dim y As Integer
Dim w As Integer
Dim h As Integer
Dim a As Integer
Dim aa(0 To 9) As Integer
Dim maxa As Integer
Dim maxi As Integer
Dim b As Long
Dim nm(9) As String
Dim s As String
Dim r As String
Dim c1 As String * 1
Dim c2 As String * 1
Dim hsbH, hsbS, hsbB As Single
Private Sub Form_Load()
FPFN = Command$
If Left(FPFN, 1) = Chr(34) Then FPFN = Mid(FPFN, 2)
If Right(FPFN, 1) = Chr(34) Then FPFN = Left(FPFN, Len(FPFN) - 1)
If Not FileExists(FPFN) Then
logtofile "[" + FPFN + "] file not find!"
End 'form
End If
nm(0) = _
"----OOO----" + _
"--OOOOOOO--" + _
"--OO---OO--" + _
"-OO-----OO-" + _
"-OO-----OO-" + _
"-OO-----OO-" + _
"-OO-----OO-" + _
"-OO-----OO-" + _
"-OO-----OO-" + _
"-OO-----OO-" + _
"--OO---OO--" + _
"--OOOOOOO--" + _
"----OOO----"
nm(1) = _
"----OOO----" + _
"--OOOOO----" + _
"--OOOOO----" + _
"-----OO----" + _
"-----OO----" + _
"-----OO----" + _
"-----OO----" + _
"-----OO----" + _
"-----OO----" + _
"-----OO----" + _
"-----OO----" + _
"--OOOOOOOO-" + _
"--OOOOOOOO-"
nm(2) = _
"--OOOOO----" + _
"-OOOOOOO---" + _
"-O-----OO--" + _
"-------OO--" + _
"-------OO--" + _
"------OO---" + _
"-----OO----" + _
"----OO-----" + _
"---OO------" + _
"--OO-------" + _
"-OO--------" + _
"-OOOOOOOO--" + _
"-OOOOOOOO--"
nm(3) = _
"--OOOOO----" + _
"-OOOOOOOO--" + _
"-O-----OO--" + _
"-------OO--" + _
"------OO---" + _
"--OOOO-----" + _
"--OOOOOO---" + _
"------OOO--" + _
"-------OO--" + _
"-------OO--" + _
"-O----OOO--" + _
"-OOOOOOO---" + _
"--OOOOO----"
nm(4) = _
"------OO---" + _
"-----OOO---" + _
"-----OOO---" + _
"----OOOO---" + _
"---OO-OO---" + _
"---OO-OO---" + _
"--OO--OO---" + _
"--OO--OO---" + _
"-OOOOOOOOO-" + _
"-OOOOOOOOO-" + _
"------OO---" + _
"------OO---" + _
"------OO---"
nm(5) = _
"-OOOOOOOO--" + _
"-OOOOOOOO--" + _
"-OO--------" + _
"-OO--------" + _
"-OO--------" + _
"-OOOOO-----" + _
"-OOOOOOO---" + _
"------OOO--" + _
"-------OO--" + _
"-------OO--" + _
"-O----OOO--" + _
"-OOOOOOO---" + _
"--OOOOO----"
nm(6) = _
"----OOOO---" + _
"---OOOOOO--" + _
"--OO----O--" + _
"--OO-------" + _
"-OO--------" + _
"-OO-OOOO---" + _
"-OOOOOOOO--" + _
"-OOO---OOO-" + _
"-OO-----OO-" + _
"-OO-----OO-" + _
"--OO---OOO-" + _
"--OOOOOOO--" + _
"----OOOO---"
nm(7) = _
"--OOOOOOOO-" + _
"--OOOOOOOO-" + _
"--------OO-" + _
"--------O--" + _
"-------OO--" + _
"------OO---" + _
"------O----" + _
"-----OO----" + _
"-----O-----" + _
"----OO-----" + _
"----OO-----" + _
"---OO------" + _
"---OO------"
nm(8) = _
"---OOOOO---" + _
"--OOOOOOO--" + _
"--OO---OO--" + _
"--OO---OO--" + _
"--OOO--O---" + _
"---OOOOO---" + _
"---OOOOO---" + _
"--OO--OOO--" + _
"-OO-----OO-" + _
"-OO-----OO-" + _
"-OOO---OOO-" + _
"--OOOOOOO--" + _
"---OOOOO---"
nm(9) = _
"---OOOO----" + _
"--OOOOOOO--" + _
"-OOO---OO--" + _
"-OO-----OO-" + _
"-OO-----OO-" + _
"-OOO---OOO-" + _
"--OOOOOOOO-" + _
"---OOOO-OO-" + _
"--------OO-" + _
"-------OO--" + _
"--O----OO--" + _
"--OOOOOO---" + _
"---OOOO----"
Timer1.Enabled = True
End Sub
'未完待续