Home >Web Front-end >H5 Tutorial >Use html5 canvas to crack simple verification code and getImageData interface application_html5 tutorial skills
Our school’s academic management system (it seems to be used by more than just our school), the server crashes with no explanation when it comes to course selection. Sometimes, in order to select a course, I have to repeatedly enter the verification code. When I think of thousands of college students wasting their time on After entering the verification code, I felt that I had an obligation to save mankind.
After searching, I saw this article, which was written 3 years ago. I referred to the first half and used the TamperMonkey plug-in to roughly achieve the desired effect. You can get this script at Userscript, it is also available on GitHub. The code is ugly, please debug it, please give me some advice.
Let’s talk about the idea: The canvas in HTML 5 has an interface getImageData that can be used to obtain pixel data from the verification code image. Each pixel has four values corresponding to r, g, b, and a. r, g, and b are the three colors of red, green, and blue, and a is the transparency.
I observed that the verification code of the educational management system is 5 digits, and the font size remains the same. Although there is interference in the background, it is obviously very different from the font color, so I used a very large one. Rough method: We know that the lighter the color, the greater the RGB value, and the darker the color, the smaller the RGB value. So I judged each pixel. If the sum of RGB is less than 350 (this value is measured), it is a pixel belonging to the font. For the convenience of observation, I set its RGB value to 255, otherwise it is set to 0. In this way, a picture with white text on a black background is obtained.