📄 logincheckcode.java
字号:
package other;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.security.SecureRandom;
import java.util.Random;
import javax.servlet.http.HttpSession;
public class LoginCheckCode
{
private String checkCode;
private LoginCheckCode()
{
checkCode = "";
nextCode();
}
public static LoginCheckCode getInstance(HttpSession session)
{
LoginCheckCode logincheckcode = null;
if(null != session)
{
Object obj = null;
synchronized(session)
{
obj = session.getAttribute("check");
}
if(null != obj && (obj instanceof LoginCheckCode))
{
logincheckcode = (LoginCheckCode)obj;
}
else
{
logincheckcode = new LoginCheckCode();
synchronized(session)
{
session.setAttribute("check", logincheckcode);
}
}
}
return logincheckcode;
}
public synchronized String nextCode()
{
String s = "";
SecureRandom securerandom = new SecureRandom();
for(int i = 0; i < 4; i ++)
{
s = (new StringBuilder()).append(s).append(String.valueOf(securerandom.nextInt(10))).toString();
}
checkCode = s;
return checkCode;
}
public synchronized BufferedImage getImage()
{
byte byte0 = 60;
byte byte1 = 20;
BufferedImage bufferedimage = new BufferedImage(byte0, byte1, 1);
Graphics g = bufferedimage.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, byte0, byte1);
g.setFont(new Font("Times New Roman", 0, 18));
g.setColor(getRandColor(160, 200));
for(int i = 0; i < 155; i ++)
{
int k = random.nextInt(byte0);
int l = random.nextInt(byte1);
int i1 = random.nextInt(12);
int j1 = random.nextInt(12);
g.drawLine(k, l, k + i1, l + j1);
}
for(int j = 0; j < checkCode.length(); j ++)
{
String s = checkCode.substring(j, j + 1);
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
g.drawString(s, 13 * j + 6, 16);
}
g.dispose();
return bufferedimage;
}
private Color getRandColor(int i, int j)
{
Random random = new Random();
if(i > 255)
{
i = 255;
}
if(j > 255)
{
j = 255;
}
int k = i + random.nextInt(j - i);
int l = i + random.nextInt(j - i);
int i1 = i + random.nextInt(j - i);
return new Color(k, l, i1);
}
public synchronized String getCheckCode()
{
return checkCode;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -