Программа верная и все работает написана на VB6
Приложение распознает в стороннем приложении меняющиеся каждые 2-4 секунды одно-двузначные цыфры, на однотонном фоне, цыфры правильной формы, без искажений.
Значение текста на картинке в другом приложении меняется периодически, а наше приложение распознает эту картнку писваивает значение теста на картинке текстовому полю.
На фоме есть тестовые поля:
text1 - Значение окна заголовка распозноваемого окна
text2 - задает координату Х области распознования в распозноваемом окне
text3 - задает координату Y области распознования в распозноваемом окне
text4 - Показывает значение распозноваемого числа
И есть пикчер бокс
Picture1 - элемент картинки (в котором появляется число) из распозноваемого окна, размер 21x21
- Код: Выделить всё
Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private hWNDSrc As Long
Private hDCSrc As Long
Private Sub Timer1_Timer()
hWNDSrc = FindWindow(vbNullString, text1.Text & vbNullChar)
If hWNDSrc > 0 Then
hDCSrc = GetDC(hWNDSrc)
Picture1.Cls
BitBlt Picture1.hDC, 0, 0, 21, 21, hDCSrc, CLng(Text2.Text), CLng(Text3.Text), vbSrcCopy
ReleaseDC hWNDSrc, hDCSrc
OCR
Else
Picture1.Cls
Text4.Text = vbNullString
End If
End Sub
Private Sub OCR()
Dim X As Integer
Dim Y As Integer
Dim B1 As Boolean
Dim B2 As Boolean
Dim sCode As String
Dim Segment(5, 6) As Boolean
Dim iTop As Integer
Dim iBottom As Integer
Dim iLeft(6) As Integer
Dim iRight(6) As Integer
Dim iAmount As Integer
Dim iNumber As Integer
With Picture1
For Y = 1 To 20 'определение верха и низа строки
B1 = False
For X = 1 To 20
If .Point(X, Y) > 16448250 Then B1 = True: Exit For
Next X
If B1 Then
If B2 = False Then
B2 = True
iTop = Y
Picture1.Line (0, Y - 1)-(21, Y - 1), 9830178
End If
Else
If B2 Then
iBottom = Y - 1
Picture1.Line (0, Y)-(21, Y), 9830178
Exit For
End If
End If
Next Y
B2 = False 'определение начал и концов цифр
For X = 1 To 20
B1 = False
For Y = iTop To iBottom
If .Point(X, Y) > 16448250 Then B1 = True: Exit For
Next Y
If B1 Then
If B2 = False Then
B2 = True
iLeft(iNumber) = X
Picture1.Line (X - 1, 0)-(X - 1, 21), 9830178
End If
Else
If B2 Then
B2 = False
iRight(iNumber) = X - 1
Picture1.Line (X, 0)-(X, 21), 9830178
iNumber = iNumber + 1
End If
End If
Next X
iAmount = iNumber - 1
For iNumber = 0 To iAmount 'определение сегментов
X = (iRight(iNumber) - iLeft(iNumber)) \ 2 'половина ширины символа
Y = (iBottom - iTop) \ 3 'четверть высоты символа
Segment(iNumber, 0) = .Point(iLeft(iNumber) + X - 1, iTop) > 16448250 _
And .Point(iLeft(iNumber) + X + 1, iTop) > 16448250
' .PSet 2, iLeft(iNumber) + X - 1, iTop, 8421631
' .PSet 2, iLeft(iNumber) + X + 1, iTop, 8421631
Segment(iNumber, 1) = .Point(iRight(iNumber), iTop + Y) > 16448250
Segment(iNumber, 2) = .Point(iRight(iNumber), iBottom - Y) > 16448250
Segment(iNumber, 3) = .Point(iLeft(iNumber) + X, iBottom) > 16448250
Segment(iNumber, 4) = .Point(iLeft(iNumber), iBottom - Y) > 16448250
Segment(iNumber, 5) = .Point(iLeft(iNumber), iTop + Y) > 16448250
Segment(iNumber, 6) = .Point(iLeft(iNumber) + X, iTop + Y * 2 - 1) > 16448250 _
Or .Point(iLeft(iNumber) + X, iTop + Y * 2) > 16448250 _
Or .Point(iLeft(iNumber) + X, iTop + Y * 2 + 1) > 16448250
' .PSet 2, iLeft(iNumber) + X, iTop + Y * 2 - 1, 8421631
' .PSet 2, iLeft(iNumber) + X, iTop + Y * 2, 8421631
' .PSet 2, iLeft(iNumber) + X, iTop + Y * 2 + 1, 8421631
Next
End With
Text4.Text = vbNullString
For iNumber = 0 To iAmount 'дешифровка сегментов в цифры
'Text4.Text = "If Segment(iNumber, 0) = " & Segment(iNumber, 0) & " _" & vbCrLf & _
"And Segment(iNumber, 1) = " & Segment(iNumber, 1) & " _" & vbCrLf & _
"And Segment(iNumber, 2) = " & Segment(iNumber, 2) & " _" & vbCrLf & _
"And Segment(iNumber, 3) = " & Segment(iNumber, 3) & " _" & vbCrLf & _
"And Segment(iNumber, 4) = " & Segment(iNumber, 4) & " _" & vbCrLf & _
"And Segment(iNumber, 5) = " & Segment(iNumber, 5) & " _" & vbCrLf & _
"And Segment(iNumber, 6) = " & Segment(iNumber, 6)
If Segment(iNumber, 0) = True _
And Segment(iNumber, 1) = True _
And Segment(iNumber, 2) = True _
And Segment(iNumber, 3) = True _
And Segment(iNumber, 4) = True _
And Segment(iNumber, 5) = True _
And Segment(iNumber, 6) = False Then Text4.Text = Text4.Text & "0"
If Segment(iNumber, 0) = False _
And Segment(iNumber, 1) = False _
And Segment(iNumber, 2) = False _
And Segment(iNumber, 3) = True _
And Segment(iNumber, 4) = False _
And Segment(iNumber, 5) = False _
And Segment(iNumber, 6) = True Then Text4.Text = Text4.Text & "1"
If Segment(iNumber, 0) = True _
And Segment(iNumber, 1) = True _
And Segment(iNumber, 2) = False _
And Segment(iNumber, 3) = True _
And Segment(iNumber, 4) = False _
And Segment(iNumber, 5) = True _
And Segment(iNumber, 6) = True Then Text4.Text = Text4.Text & "2"
If Segment(iNumber, 0) = True _
And Segment(iNumber, 1) = True _
And Segment(iNumber, 2) = True _
And Segment(iNumber, 3) = True _
And Segment(iNumber, 4) = False _
And Segment(iNumber, 5) = False _
And Segment(iNumber, 6) = True Then Text4.Text = Text4.Text & "3"
If Segment(iNumber, 0) = False _
And Segment(iNumber, 1) = False _
And Segment(iNumber, 2) = False _
And Segment(iNumber, 3) = False _
And Segment(iNumber, 4) = False _
And Segment(iNumber, 5) = False _
And Segment(iNumber, 6) = True Then Text4.Text = Text4.Text & "4"
If Segment(iNumber, 0) = True _
And Segment(iNumber, 1) = False _
And Segment(iNumber, 2) = True _
And Segment(iNumber, 3) = True _
And Segment(iNumber, 4) = False _
And Segment(iNumber, 5) = False _
And Segment(iNumber, 6) = True Then Text4.Text = Text4.Text & "5"
If Segment(iNumber, 0) = True _
And Segment(iNumber, 1) = False _
And Segment(iNumber, 2) = True _
And Segment(iNumber, 3) = True _
And Segment(iNumber, 4) = True _
And Segment(iNumber, 5) = True _
And Segment(iNumber, 6) = True Then Text4.Text = Text4.Text & "6"
If Segment(iNumber, 0) = True _
And Segment(iNumber, 1) = False _
And Segment(iNumber, 2) = False _
And Segment(iNumber, 3) = True _
And Segment(iNumber, 4) = False _
And Segment(iNumber, 5) = False _
And Segment(iNumber, 6) = True Then Text4.Text = Text4.Text & "7"
If Segment(iNumber, 0) = True _
And Segment(iNumber, 1) = True _
And Segment(iNumber, 2) = True _
And Segment(iNumber, 3) = True _
And Segment(iNumber, 4) = True _
And Segment(iNumber, 5) = True _
And Segment(iNumber, 6) = True Then Text4.Text = Text4.Text & "8"
If Segment(iNumber, 0) = True _
And Segment(iNumber, 1) = True _
And Segment(iNumber, 2) = True _
And Segment(iNumber, 3) = True _
And Segment(iNumber, 4) = False _
And Segment(iNumber, 5) = True _
And Segment(iNumber, 6) = True Then Text4.Text = Text4.Text & "9"
Next
End Sub