Advertisement
calfred2808

TUTORIAL DECAPTCHA WITH AFORGE.NET WINFORM

Sep 6th, 2018
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.92 KB | None | 0 0
  1. 'https://laptrinhvb.net/bai-viet/chuyen-de-csharp/---Csharp----Huong-dan-DeCaptcha-su-dung-thu-vien-Aforge-NET/868d43071f4f2649.html
  2.  
  3. Imports AForge.Imaging.Filters
  4. Imports System
  5. Imports System.Collections.Generic
  6. Imports System.ComponentModel
  7. Imports System.Data
  8. Imports System.Drawing
  9. Imports System.Linq
  10. Imports System.Text
  11. Imports System.Threading.Tasks
  12. Imports System.Windows.Forms
  13. Imports Tesseract
  14.  
  15. Namespace DeCaptcha
  16.     Public Partial Class Form1
  17.         Inherits Form
  18.  
  19.         Public Sub New()
  20.             InitializeComponent()
  21.         End Sub
  22.  
  23.         Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
  24.         End Sub
  25.  
  26.         Private Function reconhecerCaptcha(ByVal img As Image) As String
  27.             Dim imagem As Bitmap = New Bitmap(img)
  28.             imagem = imagem.Clone(New Rectangle(0, 0, img.Width, img.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb)
  29.             Dim erosion As Erosion = New Erosion()
  30.             Dim dilatation As Dilatation = New Dilatation()
  31.             Dim inverter As Invert = New Invert()
  32.             Dim cor As ColorFiltering = New ColorFiltering()
  33.             cor.Blue = New AForge.IntRange(200, 255)
  34.             cor.Red = New AForge.IntRange(200, 255)
  35.             cor.Green = New AForge.IntRange(200, 255)
  36.             Dim open As Opening = New Opening()
  37.             Dim bc As BlobsFiltering = New BlobsFiltering()
  38.             Dim close As Closing = New Closing()
  39.             Dim gs As GaussianSharpen = New GaussianSharpen()
  40.             Dim cc As ContrastCorrection = New ContrastCorrection()
  41.             bc.MinHeight = 10
  42.             Dim seq As FiltersSequence = New FiltersSequence(gs, inverter, open, inverter, bc, inverter, open, cc, cor, bc, inverter)
  43.             pic_decaptcha.Image = seq.Apply(imagem)
  44.             Dim reconhecido As String = OCR(CType(pic_decaptcha.Image, Bitmap))
  45.             Return reconhecido
  46.         End Function
  47.  
  48.         Private Function OCR(ByVal b As Bitmap) As String
  49.             Dim res As String = ""
  50.  
  51.             Using engine = New TesseractEngine("tessdata", "eng", EngineMode.[Default])
  52.                 engine.SetVariable("tessedit_char_whitelist", "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  53.                 engine.SetVariable("tessedit_unrej_any_wd", True)
  54.  
  55.                 Using page = engine.Process(b, PageSegMode.SingleLine)
  56.                     res = page.GetText()
  57.                 End Using
  58.             End Using
  59.  
  60.             Return res
  61.         End Function
  62.  
  63.         Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
  64.             If openFileDialog.ShowDialog() = DialogResult.OK Then
  65.                 pic_orgin.LoadAsync(openFileDialog.FileName)
  66.             End If
  67.         End Sub
  68.  
  69.         Private Sub btn_decaptcha_Click(ByVal sender As Object, ByVal e As EventArgs)
  70.             resultLabel.Text = reconhecerCaptcha(pic_orgin.Image)
  71.         End Sub
  72.     End Class
  73. End Namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement