package com.pollex.pam.service.util;
|
|
import java.awt.Color;
|
import java.awt.Font;
|
import java.awt.Graphics;
|
import java.awt.image.BufferedImage;
|
import java.util.Random;
|
|
public final class VerifyCodeUtil {
|
|
|
public static String createcode() {
|
String code = "";
|
code = "";
|
String randomRange = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";// Randomly generated character
|
// range (0-9, a-z, A-Z)
|
|
// The number of digits for generating the verification code (here 4 digits)
|
for (int i = 0; i < 4; i++) {
|
int index = (int) (Math.random() * 62);// Will produce a [0,62) number, excluding decimals
|
char randomCode = randomRange.charAt(index);
|
code = code + randomCode;
|
}
|
return code;
|
}
|
|
// 3. Generate pictures
|
public static BufferedImage createimage(String code) {
|
// The first 2 parameters are: width, height. The back is the image type
|
// Create a BufferedImage object without transparent color, TYPE_INT_ARGB is
|
// with transparent color
|
BufferedImage bi = new BufferedImage(130, 50, BufferedImage.TYPE_INT_RGB);
|
|
// 1. Get a canvas
|
Graphics g = bi.getGraphics();
|
// 2. Add background color
|
g.setColor(Color.WHITE);
|
g.fillRect(0, 0, 130, 50);
|
|
// 3. Add interference lines
|
for (int i = 0; i < 10; i++) {
|
Random r = new Random();
|
int red = r.nextInt(256);
|
int green = r.nextInt(256);
|
int blue = r.nextInt(256);
|
Color c = new Color(red, green, blue);
|
g.setColor(c);
|
int x1 = r.nextInt(131);
|
int y1 = r.nextInt(51);
|
int x2 = r.nextInt(131);
|
int y2 = r.nextInt(51);
|
g.drawLine(x1, y1, x2, y2);// Draw a line
|
// g.drawOval(x1, y1, x2, y2);//Draw a curve
|
}
|
|
// 3. Add text
|
g.setColor(Color.BLACK);
|
g.setFont(new Font(" ", Font.BOLD, 40));
|
|
// 4. Fill the text into the artboard
|
g.drawString(code, 15, 40);
|
|
// 5. Close the canvas
|
g.dispose();
|
return bi;
|
}
|
|
}
|