JAVA生成图片验证码
来源:互联网 发布:2009网络流行歌曲大全 编辑:程序博客网 时间:2024/06/17 22:43
原文地址:http://blog.csdn.net/ruixue0117/article/details/22829557 本屌稍作修改!再加一个基础功能的方法
一:基础功能方法
Img.java效果
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.OutputStream;
- import javax.imageio.ImageIO;
- public class Img {
- public static void outputCodeImg (int width,int height,String code,OutputStream os ) throws FileNotFoundException, IOException {
- BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR_PRE);
- //获取画笔
- Graphics gra = bi.getGraphics();
- //设置背景颜色
- gra.setColor(Color.BLACK);
- //填充背景颜色
- gra.fillRect(0, 0, width, height);
- //第二次 设置字体颜色
- gra.setColor(Color.red);
- //设置字体样式和大小
- gra.setFont(new Font(null, Font.HANGING_BASELINE,20));
- //描绘
- gra.drawString(code.toString(), 5, height-5);
- ImageIO.write(bi, "png", os);
- }
- }<strong>
- </strong>
二:升级版(转帖)
ImgCode.java
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.geom.AffineTransform;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.Arrays;
- import java.util.Random;
- import javax.imageio.ImageIO;
- public class ImgCode {
- /**
- * 生成验证码图片
- * @param width
- * @param height
- * @param codeLength
- * @param os
- * @throws IOException
- */
- public static void outputCodeImg(int width,int height,String code,OutputStream os) throws IOException{
- BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- //获取画笔
- Graphics2D g2 = image.createGraphics();
- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
- Color[] colors = new Color[5];
- Color[] colorSpaces = new Color[] { Color.WHITE, Color.CYAN,
- Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
- Color.PINK, Color.YELLOW };
- float[] fractions = new float[colors.length];
- Random rand = new Random();
- for(int i = 0; i < colors.length; i++){
- colors[i] = colorSpaces[rand.nextInt(colorSpaces.length)];
- fractions[i] = rand.nextFloat();
- }
- Arrays.sort(fractions);
- g2.setColor(Color.GRAY);// 设置边框色
- g2.fillRect(0, 0, width, height);
- Color c = getRandColor(200, 250);
- g2.setColor(c);// 设置背景色
- g2.fillRect(0, 2, width, height-4);
- //绘制干扰线
- Random random = new Random();
- g2.setColor(getRandColor(160, 200));// 设置线条的颜色
- for (int i = 0; i < 20; i++) {
- int x = random.nextInt(width - 1);
- int y = random.nextInt(height - 1);
- int xl = random.nextInt(6) + 1;
- int yl = random.nextInt(12) + 1;
- g2.drawLine(x, y, x + xl + 40, y + yl + 20);
- }
- // 添加噪点
- float yawpRate = 0.05f;// 噪声率
- int area = (int) (yawpRate * width * height);
- for (int i = 0; i < area; i++) {
- int x = random.nextInt(width);
- int y = random.nextInt(height);
- int rgb = getRandomIntColor();
- image.setRGB(x, y, rgb);
- }
- shear(g2, width, height, c);// 使图片扭曲
- g2.setColor(getRandColor(100, 160));
- int fontSize = height-4;
- Font font = new Font("Algerian", Font.ITALIC, fontSize);
- g2.setFont(font);
- char[] chars = code.toCharArray();
- int verifySize = code.length();
- for(int i = 0; i < verifySize; i++){
- AffineTransform affine = new AffineTransform();
- affine.setToRotation(Math.PI / 4 * rand.nextDouble() * (rand.nextBoolean() ? 1 : -1), (width / verifySize) * i + fontSize/2, height/2);
- g2.setTransform(affine);
- //如果上下 左右两边的验证码出现的位置不对,条件4 , 5参数
- g2.drawChars(chars, i, 1, ((width-10) / verifySize) * i + 5, height/2 + fontSize/2 -2 );
- }
- g2.dispose();
- ImageIO.write(image, "jpg", os);
- }
- /**
- * 返回验证码
- * @return
- */
- public static String getCode(int length){
- Random random = new Random();
- StringBuffer code = new StringBuffer();
- for(int i = 0; i < length; i++){
- int num = random.nextInt(10); //数字
- char upperCase = (char) (random.nextInt(26)+65); //大写字母
- String[] s= new String[2];
- s[0] = num + "";
- s[1] = upperCase+ "";
- code.append(s[random.nextInt(2)]);
- }
- return code.toString();
- }
- private static Color getRandColor(int fc, int bc) {
- Random random = new Random();
- if (fc > 255)
- fc = 255;
- if (bc > 255)
- bc = 255;
- int r = fc + random.nextInt(bc - fc);
- int g = fc + random.nextInt(bc - fc);
- int b = fc + random.nextInt(bc - fc);
- return new Color(r, g, b);
- }
- private static int getRandomIntColor() {
- Random random = new Random();
- int[] rgb = new int[3];
- for (int i = 0; i < 3; i++) {
- rgb[i] = random.nextInt(255);
- }
- int color = 0;
- for (int c : rgb) {
- color = color << 8;
- color = color | c;
- }
- return color;
- }
- private static void shear(Graphics g, int w1, int h1, Color color) {
- shearX(g, w1, h1, color);
- shearY(g, w1, h1, color);
- }
- private static void shearX(Graphics g, int w1, int h1, Color color) {
- Random random = new Random();
- int period = random.nextInt(2);
- boolean borderGap = true;
- int frames = 1;
- int phase = random.nextInt(2);
- for (int i = 0; i < h1; i++) {
- double d = (double) (period >> 1)
- * Math.sin((double) i / (double) period
- + (6.2831853071795862D * (double) phase)
- / (double) frames);
- g.copyArea(0, i, w1, 1, (int) d, 0);
- if (borderGap) {
- g.setColor(color);
- g.drawLine((int) d, i, 0, i);
- g.drawLine((int) d + w1, i, w1, i);
- }
- }
- }
- private static void shearY(Graphics g, int w1, int h1, Color color) {
- Random random = new Random();
- int period = random.nextInt(40) + 10; // 50;
- boolean borderGap = true;
- int frames = 20;
- int phase = 7;
- for (int i = 0; i < w1; i++) {
- double d = (double) (period >> 1)
- * Math.sin((double) i / (double) period
- + (6.2831853071795862D * (double) phase)
- / (double) frames);
- g.copyArea(i, 0, 1, h1, 0, (int) d);
- if (borderGap) {
- g.setColor(color);
- g.drawLine(i, (int) d, i, 0);
- g.drawLine(i, (int) d + h1, i, h1);
- }
- }
- }
- }<strong>
- </strong>
效果
测试类Test.java
- public class Test{
- public static void main(String[] args) {
- try {
- String code = ImgCode.getCode(5);
- ImgCode.outputCodeImg(120, 30, code, new FileOutputStream(new File("c:/"+code+".jpg")));
- Img.outputCodeImg(80, 25, code, new FileOutputStream(new File("c:/"+code+".png")));
- System.out.println(code);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
0 0
- Java生成图片验证码
- Java生成验证码图片
- Java生成图片验证码
- Java生成图片验证码
- JAVA 生成验证码图片
- Java生成图片验证码
- Java生成验证码图片
- java生成验证码图片
- java生成验证码图片
- java 生成图片验证码
- java生成图片验证码
- java验证码图片生成
- JAVA生成图片验证码
- JAVA生成图片验证码
- java--生成图片验证码
- java生成图片验证码
- Java生成图片验证码
- java生成图片验证码
- markdown插入图片
- Linux中yum和apt-get用法及区别
- 通过eclipse查看、阅读hadoop2.4源码
- 关于hibernate 1+n问题的一些总结
- 搜索
- JAVA生成图片验证码
- 一次误用栈导致的错误。
- sas vname 返回最大值对应的变量名
- Spring注解@Component、@Repository、@Service、@Controller区别
- python2.7学习笔记(3) ——字符编码,列表,元组
- ISO14229 根据标识符读取数据ReadDataByIdentifier(22 Hex) service
- POJ 1061 青蛙的约会 (扩展欧几里得)
- vi 显示行号
- C++/STL中 vector中对 “=”赋值运算符的支持