这篇文章主要介绍了java 实现输出随机图片实例代码的相关资料,需要的朋友可以参考下
java 实现输出随机图片实例代码
输出随机图片(captcha图像):completely automated public turing test to tell computers and humans apart (全自动区分计算机和人类的测试)
相关主要类(jdk 查看api)
bufferedimage:内存图像
graphics:画笔
imageio:输出图像
放在html页面上83bc49b7e8bcbe1e90eb7bc15fbd721e
注意:浏览器默认会缓存图片
public static int width = 120;
public static int height = 25;
public void doget(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
response.setcontenttype("text/html");
//创建内存图像
bufferedimage image = new bufferedimage(width,height,bufferedimage.type_int_rgb);
//勾勒图像
graphics graphics = image.getgraphics();
//设置背景
graphics.setcolor(color.white);
graphics.fillrect(0, 0, width, height);
//设置边框
graphics.setcolor(color.blue);
graphics.drawrect(1, 1, width-2, height-2);
//画干扰线
graphics.setcolor(color.yellow);
for(int i=0;i<8;i++){
int xstart = new random().nextint(width);
int ystart = new random().nextint(height);
int xend = new random().nextint(width);
int yend = new random().nextint(height);
graphics.drawline(xstart, ystart, xend, yend);
}
//写随机数
graphics.setcolor(color.red);
int x = 5;
for(int i=0;i<4;i++){
graphics.drawstring(new random().nextint(9)+"", x, 20);
x+=30;
}
response.setcontenttype("image/jpeg");//设置响应格式
imageio.write(image, "jpeg", response.getoutputstream());
}
以上就是java实现随机图片生成的实例代码的详细内容。
