移动梦网2005版图形码识别vb.net2003源代码
因为涉及到商业软件,所以要等过期以后才发布出来。
2005年4月初,识别入门的时候写的,代码不好,凑合着看吧。
要点:
1.去背景:这个图形码的背景色和文字色是有规律的,所以很好去背静
2.分割随即位置的字符,方法:http://blog.csdn.net/Qqwwee_Com/archive/2006/04/14/662505.aspx
Imports System.Net
Imports System.Drawing
Public Class CrMonterNetImgClass CrMonterNetImg
Private ImgUrl As String
Private Cookie As System.Net.CookieCollection
Sub New()Sub New(ByVal StrUrl As String, ByVal Cookies As System.Net.CookieCollection)
ImgUrl = StrUrl
Cookie = Cookies
End Sub
识别:返回数字!#Region "识别:返回数字!"
Public Function proc()Function proc() As String
Dim Img As Bitmap
Dim cc(1) As Color
Dim Va As Array
Dim IntRang As Integer '检查图象分段
Dim IntS(4) As Integer '左边
Dim IntE(4) As Integer '右边
Dim T(4) As Integer '上面
Dim B(4) As Integer '下面
Dim StrCode As String = ""
Try
While Not IntRang = 4
Img = crImg() '抓取图片
cc = GetDeepColor(Img) '得到深浅色
FormatImg(Img, cc) '去背景色+单色显示
Va = GetV(Img)
IntRang = chkImg(Va, IntS, IntE)
End While
getTB(Img, IntS, IntE, T, B)
StrCode = crImg(Img, IntS, IntE, T, B)
Img.Dispose()
Catch ex As Exception
Finally
If Not Img Is Nothing Then
Img.Dispose()
End If
End Try
Return StrCode
End Function
#End Region
加载图片数据#Region "加载图片数据"
Private Function crImg()Function crImg() As Bitmap
Dim 请求 As Net.HttpWebRequest
Dim 响应 As Net.HttpWebResponse
Dim img As System.Drawing.Bitmap
Try
请求 = Net.HttpWebRequest.Create(ImgUrl)
请求.CookieContainer = New CookieContainer
If Not Cookie Is Nothing Then
请求.CookieContainer.Add(Cookie)
End If
响应 = 请求.GetResponse
img = New Bitmap(响应.GetResponseStream)
'img.Save("c:o.bmp")
Return img
Catch ex As Exception
Return Nothing
Finally
If Not 响应 Is Nothing Then
响应.Close()
End If
End Try
End Function
#End Region
得到深色、浅色--深色的RGB刚好相差30#Region "得到深色、浅色--深色的RGB刚好相差30"
Private Function GetDeepColor()Function GetDeepColor(ByVal Img As Bitmap) As Color()
Dim cc(2) As Color
Dim c, c1, ctemp As Color
'深色 浅色 当前色
If Img Is Nothing Then Return Nothing
For w As Integer = 0 To Img.Width - 1
For h As Integer = 4 To 6
'这3行应该可以找出来这2个颜色
ctemp = Img.GetPixel(w, h) ' 当前的颜色
If ctemp.R < 255 Then '有颜色的时候才在处理
If c.R > 0 Then
'用找到的颜色和当前颜色比较
If c.R < ctemp.R Then
'如果找到的颜色比当前颜色要小
'那么 c就是深色
'c1就是浅色
c1 = ctemp
End If
If c.R > ctemp.R Then
c1 = c
c = ctemp
End If
Else
c = ctemp
End If
If c1.ToArgb > 0 Then
Exit For
End If
End If
Next
Next
cc(0) = c
cc(1) = c1
Return cc
End Function
#End Region
去背景,单色显示#Region "去背景,单色显示"
Private Function FormatImg()Function FormatImg(ByVal Img As Bitmap, ByVal Cc As Color())
Dim ctemp As Color
For w As Integer = 0 To Img.Width - 1
For h As Integer = 0 To Img.Height - 1
ctemp = Img.GetPixel(w, h)
If ctemp.ToArgb = Cc(1).ToArgb Then
Img.SetPixel(w, h, Color.White)
End If
If ctemp.ToArgb = Cc(0).ToArgb Then
Img.SetPixel(w, h, Color.Black)
End If
Next
Next
End Function
#End Region
得到垂直投影#Region "得到垂直投影"
Private Function GetV()Function GetV(ByVal img As Bitmap) As Array
Dim ctemp As Color
Dim S As String = ""
For w As Integer = 0 To img.Width - 1
For h As Integer = 0 To img.Height - 1
ctemp = img.GetPixel(w, h)
If ctemp.ToArgb = Color.Black.ToArgb Then
S += "," & w
Exit For
End If
Next
Next
If Len(S) > 0 Then
S = S.Substring(1)
End If
Return S.Split(",")
End Function
#End Region
检测当前的图片是否符合社识别条件#Region "检测当前的图片是否符合社识别条件"
Private Function chkImg()Function chkImg(ByVal Va As Array, ByRef intS() As Integer, ByRef intE() As Integer) As Integer
Dim Strs() As String = Va
Dim intCur As Integer '当前数值
Dim IntPrew As Integer
Dim IntRang As Integer '一共找到多少段
intS = New Integer() {-1, -1, -1, -1, -1, -1, -1} '保存段的开始数字,多申请几个,避免出问题
intE = New Integer() {-1, -1, -1, -1, -1, -1, -1} '保存段的结束数字,多申请几个,避免出问题
For i As Integer = 0 To Strs.Length - 2
intCur = Strs(i)
IntPrew = Strs(i + 1)
If IntPrew - intCur = 1 Then
If intS(IntRang) = -1 Then
intS(IntRang) = intCur
End If
If i = Strs.Length - 2 Then
'如果到了倒数第二数字,最后一个数字就是结束了
intE(IntRang) = IntPrew
End If
Else
intE(IntRang) = intCur '设置当前段的结束
IntRang += 1 '连续的数字段计数器+1
End If
Next
Return IntRang + 1
End Function
#End Region
查找最上和最下的点#Region "查找最上和最下的点"
Private Function getTB()Function getTB(ByVal Img As Bitmap, ByVal IntS() As Integer, ByVal IntE() As Integer, ByRef T() As Integer, ByRef B As Integer())
For i As Integer = 0 To 3
For ww As Integer = IntS(i) To IntE(i)
For hh As Integer = 0 To 19
If Img.GetPixel(ww, hh).ToArgb = Color.Black.ToArgb Then
If T(i) = 0 Then
T(i) = hh
End If
If hh < T(i) Then
T(i) = hh
End If
If hh > B(i) Then
B(i) = hh
End If
End If
Next
Next
Next
End Function
#End Region
识别#Region "识别"
Private Function CrImg()Function CrImg(ByVal Img As Bitmap, ByVal IntS() As Integer, ByVal Inte() As Integer, ByVal T() As Integer, ByVal B() As Integer) As String
Dim s As String = ""
For i As Integer = 0 To 3
Dim r As New Rectangle(IntS(i), T(i), Inte(i) - IntS(i) + 1, B(i) - T(i) + 1)
Dim btemp As Bitmap = Img.Clone(r, Img.PixelFormat)
s += CrImg(btemp)
btemp.Dispose()
Next
Return s
End Function
Private Function CrImg()Function CrImg(ByVal Img As Bitmap) As String
If Img Is Nothing Then Return " "
If Img.Width = 6 And Img.Height = 9 Then
Return "1"
End If
If Img.Width = 9 And Img.Height = 12 Then
Return "4"
End If
If Img.Width = 8 And Img.Height = 9 Then
If Img.GetPixel(0, 1).ToArgb = Color.Black.ToArgb Then
Return "2"
Else
Return "0"
End If
End If
If Img.Width = 8 And Img.Height = 11 Then
If Img.GetPixel(2, 0).ToArgb = Color.Black.ToArgb Then
Return "8"
Else
Return "6"
End If
End If
If Img.Width = 8 And Img.Height = 12 Then
If Img.GetPixel(5, 2).ToArgb = Color.Black.ToArgb Then
Return "3"
End If
If Img.GetPixel(2, 4).ToArgb = Color.Black.ToArgb Then
Return "5"
End If
If Img.GetPixel(0, 0).ToArgb = Color.Black.ToArgb Then
Return "7"
End If
If Img.GetPixel(0, 5).ToArgb = Color.Black.ToArgb Then
Return "9"
End If
End If
Return " "
End Function
#End Region
End Class
阅读(812) | 评论(0) | 转发(0) |