2-3问题,验证码显示不出内容
来源:2-3 生成验证码页面
qq_9o後虛徦_0
2018-06-05 11:05:58
package com.imooc.code;
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.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import com.sun.org.apache.xerces.internal.dom.PSVIAttrNSImpl;
public class CaptcahCode {
public static String drawImage(HttpServletResponse response) {
StringBuilder builder = new StringBuilder();
for(int i =0;i<5;i++) {
builder.append(randomChar());
}
String code = builder.toString();
//2.定义图片的宽度和高度
int width = 120;
int height = 25;
//简历bufferedImage对象,制定图片的长度和宽度以及色彩
BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);
//获得到Graphics2D绘制对象,开始绘制验证码
Graphics2D g = bi.createGraphics();
//设置文字的字体和大小
Font font = new Font("微软雅黑",Font.PLAIN,20);
//设置字体颜色
Color color = new Color(0,0,0);
g.setFont(font);
g.setColor(color);
//设置背景
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 = (width-bounds.getHeight())/2;
double ascent = bounds.getY();
double baseY = y-ascent;
g.drawString(code, (int)x, (int)y);
g.dispose();
//生成验证码
try {
ImageIO.write(bi,"jpg",response.getOutputStream());
//刷新响应流
response.flushBuffer();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return code;
}
private static char randomChar() {
String string = "QWERTYUIOPASDFGHJKLZXCVBNM0123456789";
Random random = new Random();
//random.nextInt(string.length()),随机生成一个,0-string.length(),之间的数
//charAt,返回指定索引处的 char 值。索引范围为从 0 到 length() - 1。序列的第一个 char 值在索引 0 处,第二个在索引 1 处,依此类推,这类似于数组索引。
return string.charAt(random.nextInt(string.length()));
}
public static void main(String[] args) {
CaptcahCode.drawImage(null);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.imooc.code.CaptcahCode"%>
<%
response.setHeader("pragma","no-cache");
response.setHeader("cache-control","no-cache");
response.setHeader("expires","0");
String code = CaptcahCode.drawImage(response);
session.setAttribute("code",code);
out.clear();
out = pageContext.pushBody();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>java验证码</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<img src="code.jsp" alt="">
</body>
</html>
2回答
主要是通过计算使图片居中的,请看下图代码double y的参数应该是heigth-bounds.getY();(int y)应该修使用baseY。
修订之后的代码如下图:
修订之后的效果如下图:
如果有不理解的地方请参考http://class.imooc.com/course/qadetail/54427,建议你动手自己测试一下就能理解是什么意思了。祝学习愉快~
chrismorgen
2018-06-05
什么都不显示是因为绘制的矩形窗体超出了图片的范围,建议你将heigth设置为70,或者宽高和老师的一样。如果我的建议解决了你的问题,请采纳,祝学习愉快~
相似问题