java 图片线条_在java的image图片类中画线怎么画

这段代码是一个基于Servlet的Java实现的验证码生成器。它创建了一个80x25像素的图像,并在其中随机绘制了由大写字母、小写字母和数字组成的四位验证码。验证码的颜色、字体和位置都是随机的,以增加识别难度。同时,代码还包含了一些干扰线条以增强安全性。生成的验证码会存储在HttpSession中,便于后续验证使用。
摘要由CSDN通过智能技术生成

展开全部

package com.weifeng.sys.util;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.util.Random;

import javax.imageio.ImageIO;

import javax.servlet.ServletException;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

public class Image extends HttpServlet {

private static final long serialVersionUID = 1L;

private String sRand;//验证码

//验证码集合

private String[] allchar = {

e5a48de588b662616964757a686964616f31333337623432"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W","X", "Y", "Z",

"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y",

"1", "2", "3", "4", "5", "6", "7", "8", "9"};

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("image/jpeg");

response.setHeader("Pragma", "No-cache");

response.setHeader("Cache-Control", "no-cache");

response.setDateHeader("Expires", 0);

HttpSession session = request.getSession();

//在内存中创建图象

int width = 80, height = 25;

//保存图片

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

//保存随即产生的验证码

StringBuffer sb = new StringBuffer();

//随机数生成器

Random r = new Random();

//随机生成四个随即字符

for (int i = 0; i 

sb.append(allchar[r.nextInt(allchar.length)]);

}

sRand = sb.toString();

//image = new BufferedImage(80, 40, BufferedImage.TYPE_3BYTE_BGR);

//绘图上下文

Graphics g = image.getGraphics();

// 设定背景色

Color bgColor=getRandColor(200,250);

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

// 设定字体

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

//绘制验证码图片

int yzmWidth = 20, yzmHeight = 24;

for (int i = 0; i 

BufferedImage bi = new BufferedImage(yzmWidth, yzmHeight, BufferedImage.TYPE_3BYTE_BGR);

Graphics bg = bi.getGraphics();

bg.setColor(bgColor);

bg.fillRect(0, 0, yzmWidth, yzmHeight);

bg.setColor(new Color(yzmHeight + r.nextInt(110), yzmHeight + r.nextInt(110), yzmHeight + r.nextInt(110)));

bg.setFont(new Font("", r.nextInt(4) + 1, r.nextInt(5) + 16));

g.setColor(new Color(yzmHeight + r.nextInt(110), yzmHeight + r.nextInt(110), yzmHeight + r.nextInt(110)));// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成

bg.drawString(sRand.substring(i, i + 1), 2, yzmWidth);

g.drawImage(bi, 0 + yzmWidth * i, 0, yzmWidth, yzmHeight, null);

}

//干扰线

//int grxHeight = 25;

//for (int j = 0; j <50; j++) {

//int x = r.nextInt(40);

// int y = r.nextInt(grxHeight);

// int xl = r.nextInt(80);

// int yl = r.nextInt(grxHeight);

// g.setColor(new Color(10 + r.nextInt(90), 10 + r.nextInt(90), 10 + r.nextInt(90),r.nextInt(80)));

// g.drawLine(x, y, xl, yl);

//}

//干扰线

//for (int j = 0; j <100; j++) {

//int x = r.nextInt(60);

// int y = r.nextInt(grxHeight);

// g.setColor(new Color(10 + r.nextInt(90), 10 + r.nextInt(90), 10 + r.nextInt(90),100));

// g.drawLine(x, y, x, y);

//}

//画边框

g.setColor(new Color(1));

g.drawRect(0,0,width-1,height-1);

// 将认证码存入SESSION

session.setAttribute("rand", sRand);

// 图象生效

g.dispose();

ServletOutputStream responseOutputStream = response.getOutputStream();

// 输出图象到页面

ImageIO.write(image, "JPEG", responseOutputStream);

// 以下关闭输入流!

responseOutputStream.flush();

responseOutputStream.close();

}

// 给定范围获得随机颜色

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

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

processRequest(request, response);

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

processRequest(request, response);

}

public String getServletInfo() {

return "Short description";

}

}

中间屏蔽的那段就是画线操作

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值