代码实现简单的生成随机验证码小程序
在小程序应用中,我们会遇到随机验证码图片,那么如何使用代码实现一个简单的生成随机验证码的小程序
//随机生成验证码
//第一步:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test(response);
}
private void test(HttpServletResponse response) throws IOException {
int width = 120,height=25;
//生成一张图片 此时得到一张宽120,长25的一张黑色图片
BufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics grap = .getGraphics();//得到一个画笔
//填充背景色
grap.setColor(Color.pink);
//设置填充的区域
grap.fillRect(1, 1, width-2, height-2);
//设置边框的颜色 同填充背景颜色 靠近谁是设置哪个属性的颜色
grap.setColor(Color.red);
grap.drawRect(0, 0, width-1, height-1);
//设置字体
grap.setFont(new Font("黑体", Font.BOLD|Font.ITALIC, 18));
//向图片上写字 嘿嘿随机生成了字符串
Random r = new Random();
int p = 15;
for(int i=1;i
login.html
// 大功告成就可以发布到tomcat上浏览了
//此处介绍一种懒人方法,在doGet方法中,其中ValidateCode四个函数分别是矩形的宽、高以及验证码的个数和干扰线的条数,然后第一步的函数就可统统省略了
ValidateCode code = new ValidateCode(320, 25, 4, 8);
code.write(response.getOutputStream());
//注:导相应的ValidateCode的JAR包喔。
第二部分:如何开通一个小商店
****更多行业产品开发方案,请关注 JSudo****
<以上资讯仅供参考,如需解决具体问题,建议您关注作者;如果有软件产品开发需求,可在线咨询获取产品开发方案和报价>
小程序开发
小程序商城
微信小程序
加客服微信获取更多内容