jcaptcha+ofbiz生成验证码

1.  在jcaptcha的官网上下载jar 【http://jcaptcha.sourceforge.net】

    下载一个jcaptcha-1.0-bin.zip解压缩,将其中的jcaptcha-1.0-all.jar拷贝至{ofbiz.home}\framework\base\lib\commons目录下


2. 编写代码如下

单例引擎

package com.web.image;

import java.awt.Font;

import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
import com.octo.captcha.component.image.backgroundgenerator.FunkyBackgroundGenerator;
import com.octo.captcha.component.image.color.RandomRangeColorGenerator;
import com.octo.captcha.component.image.fontgenerator.FontGenerator;
import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
import com.octo.captcha.component.image.textpaster.RandomTextPaster;
import com.octo.captcha.component.image.textpaster.TextPaster;
import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;
import com.octo.captcha.component.image.wordtoimage.WordToImage;
import com.octo.captcha.component.word.wordgenerator.RandomWordGenerator;
import com.octo.captcha.component.word.wordgenerator.WordGenerator;
import com.octo.captcha.engine.image.ListImageCaptchaEngine;
import com.octo.captcha.image.gimpy.GimpyFactory;
import com.octo.captcha.service.captchastore.FastHashMapCaptchaStore;
import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
import com.octo.captcha.service.image.ImageCaptchaService;

public class CaptchaServiceSingleton {

	private static ImageCaptchaService instance;
	
	public static ImageCaptchaService getInstance() {
		if(instance == null) {
			instance = new DefaultManageableImageCaptchaService(
					new FastHashMapCaptchaStore(), new MyImageCaptchaEngine(), 180,
					100000, 75000);
		}
		return instance;
	}
}

class MyImageCaptchaEngine extends ListImageCaptchaEngine {
	protected void buildInitialFactories() {
		
		//生成随机字符
		WordGenerator wgen = new RandomWordGenerator(
				"abcdefghijklmnopqrstuvwxyz123456789");
		RandomRangeColorGenerator cgen = new RandomRangeColorGenerator(
				new int[] { 0, 100 }, new int[] { 0, 100 },
				new int[] { 0, 100 });
		
		//文字显示的个数
		TextPaster textPaster = new RandomTextPaster(6, 6, cgen, true);
		
		//图片的大小
		BackgroundGenerator backgroundGenerator = new FunkyBackgroundGenerator(
				150, 50);
		
		//字体格式
		Font[] fontsList = new Font[] { new Font("Arial", 0, 15),
				new Font("Tahoma", 0, 15), new Font("Verdana", 0, 15) };
		
		//文字的大小
		FontGenerator fontGenerator = new RandomFontGenerator(15, 30, fontsList);
		
		WordToImage wordToImage = new ComposedWordToImage(fontGenerator,
				backgroundGenerator, textPaster);
		this.addFactory(new GimpyFactory(wgen, wordToImage));
	}
}

event类

package com.web.image;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ofbiz.base.util.Debug;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageCaptchaEvent {
	
	public static final String module = ImageCaptchaEvent.class.getName();
	
	public static String imageGeneration(HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		Debug.log("imageGeneration start <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<", module);
		
		byte[] captchaChallengeAsJpeg = null;

		ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();

		try {

			String captchaId = req.getSession().getId();

			BufferedImage challenge = CaptchaServiceSingleton.getInstance()
					.getImageChallengeForID(captchaId, req.getLocale());
			
			JPEGImageEncoder jpegEncoder = JPEGCodec
					.createJPEGEncoder(jpegOutputStream);
			
			jpegEncoder.encode(challenge);

		} catch (Exception e) {
			e.printStackTrace();
		}

		captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

		resp.setHeader("Cache-Control", "no-store");
		resp.setHeader("Pragma", "no-cache");
		resp.setDateHeader("Expires", 0);
		resp.setContentType("image/jpeg");

		ServletOutputStream responseOutputStream = resp.getOutputStream();
		responseOutputStream.write(captchaChallengeAsJpeg);
		responseOutputStream.flush();
		responseOutputStream.close();
		Debug.log("imageGeneration end >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", module);
		return "success";
	}
}

在controller.xml中添加如下配置

<request-map uri="jcaptcha">
        <security https="true" auth="true"/>
	<event type="java" path="com.web.image.ImageCaptchaEvent" invoke="imageGeneration"/>
	<response name="success" type="none"/>
</request-map>

在ftl文件中加入图片

<a href="#" title="看不清? 点我一下!" style="text-decoration:none;">
<img width="80" height="30" src="<@ofbizUrl>jcaptcha</@ofbizUrl>" οnclick="changeImage(this)" alt="看不清? 点我一下!" style="border:1px solid red;cursor:pointer;"/>
</a>

<script type="text/javascript">
function changeImage(obj) {
	if(obj && obj.src) {
		obj.src = "<@ofbizUrl>jcaptcha</@ofbizUrl>?temp=" + new Date().getTime().toString(36);
	}
	return false;
}
</script>

至此已经完成了图片的生成



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值