Small Basic中文站-快乐编程

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 7669|回复: 1

VB.Net验证码识别的源代码

  [复制链接]
发表于 2010-6-24 01:16:24 | 显示全部楼层 |阅读模式
本帖最后由 akyao 于 2010-6-24 01:17 编辑

vb.net写的验证码识别代码,代码质量不是太高,而且去噪部分算法很不好,但识别现在网站上的图形验证码已经够用了
本文由AkYao从apps.hi.baidu.com/share/detail/2188410转载. 代码如下:


  1. Imports System
  2. Imports System.Text
  3. Imports System.Data
  4. Imports System.Data.OleDb
  5. Imports System.Drawing
  6. Imports System.Drawing.Graphics

  7. Public Class CrackImage

  8. Private ConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=Learn.mdb"
  9. Private resultNumber As String
  10. Private rsultStudy As Boolean = False

  11. '识别
  12. Public Sub New(ByVal SrcImage As Bitmap)
  13. GetImageNumber = GetIamgeResultNumber(GetNewIamge(SrcImage))
  14. End Sub

  15. '学习
  16. Public Sub New(ByVal SrcImagePath As String, ByVal StudyNumber As String)
  17. rsultStudy = StudyCode(SrcImagePath, StudyNumber)
  18. End Sub


  19. Public Property GetImageNumber() As String
  20. Get
  21. Return resultNumber
  22. End Get
  23. Set(ByVal Value As String)
  24. resultNumber = Value
  25. End Set
  26. End Property


  27. Public Property GetStudyImageResult() As Boolean
  28. Get
  29. Return rsultStudy
  30. End Get
  31. Set(ByVal Value As Boolean)
  32. rsultStudy = Value
  33. End Set
  34. End Property

  35. '处理新图片
  36. Private Function GetNewIamge(ByVal srcBitBmpImage As Bitmap) As Bitmap
  37. '建立临时表
  38. Dim myDataTable As New DataTable
  39. Dim myCol2 As New DataColumn
  40. myCol2.DataType = System.Type.GetType("System.Int32")
  41. myCol2.ColumnName = "RgbValue"
  42. myDataTable.Columns.Add(myCol2)

  43. Dim myCol3 As New DataColumn
  44. myCol3.DataType = System.Type.GetType("System.Int32")
  45. myCol3.ColumnName = "RgbCount"
  46. myDataTable.Columns.Add(myCol3)

  47. '载入图片
  48. Dim img As Bitmap = srcBitBmpImage
  49. Dim x, y As Integer

  50. '去除杂点
  51. '遍历所有点,存储每点的颜色代码,并对各种颜色进行统计 (这些代码可以不要,直接将图转化成黑白只剩下噪点和感染线条)
  52. For x = 0 To img.Width - 1
  53. For y = 0 To img.Height - 1
  54. Dim Found As Boolean = False
  55. If myDataTable.Rows.Count > 0 Then
  56. For k As Integer = 0 To myDataTable.Rows.Count - 1
  57. If myDataTable.Rows(k).Item("RgbValue") = img.GetPixel(x, y).ToArgb Then
  58. myDataTable.Rows(k).Item("RgbCount") += 1
  59. Found = True
  60. Exit For
  61. End If
  62. Next
  63. End If

  64. If Found = False Then
  65. Dim myRow As DataRow
  66. myRow = myDataTable.NewRow()
  67. myRow.Item("RgbValue") = img.GetPixel(x, y).ToArgb
  68. myRow.Item("RgbCount") = 1
  69. myDataTable.Rows.Add(myRow)
  70. End If
  71. Next
  72. Next

  73. '获取背景色码
  74. '象素点出现最多的就视为背景色
  75. Dim intMaxRgbValue As Integer = 0
  76. If myDataTable.Rows.Count > 0 Then
  77. myDataTable.DefaultView.Sort = "RgbCount DESC"
  78. intMaxRgbValue = myDataTable.DefaultView.Item(0).Item("RgbValue")
  79. End If

  80. '勾画数字轮廓
  81. For x = 0 To img.Width - 1
  82. For y = 0 To img.Height - 1
  83. Dim x1, y1 As Integer 'x1和y1记录的是相对当前象素的上一个象素的坐标
  84. If x = 0 Then
  85. x1 = x
  86. Else
  87. x1 = x - 1
  88. End If

  89. If y = 0 Then
  90. y1 = y
  91. Else
  92. y1 = y - 1
  93. End If

  94. Dim x2, y2 As Integer 'x2和y2记录的是相对当前象素下一个象素的坐标
  95. If x = img.Width - 1 Then
  96. x2 = img.Width - 1
  97. Else
  98. x2 = x + 1
  99. End If

  100. If y = img.Height - 1 Then
  101. y2 = img.Height - 1
  102. Else
  103. y2 = y + 1
  104. End If

  105. '都是普通的去噪手法,去噪成黑白色,这里是去噪成黑黄 色
  106. If img.GetPixel(x2, y).ToArgb = intMaxRgbValue Or img.GetPixel(x1, y).ToArgb = intMaxRgbValue Then
  107. img.SetPixel(x, y, Color.Black)
  108. ElseIf img.GetPixel(x, y).ToArgb <> intMaxRgbValue Then
  109. img.SetPixel(x, y, Color.Yellow)
  110. Else
  111. img.SetPixel(x, y, Color.Black)
  112. End If
  113. Next
  114. Next

  115. Return img
  116. End Function

  117. '获取处理后的数字
  118. Private Function GetIamgeResultNumber(ByVal srcNewImg As Bitmap) As String
  119. Dim RawData As New StringBuilder
  120. '设置分割大小
  121. Dim imgNewWidth As Integer = 16
  122. Dim imgNewHeight As Integer = 13

  123. Dim imgNew As New Bitmap(srcNewImg)
  124. Dim x, y As Integer
  125. Dim result As String = ""

  126. Dim ImageSplitWidth As Integer = imgNew.Width - imgNewWidth

  127. For m As Integer = 0 To ImageSplitWidth Step imgNewWidth
  128. Dim Rc As New Rectangle(m, 0, imgNewWidth, imgNewHeight)
  129. Dim B As Bitmap
  130. B = imgNew.Clone(Rc, imgNew.PixelFormat)
  131. '对比分割的颜色,黑色取1,其他取0,就此生成特征码
  132. For x = 0 To imgNewWidth - 1
  133. For y = 0 To imgNewHeight - 1
  134. If B.GetPixel(x, y).ToArgb <> Color.Black.ToArgb Then
  135. RawData.Append("1")
  136. Else
  137. RawData.Append("0")
  138. End If
  139. Next
  140. Next

  141. result += GetIamgeRawToNumber(RawData.ToString)
  142. RawData.Replace("0", "").Replace("1", "")
  143. Next

  144. Return result
  145. End Function

  146. '学习新图片
  147. Private Function StudyCode(ByVal srcImage As String, ByVal objNumber As String) As Boolean
  148. If objNumber = "" Or objNumber.Length < 4 Then Exit Function
  149. Dim RawData As New StringBuilder
  150. Dim imgNewWidth As Integer = 16
  151. Dim imgNewHeight As Integer = 13
  152. Dim p As Integer = 0
  153. Dim x, y As Integer

  154. Dim srcBitbmp As New Bitmap(srcImage)
  155. Dim imgNew As Bitmap = GetNewIamge(srcBitbmp)

  156. '分割图片并保存学习代码
  157. Dim ImageSplitWidth As Integer = imgNew.Width - imgNewWidth
  158. For m As Integer = 0 To ImageSplitWidth Step imgNewWidth
  159. Dim CurNumber As String
  160. CurNumber = objNumber.Substring(p, 1)
  161. If CurNumber <> "." Then
  162. Dim Rc As New Rectangle(m, 0, imgNewWidth, imgNewHeight)
  163. Dim B As Bitmap
  164. B = imgNew.Clone(Rc, imgNew.PixelFormat)

  165. For x = 0 To imgNewWidth - 1
  166. For y = 0 To imgNewHeight - 1
  167. If B.GetPixel(x, y).ToArgb <> Color.Black.ToArgb Then
  168. RawData.Append("1")
  169. Else
  170. RawData.Append("0")
  171. End If
  172. Next
  173. Next
  174. SaveImageRaw(objNumber.Substring(p, 1), RawData.ToString)
  175. RawData.Replace("0", "").Replace("1", "")
  176. End If
  177. p += 1
  178. Next

  179. Return True
  180. End Function

  181. '获取图片Raw数据
  182. Private Function GetIamgeRawToNumber(ByVal strRaw As String) As String
  183. Dim conn As New OleDbConnection(ConnStr)
  184. Dim comm As New OleDbCommand
  185. Dim reader As OleDbDataReader

  186. Dim myDataTable As New DataTable
  187. Dim myCol1 As New DataColumn
  188. myCol1.DataType = System.Type.GetType("System.Int32")
  189. myCol1.ColumnName = "MatchNumber"
  190. myDataTable.Columns.Add(myCol1)

  191. Dim myCol2 As New DataColumn
  192. myCol2.DataType = System.Type.GetType("System.Int32")
  193. myCol2.ColumnName = "MatchCount"
  194. myDataTable.Columns.Add(myCol2)

  195. comm.CommandText = "select LearnCharacter,Eigenvalue from tbLearn"
  196. conn.Open()
  197. comm.Connection = conn
  198. reader = comm.ExecuteReader

  199. If reader.HasRows Then
  200. While reader.Read
  201. Dim myRow As DataRow
  202. myRow = myDataTable.NewRow()
  203. myRow.Item("MatchNumber") = reader.Item(0)
  204. myRow.Item("MatchCount") = CompareRaw(reader.Item(1), strRaw)
  205. myDataTable.Rows.Add(myRow)
  206. End While
  207. End If
  208. conn.Close()

  209. Dim reuslt As String = ""
  210. If myDataTable.Rows.Count > 0 Then
  211. myDataTable.DefaultView.Sort = "MatchCount DESC"
  212. reuslt = myDataTable.DefaultView.Item(0).Item("MatchNumber")
  213. End If

  214. Return reuslt

  215. End Function

  216. '比较图片Raw数据
  217. Private Function CompareRaw(ByVal strDataBaseRaw As String, ByVal strObjRaw As String) As Integer
  218. Dim intRawLen As Integer = strDataBaseRaw.Length
  219. Dim MatchCount As Integer = 0
  220. For i As Integer = 0 To intRawLen - 1
  221. If strDataBaseRaw.Substring(i, 1) = strObjRaw.Substring(i, 1) Then
  222. MatchCount += 1
  223. End If
  224. Next
  225. Return MatchCount
  226. End Function

  227. '保存图片Raw数据
  228. Private Sub SaveImageRaw(ByVal strNumber As String, ByVal strRaw As String)
  229. Dim conn As New OleDbConnection(ConnStr)
  230. Dim comm As New OleDbCommand
  231. comm.CommandText = "insert into tbLearn(LearnCharacter,Eigenvalue) values ('" + strNumber + "','" + strRaw + "')"
  232. conn.Open()
  233. comm.Connection = conn
  234. comm.ExecuteNonQuery()
  235. conn.Close()
  236. End Sub

  237. End Class
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|Small Basic中文站-快乐编程 ( 闽ICP备09051788号 |

GMT+8, 2019-11-17 13:04 , Processed in 0.202263 second(s), 11 queries , File On.

Powered by Discuz! X3.3

快速回复 返回顶部 返回列表