java 字符扭曲 验证码

hfdkjshfks 2015-08-17 02:45:35

像这种的验证码
...全文
337 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hfdkjshfks 2015-08-17
  • 打赏
  • 举报
回复
引用 5 楼 shijing266 的回复:
额 注释符号没打好,你将就着看吧
额,你的那些类都没有给我,我还是不知道你是怎么实现的啊???
hfdkjshfks 2015-08-17
  • 打赏
  • 举报
回复
引用 6 楼 dcs4569 的回复:
百度搜这种html生成验证码的代码一大堆,推荐去搜搜
我都搜了一整天了,被逼无奈才到这里问的。
dcs4569 2015-08-17
  • 打赏
  • 举报
回复
百度搜这种html生成验证码的代码一大堆,推荐去搜搜
dcs4569 2015-08-17
  • 打赏
  • 举报
回复
百度搜这种html生成验证码的代码一大堆,推荐去搜搜
  • 打赏
  • 举报
回复
额 注释符号没打好,你将就着看吧
  • 打赏
  • 举报
回复
我这里有一段我们正在用的验证码,你可以看看
/*
* ValidateCodeServlet.java
* Created on  2015/3/19 10:30
*/
package com.cq2022.zago.base.servlet;


import com.cq2022.zago.base.constant.Constants;
import com.cq2022.zago.base.utils.vcode.ValidateCode;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 验证码
 * @version 1.0.1
 */
public class ValidateCodeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    @Override
    protected void doGet(HttpServletRequest reqeust,
                         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 = reqeust.getSession();
        ValidateCode vCode = new ValidateCode(120, 40, 5, 50);
        // 将认证码存入SESSION
        session.setAttribute(Constants.RAND, vCode.getCode());
        vCode.write(response.getOutputStream());
    }
/**
 * web.xml 添加servlet
 <servlet>
 <servlet-name>validateCodeServlet</servlet-name>
 <servlet-class>cn.dsna.util.images.ValidateCodeServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>validateCodeServlet</servlet-name>
 <url-pattern>*.images</url-pattern>
 </servlet-mapping>

 在地址栏输入XXX/dsna.images 测试
 */

}
/*
* VerifyCodeServlet.java
* Created on  2015/6/7 18:53
*/
package com.cq2022.zago.base.servlet;

import com.cq2022.zago.base.constant.Constants;
import com.github.bingoohuang.patchca.color.ColorFactory;
import com.github.bingoohuang.patchca.color.SingleColorFactory;
import com.github.bingoohuang.patchca.custom.ConfigurableCaptchaService;
import com.github.bingoohuang.patchca.filter.predefined.*;
import com.github.bingoohuang.patchca.font.RandomFontFactory;
import com.github.bingoohuang.patchca.text.renderer.BestFitTextRenderer;
import com.github.bingoohuang.patchca.text.renderer.TextRenderer;
import com.github.bingoohuang.patchca.utils.encoder.EncoderHelper;
import com.github.bingoohuang.patchca.word.RandomWordFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

/**
 * 验证码
 *
 * @version 1.0.1
 */
public class VerifyCodeServlet extends HttpServlet {

    private static final String DEFAULT_CHARACTERS = "23456789abcdefghigkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ"; // 自己设置

    private static ConfigurableCaptchaService ccs = null;
    private static ColorFactory cf = null;
    private static TextRenderer tr = null;
    private static RandomFontFactory ff = null;
    private static RandomWordFactory rwf = null;
    private static Random r = new Random();
    private static CurvesRippleFilterFactory crff = null;  //干扰线波纹
    private static MarbleRippleFilterFactory mrff = null;  //大理石波纹
    private static DoubleRippleFilterFactory drff = null;  //双波纹
    private static WobbleRippleFilterFactory wrff = null;   //摆波纹
    private static DiffuseRippleFilterFactory dirff = null;  //漫波纹

