Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3355529
  • 博文数量: 1450
  • 博客积分: 11163
  • 博客等级: 上将
  • 技术积分: 11101
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-25 14:40
文章分类

全部博文(1450)

文章存档

2017年(5)

2014年(2)

2013年(3)

2012年(35)

2011年(39)

2010年(88)

2009年(395)

2008年(382)

2007年(241)

2006年(246)

2005年(14)

分类: C/C++

2006-11-03 14:20:00

移动梦网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 CrMonterNetImg

    
Private ImgUrl As String
    
Private Cookie As System.Net.CookieCollection
    
Sub New(ByVal StrUrl As StringByVal Cookies As System.Net.CookieCollection)
        ImgUrl 
= StrUrl
        Cookie 
= Cookies
    
End Sub


#Region "识别:返回数字!"
    
Public Function proc() As String
        
Dim Img As Bitmap
        
Dim cc(1As Color
        
Dim Va As Array
        
Dim IntRang As Integer  '检查图象分段

        
Dim IntS(4As Integer '左边
        Dim IntE(4As Integer '右边
        Dim T(4As Integer '上面
        Dim B(4As 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() 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


#Region "得到深色、浅色--深色的RGB刚好相差30"

    
Private Function GetDeepColor(ByVal Img As Bitmap) As Color()
        
Dim cc(2As 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(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(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(ByVal Va As Array, ByRef intS() As IntegerByRef intE() As IntegerAs 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(ByVal Img As Bitmap, ByVal IntS() As IntegerByVal IntE() As IntegerByRef T() As IntegerByRef 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(ByVal Img As Bitmap, ByVal IntS() As IntegerByVal Inte() As IntegerByVal T() As IntegerByVal B() As IntegerAs 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(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(01).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(20).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(52).ToArgb = Color.Black.ToArgb Then
                
Return "3"
            
End If
            
If Img.GetPixel(24).ToArgb = Color.Black.ToArgb Then
                
Return "5"
            
End If
            
If Img.GetPixel(00).ToArgb = Color.Black.ToArgb Then
                
Return "7"
            
End If
            
If Img.GetPixel(05).ToArgb = Color.Black.ToArgb Then
                
Return "9"
            
End If
        
End If
        
Return " "
    
End Function

#End Region


End Class
阅读(776) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~