二维码
来源:互联网 发布:李涛疯狂淘宝电话 编辑:程序博客网 时间:2024/04/25 22:36
ValidateCode_3.java
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;import java.awt.Color;
import java.awt.Font;
import java.awt.BasicStroke;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;
public class ValidateCode {
public static void main(String[] args) {
getValidateCode("f:\\validateCode.jpg");
}
private static void getValidateCode(String path) {
int width = 100;
int height = 50;
BufferedImage image =
new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(new Color(
getRandom(0, 100),
getRandom(0, 100),
getRandom(0, 100)));
g.fillRect(0, 0, width, height);
int number = 4;
for(int i = 1; i <= number; i++) {
g.setColor(new Color(
getRandom(100, 255),
getRandom(100, 255),
getRandom(100, 255),
getRandom(100, 255)));
g.setFont(new Font(getFontName(), getStyle(), 40));
g.drawString(String.valueOf(getChar()),
width * i / (number + 3), (int) (height / 1.5));
}
BasicStroke stroke = new BasicStroke(getRandom(1, 5),
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
g.setStroke(stroke);
g.drawLine(getRandom(0, 10), height / getRandom(1, 10),
width, height / getRandom(1, 2));
g.drawLine(getRandom(0, 10), height / getRandom(3, 10),
width, height / getRandom(1, 3));
try {
FileOutputStream out = new FileOutputStream(path);
ImageIO.write(image, "jpg", out);
out.flush();
out.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
private static char getChar() {
Character[] chars = {'0', 'v', '2', 'x', '4', 'z', '6', 'r', '8', 't',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', '7', 's', '9',
'u', '1', 'w', '3', 'y', '5'};
List list = Arrays.asList(chars);
Collections.shuffle(list);
chars = (Character[])list.toArray();
return chars[getRandom(0, chars.length)];
}
private static String getFontName() {
String[] names =
{"SimSun", "Times New Roman", "Arial", "Courier New"};
return names[getRandom(0, names.length)];
}
private static int getStyle() {
int[] styles =
{Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD | Font.ITALIC};
return styles[getRandom(0, styles.length)];
}
private static int getRandom(int start, int end) {
int index = -1;
while(!(index >= start && index <= end)) {
index = (int) (Math.random() * end);
}
return index;
}
}
validateCode .java
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
public class validateCode {
public static void main(String[] args) throws FileNotFoundException {
File f = new File("E:/validateCode.jpg");
OutputStream os = new FileOutputStream(f);
new validateCode().getCertPic(0, 0, os);
}
//验证码图片中可以出现的字符集,可根据需要修改
private char mapTable[]={
'a','b','c','d','e','h',
'j','k','m','n','p','q',
'r','s','t','u','v','w',
'x','y','z','0','2','3',
'4','5','6','7','8','9'
};
public String getCertPic(int width, int height,OutputStream os) {
if(width<=0) {
width=60;
}
if(height<=0) {
height=20;
}
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
//获取图形上下文
Graphics g = image.getGraphics();
//设定背景颜色
g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, width, height);
//画边框
g.setColor(Color.black);
g.drawRect(0, 0, width-1, height-1);
//随机产生的验证码
String strEnsure="";
//4代表4位验证码,如果要生成等多位的验证码 ,则加大数值
for(int i=0; i<4; i++) {
strEnsure += mapTable[(int)(mapTable.length*Math.random())];
}
//将验证码显示在图像中,如果要生成更多位的验证码,增加drawString语句
g.setColor(Color.black);
g.setFont(new Font("Atlantic Inline", Font.PLAIN,18));
String str = strEnsure.substring(0, 1);
g.drawString(str, 8, 17);
str = strEnsure.substring(1, 2);
g.drawString(str, 20, 15);
str = strEnsure.substring(2, 3);
g.drawString(str, 35, 18);
str = strEnsure.substring(3, 4);
g.drawString(str, 45, 15);
//随机产生10个干扰点
Random random = new Random();
for(int i=0; i<10; i++ ) {
int x = random.nextInt(width);
int y = random.nextInt(height);
g.drawOval(x, y, 1, 1);
}
//释放图形上下文
g.dispose();
try{
//输出图像到页面
ImageIO.write(image, "JPEG", os);
}catch(IOException e) {
return "";
}
return strEnsure;
}
}
- 二维码
- 二维码
- 二维码
- 二维码
- 二维码
- 二维码
- 二维码
- 二维码
- 二维码
- 二维码
- 二维码
- 二维码
- 二维码
- 二维码
- 二维码
- 二维码
- 二维码
- 二维码
- Java语法总结 - 数组
- PreTranslateMessage作用和使用方法
- 在开始创业之前,最好认真问自己这六个问题
- 【web开发】spring mvc+hibernate项目避免乱码的步骤
- Android开发——自动连接指定SSID的wifi热点(不加密/加密)
- 二维码
- 打车软件跑马圈资本:1年冒出近30款 恐类团购
- 怎样将数据送入DSP
- 类型转换
- java string int
- UIWebView响应点击
- Android 异步获取网络图片并处理导致内存溢出问题解决方法
- SQLite相关备忘
- Virtual Key Codes