编程知识 cdmana.com

Picture captcha in java basic course

Image verification code  -- Pay Extension Education

Chapter one : The function of picture captcha

1. Types of captcha

Image verification code , Puzzle effect verification code , Digital calculation of picture captcha , Check the Chinese character verification code .

2. The function of verification code

Verification code as a means of human-computer identification , Its ultimate purpose is , It's to distinguish between normal people and machine operations .

The function of distinguishing human-computer behavior is self-evident . Registration of Internet behavior 、 Sign in 、 Post 、 Collect coupons 、 Voting and other application scenarios , It's all caused by machine brush

The risk of all kinds of losses , If we don't take precautions against all kinds of machine garbage , Irrigation content 、 Junk registration 、 Malicious login 、 Brush ticket 、 Storehouse 、 Activities

Disadvantages 、 Junk advertising 、 Reptiles 、 Once the user behavior such as wool party happens , Will develop the product itself 、 The user experience has a huge impact .

Chapter two : Picture captcha programming

1. Java Write picture captcha

 

package cn.tx.servlet;

import javax.imageio.ImageIO;

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.util.Random;

/**

* Pay Extension Education : Mr. Sakura

* Tencent classroom search : Pay Extension Education

* Tencent classroom address :https://ke.qq.com/course/149432

* idea Learn the address in this free video :https://ke.qq.com/course/363570

* Join in QQ Group get free materials and videos :QQ Group number  344379612

*/

public class Demo1 {

public static void main(String[] args) throws IOException {

int width = 150;

int height = 50;

BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

// Get the brush object

// Graphics g = image.getGraphics();

// Move down , Subclass

Graphics2D g = (Graphics2D) image.getGraphics();

// Set the color

g.setColor(Color.GRAY);

// Draw a rectangular

g.fillRect(0,0,width,height);

// Set the color

g.setColor(Color.yellow);

// Set font size

g.setFont(new Font(" Regular script ",Font.BOLD,25));

String words = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";

// Random

Random random = new Random();

int x = 30;

int y = 30;

// loop

for (int i = 0; i < 4; i++) {

int index = random.nextInt(words.length());

char c = words.charAt(index);

// The formula of radian  = angle  * PI /180;

// Get it first   Plus or minus  30 The angle of  ‐30 To  30 Between

int jiaodu = random.nextInt(60) 30;

// Calculate radians

double hudu = jiaodu * Math.PI / 180;

// Set radian

g.rotate(hudu,x,y);

g.drawString(c+"",x, y);

// Correct the radian

g.rotate(hudu,x,y);

x += 30;

}

// 2 Point to determine a straight line

int x1,x2,y1,y2;

// Drew interference

g.setColor(Color.green);

for (int i = 0; i < 4; i++) {

x1 = random.nextInt(width);

x2 = random.nextInt(width);

y1 = random.nextInt(height);

y2 = random.nextInt(height);

// Draw a straight line

g.drawLine(x1,y1,x2,y2);

 

2. Verification code generation tool class

In today's materials, we provide a tool class for generating captcha , It's convenient for you to use in the future .

}

// Output the picture to demo In the folder

ImageIO.write(image,"jpg",new File("C:\\demo\\aaa.jpg"));

}

}



Based on my many years of working experience and learning experience , Recorded a new set of Java Focus on video tutorials , If you are learning now Java, At the beginning of learning Java There is no systematic learning course in the process , You can add groups 654631948 Collect the learning materials , Interview questions , Development tools, etc , There are senior people in the group java The teacher answers questions , Every day, there will also be live lectures on the basic part and architecture , You can also add my wechat renlliang2013 In depth communication .

java Basic course :https://ke.qq.com/course/149432?tuin=57912c43


Java Distributed Internet Architecture / Microservices / High performance /springboot/springcloud:

https://ke.qq.com/course/179440?tuin=57912c43

版权声明
本文为[renlianggee]所创,转载请带上原文链接,感谢

Scroll to Top