java生成图片点击验证码_JAVA 生成验证码图片

该博客介绍了一种使用JAVA生成随机点击验证码图片的方法,包括设置背景色、字体、颜色,并通过Graphics2D绘制文字到BufferedImage上,最后可以将图片保存为jpg格式或直接输出到HTTP响应中。
摘要由CSDN通过智能技术生成

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import java.io.File;

import java.util.LinkedList;

import java.util.Random;

import javax.imageio.ImageIO;

import javax.servlet.http.HttpServletResponse;

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGImageEncoder;

// import java.awt.Graphics;

public class Test

{

public String sRand = "";

public Test()

{

}

public 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);

}

/**

* 通过文件创建图像 格式为jpg类型

*/

public void creatImage(String fileName, String content)

{

// 在内存中创建图象

int width = 60, height = 20;

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

// 获取图形上下文

Graphics2D g = image.createGraphics();

// Graphics g=image.getGraphics();

// 生成随机类

Random random = new Random();

// 设定背景色

g.setColor(getRandColor(200, 250));

g.fillRect(0, 0, width, height);

// 设定字体

g.setFont(new Font("Times   New   Roman", Font.PLAIN, 18));

g.setColor(Color.black);// 黑色文字

g.drawString(content, 10, 15);

g.dispose();

try

{

File f = new File(fileName);

if (!f.exists())

{

f.createNewFile();

}

else

{

Thread.sleep(200);

f.delete();

f.createNewFile();

}

ImageIO.write(image, "jpg", f);

}

catch (Exception e)

{

e.printStackTrace();

}

}

/**

* 创建图像 格式为jpg类型

*

* @param content -

*            String 图片输出内容

* @return java.awt.image.BufferedImage

* @since 2005-7-19

*/

public BufferedImage getBufferedImage(String content)

{

int width = 60, height = 20;

return getBufferedImage(content, width, height);

}

/**

* 创建图像 格式为jpg类型

*

* @param content -

*            String 图片输出内容

* @param width -

*            int 图片宽度

* @param height -

*            int 图片高度

* @return java.awt.image.BufferedImage

* @since 2005-7-19

*/

public BufferedImage getBufferedImage(String content, int width, int height)

{

// 在内存中创建图象

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

// 获取图形上下文

Graphics2D g = image.createGraphics();

// Graphics g=image.getGraphics();

// 生成随机类

Random random = new Random();

// 设定背景色

g.setColor(getRandColor(200, 250));

g.fillRect(0, 0, width, height);

// 设定字体

g.setFont(new Font("Times   New   Roman", Font.PLAIN, 18));

g.setColor(Color.black);// 黑色文字

g.drawString(content, 10, 15);

g.dispose();

return image;

}

/**

* 将现有BufferedImage融合进Response

*

* @param response -

*            javax.servlet.http.ServletResponse 将使用的response对象

* @param img -

*            java.awt.image.BufferedImage

* @since 2005-7-19

*/

public void response(HttpServletResponse response, BufferedImage img)

{

try

{

response.setContentType("image/jpg;charset=GB2312");// 设定输出的类型

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(response.getOutputStream());

encoder.encode(img);// 对图片进行输出编码

}

catch (Exception e)

{

e.printStackTrace();

}

}

/**

* 返回一个4位的验证码

*/

public String getContent() throws InterruptedException

{

String content = "";

for (int i = 0; i < 4; i++)

{

content += getChar();

Thread.sleep(new Random().nextInt(10) + 10);// 休眠以控制字符的重复问题

}

System.out.println("---------------"+content);

return content;

}

/**

* 获取随机字符

*/

public char getChar()

{

Random random = new Random();

char ch = '0';

LinkedList ls = new LinkedList();

for (int i = 0; i < 10; i++)

{// 0-9

ls.add(String.valueOf(48 + i));

}

for (int i = 0; i < 26; i++)

{// A-Z

ls.add(String.valueOf(65 + i));

}

for (int i = 0; i < 26; i++)

{// a-z

ls.add(String.valueOf(97 + i));

}

int index = random.nextInt(ls.size());

if (index > (ls.size() - 1))

{

index = ls.size() - 1;

}

ch = (char)Integer.parseInt(String.valueOf(ls.get(index)));

return ch;

}

/**  * 测试  */ public static void main(String[] args) throws Exception {  Test c = new Test();  c.creatImage("d://me4.jpg", c.getContent()); }}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值