http://m1902.spaces.live.com/blog/cns!7D900B760C56FA84!174.entry
可惜了 是VB.Net 的
[CODE]Imports System.Drawing
Public Class yzm
Private Structure RChar '字符特征码
Dim MyChar As Char
Dim MyCharInfo(,) As Byte ' 0..63,0..63
End Structure
Private Structure RCharInfo '字符特征文件
Dim Sng As UInteger '文件类型标识
Dim Ver As Single '版本号
Dim charWidth As Byte, charheight As Byte '字符宽度和高度
Dim X0 As Byte ' 第一个字符开始x偏移
Dim TotalChars As Byte '图象字符总数
Dim allcharinfo() As RChar ' 0..9
End Structure
Private MyCharInfo As New RCharInfo
Public Function ShowAllCharInfo() As Bitmap
'根据学习后的字符特征码显示所有字符
Dim Result As New Bitmap((MyCharInfo.charWidth + 5) * 10 - 5, MyCharInfo.charheight)
Dim i As Integer, j As Integer, k As Integer
For i = 0 To (MyCharInfo.charWidth + 5) * 10 - 6
For j = 0 To MyCharInfo.charheight - 1
Result.SetPixel(i, j, Color.GreenYellow)
Next
Next
With Result
For k = 0 To 9
For i = 0 To MyCharInfo.charWidth - 1
For j = 0 To MyCharInfo.charheight - 1
If MyCharInfo.allcharinfo(k).MyCharInfo(i, j) > 0 Then
.SetPixel(i + k * MyCharInfo.charWidth + 5, j, Color.Black)
End If
Next
Next
Next
End With
ShowAllCharInfo = Result
End Function
Public Sub InteCharInfo(ByVal Charwidth As Short, ByVal X0 As Short, ByVal Length As UInteger, ByVal Height As UInteger)
'根据不同情况初始化特征码信息
With MyCharInfo
.Sng = 751115327
.Ver = 1.1
.TotalChars = Length
.charWidth = Charwidth
.charheight = Height
.X0 = X0
End With
Dim k As UInteger, I As UInteger, J As UInteger
For k = 0 To 9
For I = 0 To 49
For J = 0 To 49
MyCharInfo.allcharinfo(k).MyCharInfo(I, J) = 1
Next
Next
Next
End Sub
Public Sub GetCharInfoFromImage(ByVal MyCanvas As Bitmap, ByVal CharInfo As String)
'学习一个附加码的所有字符
Dim i As UInteger, x As UInteger
For i = 1 To MyCharInfo.TotalChars
x = MyCharInfo.X0 + MyCharInfo.charWidth * (i - 1)
ModifyInfo(MyCanvas, CharInfo(i - 1), x, MyCharInfo.charWidth, MyCharInfo.charheight)
Next
End Sub
Public Function GetStringFromImage(ByVal SBMP As Bitmap) As String
'从图片中识别字串
Dim k As Integer, m As Integer, x As Integer
Dim result As String = ""
For k = 0 To MyCharInfo.TotalChars - 1
x = MyCharInfo.X0 + MyCharInfo.charWidth * k
For m = 9 To 0 Step -1
If CMPBMP(SBMP, x, m) = 0 Then
result &= m.ToString
Exit For
End If
If m = 0 Then result &= "?"
Next
Next
GetStringFromImage = result
End Function
Private Function CMPBMP(ByVal sBMP As Bitmap, ByVal x0 As Integer, ByVal m As Integer) As Integer
Dim i As Integer, j As Integer
Dim result As Integer = 0
For i = 0 To MyCharInfo.charWidth - 1
For j = 0 To MyCharInfo.charheight - 1
If (GetColorValue(sBMP, x0 + i, j) > 0) And (MyCharInfo.allcharinfo(m).MyCharInfo(i, j) = 1) Then
result += 1
End If
Next
Next
CMPBMP = result
End Function
Public Sub New()
ReDim MyCharInfo.allcharinfo(9)
Dim I As UInteger
For I = 0 To 9
ReDim MyCharInfo.allcharinfo(I).MyCharInfo(49, 49)
Next
End Sub
Private Sub ModifyInfo(ByVal MyCanvas As Bitmap, ByVal MyChar As Char, ByVal X0 As Integer, ByVal CharWidth As Integer, ByVal CharHeight As Integer)
'修正指定字符特征码
Dim i As Integer, j As Integer
For i = 0 To CharWidth
For j = 0 To CharHeight
If GetColorValue(MyCanvas, X0 + i, j) > 0 Then
MyCharInfo.allcharinfo(Val(MyChar)).MyChar = MyChar
MyCharInfo.allcharinfo(Val(MyChar)).MyCharInfo(i, j) = 0
End If
Next
Next
End Sub
Private Function GetColorValue(ByVal Bitmap As Bitmap, ByVal x As Integer, ByVal y As Integer) As Integer
If (x >= Bitmap.Width) Or (x < 0) Or (y >= Bitmap.Height) Or (y < 0) Then Return 0
Dim r As Byte, g As Byte, b As Byte
r = Bitmap.GetPixel(x, y).R
g = Bitmap.GetPixel(x, y).G
b = Bitmap.GetPixel(x, y).B
GetColorValue = ((r * 256) + g) * 256 + b
Debug.Print(GetColorValue)
End Function
End Class[/CODE]