    /**
     * Constructor of the object.
     */
    public VerifyCodeServlet() {
        super();
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
        super.init();
        ccs = new ConfigurableCaptchaService();


//        cf = new SingleColorFactory(new Color(25, 60, 170));
        cf = new ColorFactory() {
            @Override
            public Color getColor(int x) {
                int[] c = new int[3];
                int i = r.nextInt(c.length);
                for (int fi = 0; fi < c.length; fi++) {
                    if (fi == i) {
                        c[fi] = r.nextInt(71);
                    } else {
                        c[fi] = r.nextInt(256);
                    }
                }
                return new Color(c[0], c[1], c[2]);
            }
        };


        ff = new RandomFontFactory();
        rwf = new RandomWordFactory();
        tr = new BestFitTextRenderer();

        crff = new CurvesRippleFilterFactory(ccs.getColorFactory());
        drff = new DoubleRippleFilterFactory();
        wrff = new WobbleRippleFilterFactory();
        dirff = new DiffuseRippleFilterFactory();
        mrff = new MarbleRippleFilterFactory();

        rwf.setCharacters(DEFAULT_CHARACTERS);
        ff.setRandomStyle(true);
//        ff.setMaxSize(16);
//        ff.setMinSize(12);


        ccs.setTextRenderer(tr);
        ccs.setFontFactory(ff);
        ccs.setWordFactory(rwf);
        ccs.setColorFactory(cf);
//        ccs.setWidth(50);
//        ccs.setHeight(20);
    }

    /**
     * The doGet method of the servlet. <br>
     * <p/>
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HttpSession session = request.getSession(true);
        OutputStream os = response.getOutputStream();

        rwf.setMaxLength(5);
        rwf.setMinLength(4);

        switch (r.nextInt(5)) {
            case 0:
                ccs.setFilterFactory(crff);
                break;
            case 1:
                ccs.setFilterFactory(mrff);
                break;
            case 2:
                ccs.setFilterFactory(drff);
                break;
            case 3:
                ccs.setFilterFactory(wrff);
                break;
            case 4:
                ccs.setFilterFactory(dirff);
                break;
        }
        setResponseHeaders(response);
        String captcha = EncoderHelper.getChallangeAndWriteImage(ccs, "png", os);
        session.setAttribute(Constants.RAND, captcha);
        os.flush();
        os.close();
    }

    protected void setResponseHeaders(HttpServletResponse response) {
        response.setContentType("image/png");
        response.setHeader("Cache-Control", "no-cache, no-store");
        response.setHeader("Pragma", "no-cache");
        long time = System.currentTimeMillis();
        response.setDateHeader("Last-Modified", time);
        response.setDateHeader("Date", time);
        response.setDateHeader("Expires", time);
    }

    /**
     * The doPost method of the servlet. <br>
     * <p/>
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doGet(request, response);
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        rwf = null;
        cf = null;
        ccs = null;
        ff = null;
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }
}
然后jsp调用: <img class="codeimg" src="${ctx}/getvcode/v2" id="verifyImg" onclick="changeVerify()" width="108" height="38">
hfdkjshfks 2015-08-17
  • 打赏
  • 举报
回复
引用 1 楼 shijing266 的回复:
没看懂你的需求....你是要实现这种么 看看 看看
对,怎么才能做成那样的效果。 你那两个都是图片进行扭曲,不是我想要的。
hfdkjshfks 2015-08-17
  • 打赏
  • 举报
回复
对,怎么才能做成那样的效果。 你那两个都是图片进行扭曲,不是我想要的。
  • 打赏
  • 举报
回复
没看懂你的需求....你是要实现这种么 看看 看看
随着通信电子技术的迅速发展,信息技术给家居行业产生了深远的影响,家居环境的智能化监控已经成为智能家居的一个重要的发展方向。人们逐渐对自己的生活提出一种更高的要求,他们需要一种智能化、可交互,并且融合现代创新科技的产品来改善他们的生活环境,使他们生活更加安全、舒适、便捷、智能。本文根据智能家居的发展背景和研究现状,并且从实用性和可行性角度出发,研究设计了一种基于STM32单片机的智能家居系统。该系统由一个多功能综合的技术系统组成,各个多功能子系统间具有协同配合能力。基于STM32单片机实现的功能子系统包括:智能温度检测,智能湿度检测,智能烟雾/火灾检测智能检测,无线传输,人机交互机构,风扇调节,报警模块。 整个系统分为前端51单片机采集板和后端STM32单片机接收板。 采集板使用DHT11温湿度传感器、MQ烟雾传感器完成室内家居环境的采集。然后通过nRFL24L01将采集到的数据发送给后端。接收板使用LCD1602完成数据显示、使用蜂鸣器模块报警,使用风扇驱动模块调节室内家居环境。本文完成了智能家居控制系统前端、后端软硬件的设计,使用Altium designer绘制了电路原理图,使用Keil C完成了51单片机和STM32单片机的编程与调试。 最后,对本文设计的基于STM32的智能家居控制系统进行部署调试。试验结果表明,该系统可成功应用在智能家居环境检测调节和火灾安全防护的领域,可提高家居生活智能化水平。

81,111

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