?? verifycode.java
字號:
package com.mno5.bbs.util.system.verify;
/**********************************************************************
* myniko tools Release 1.0
* ?Copyright myniko.com 2003
************************************************************************/
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
/**
* <p>
* Title: <font color=red>VerifyCode</font>
* Description:
* Copyright: Copyright (c) 2002
* Company: www.myniko.com
* @author <font color=red>niko </font>
* @version 1.0
*/
public class VerifyCode
{
public static final int NUMBER_ONLY = 0;
public static final int LETTER_ONLY = 1;
public static final int NUMBER_AND_LETTER = 2;
public static final int MICROSOFT_SERIAL_NUMBER = 3;
private static final char NUMBER[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
};
private static final char LETTER[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z'
};
private static final char NUMBER_LETTER[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z'
};
private static final char MICROSOFT_SN[] = {
'2', '3', '4', '6', '7', '8', '9', 'B', 'C', 'D',
'F', 'G', 'H', 'J', 'K', 'M', 'P', 'Q', 'R', 'T',
'V', 'W', 'X', 'Y'
};
private static final char CODE_ELEMENTS[][] = {
NUMBER, LETTER, NUMBER_LETTER, MICROSOFT_SN
};
private String code;
private Color fColor;
private Color bColor;
private int style;
private int length;
public VerifyCode()
{
code = "";
fColor = Color.RED;
bColor = Color.WHITE;
style = 3;
length = 4;
}
public BufferedImage getImage()
{
int top = 1;
int left = 4;
BufferedImage image = new BufferedImage(1, 1, 1);
reset();
StringBuffer sb = new StringBuffer(code.length() * 2);
for(int i = 0; i < code.length(); i++)
{
sb.append(code.charAt(i));
sb.append(' ');
}
String show = sb.substring(0, sb.length() - 1);
int showHeight = image.getGraphics().getFontMetrics().getHeight();
int showWidth = image.getGraphics().getFontMetrics().stringWidth(show);
int imageHeight = showHeight + 2 * top;
int imageWidth = showWidth + 2 * left;
image = new BufferedImage(imageWidth, imageHeight, 1);
Graphics2D g = image.createGraphics();
g.setColor(bColor);
g.fillRect(0, 0, imageWidth, imageHeight);
g.setColor(fColor);
g.drawString(show, left, (g.getFontMetrics().getHeight() - g.getFontMetrics().getDescent() - g.getFontMetrics().getLeading()) + top);
g.drawRect(0, 0, imageWidth - 1, imageHeight - 1);
g.setColor(bColor);
g.drawLine(1, imageHeight / 2, imageWidth - 2, imageHeight / 2);
return image;
}
public boolean isVerify(String string)
{
return string.toUpperCase().matches(code);
}
public void setBColor(Color color)
{
bColor = color;
}
public void setBColor(String color)
{
bColor = decodeColor(color);
}
public void setCode(String string)
{
code = string;
}
public void setFColor(Color color)
{
fColor = color;
}
public void setFColor(String color)
{
fColor = decodeColor(color);
}
public void setLength(int i)
{
length = i;
}
public void setStyle(int i)
{
style = i;
}
private void reset()
{
reset(length);
}
private void reset(int len)
{
StringBuffer sb = new StringBuffer(len);
Random random = new Random(System.currentTimeMillis());
for(int i = 0; i++ < len;)
sb.append(CODE_ELEMENTS[style][random.nextInt(CODE_ELEMENTS[style].length)]);
code = sb.toString();
}
private static Color decodeColor(String color)
{
Color c = null;
if(!color.startsWith("0x"))
{
if(color.indexOf("|") != -1)
{
String temp[] = color.split("\\|");
int colorSet[] = new int[3];
int i;
for(i = 0; i < temp.length; i++)
colorSet[i] = Integer.valueOf(temp[i]).intValue();
if(i < 3)
for(; i < 3; i++)
colorSet[i] = 0;
c = new Color(colorSet[0], colorSet[1], colorSet[2]);
} else
{
c = Color.decode("0x" + color);
}
} else
{
c = Color.decode(color);
}
return c;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -