java 字符扭曲 验证码

hfdkjshfks 学生  2015-08-17 02:45:35

像这种的验证码
...全文
222 9 点赞 打赏 收藏 举报
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
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
对,怎么才能做成那样的效果。 你那两个都是图片进行扭曲,不是我想要的。
  • 打赏
  • 举报
回复
没看懂你的需求....你是要实现这种么 看看 看看
  • 打赏
  • 举报
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-08-17 02:45
社区公告
暂无公告