首页>代码>spring mvc maven,websocket在线简单聊天室系统,ace master,extjs>/chatwebsocket/src/main/java/com/chatwebsocket/controller/system/seccode/SecCodeController.java
package com.chatwebsocket.controller.system.seccode;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.chatwebsocket.controller.base.BaseController;
import com.chatwebsocket.util.Const;

/**
 * 登录验证码
 * 
 * @author zhangmingjie
 * @date 2019年4月1日 下午4:36:45
 * @version 1.0
 */
@Controller
public class SecCodeController extends BaseController {

	@RequestMapping("/code")
	public void generate(HttpServletResponse response) {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		String code = drawImg(output);

		HttpSession session = getRequest().getSession();
		session.setAttribute(Const.SESSION_SECURITY_CODE, code);

		try {
			ServletOutputStream out = response.getOutputStream();
			output.writeTo(out);
			out.close();
		} catch (IOException e) {
		}
	}

	/**
	 * 绘制图形
	 * 
	 * @author zhangmingjie
	 * @param output
	 * @return
	 * @date 2019年4月1日 下午4:37:43
	 * @version 1.0
	 */
	private String drawImg(ByteArrayOutputStream output) {
		String code = "";
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < 4; i++) {
			sb.append(randomChar());
		}
		code = sb.toString();

		int width = 75;
		int height = 25;
		BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);

		Graphics2D g = bi.createGraphics();
		Font font = new Font("Times New Roman", Font.PLAIN, 20);
		g.setFont(font);
		g.setColor(new Color(66, 2, 82));
		g.setBackground(new Color(226, 226, 240));
		g.clearRect(0, 0, width, height);

		FontRenderContext context = g.getFontRenderContext();
		Rectangle2D bounds = font.getStringBounds(code, context);

		double x = (width - bounds.getWidth()) / 2;
		double y = (height - bounds.getHeight()) / 2;
		double ascent = bounds.getY();
		double baseY = y - ascent;
		g.drawString(code, (int) x, (int) baseY);
		g.dispose();
		try {
			ImageIO.write(bi, "jpg", output);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return code;
	}

	/**
	 * 绘制字符
	 * 
	 * @author zhangmingjie
	 * @return
	 * @date 2019年4月1日 下午4:38:09
	 * @version 1.0
	 */
	private char randomChar() {
		Random r = new Random();
		String s = "ABCDEFGHJKLMNPRSTUVWXYZ0123456789";
		return s.charAt(r.nextInt(s.length()));
	}

}
最近下载更多
80730176  LV7 2023年12月8日
云淡风轻jh  LV10 2023年11月28日
Tg171017  LV12 2023年2月18日
lironggang  LV38 2023年2月16日
nanaland  LV9 2023年2月9日
yangguang  LV8 2022年5月20日
xiex909  LV27 2022年5月18日
17岁的孩子想糖吃  LV7 2021年12月31日
137455054  LV9 2021年12月2日
18219194576  LV7 2021年11月10日
最近浏览更多
WBelong  LV7 4月2日
dafeiyu  LV10 3月1日
lilong007  LV20 3月1日
阿凡达  LV9 2月29日
云淡风轻jh  LV10 2023年11月28日
CrystalQ  LV8 2023年10月26日
Jackson1 2023年9月8日
暂无贡献等级
陈小灏  LV14 2023年5月16日
微信网友_6444139264921600  LV6 2023年4月29日
gzryue  LV6 2023年4月23日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友