| 网站首页 | VB.Net实例分析 | .Net技术文摘 | 下载中心 | VB.Net书籍笔记 | .Net源码 | VBS脚本与组件调用 | Windows2008技术文摘 | 给我们留言 | 
最新公告:

  没有公告

您现在的位置: 乐博网 >> VB.Net实例分析 >> 运算函数 >> 实例分析正文
最新推荐 更多内容
图形验证码识别引擎的源代码(VB.Net)
图形验证码识别引擎的源代码(VB.Net)
作者:点点滴滴    来源:163博客     更新时间:2010-6-29

图形验证码识别引擎的源代码(VB.Net)

源代码如下:

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

  • 上一篇:

  • 下一篇: 没有了
  • 【字体: 】【打印此文】【关闭窗口
      相关文章:(只显示最新16条)
    VB.Net图形验证码识别的源代码

    | 设为首页 | 加入收藏 | 联系站长 | | 友情链接 | 版权申明 |
    乐博网欢迎各种媒体转载我们的原创作品[转载请注明出处];我们鼓励更多VB.Net开发者一起加入研究与探讨;如发现文章访问错误、内容错误或版权疑问、内容有违相关法律(如涉及政治、色情、反动或散布虚假有害信息)等情况,请及时向我们举报,我们将及时纠正!
    联系邮箱:Shiny#vip.qq.com (#替换为@) QQ交流群: 40797788 [闽ICP备05014267号]