jcaptcha与jdk兼容问题,验证码黑框问题处理

在使用jcaptcha进行验证码的生成时会遇到验证码黑框的问题,经过字啊网上搜索,最终发现是jdk版本的兼容问题

这是jdk7的写法,jdk8会出现黑框


import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
import com.octo.captcha.component.image.backgroundgenerator.UniColorBackgroundGenerator;
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.DecoratedRandomTextPaster;
import com.octo.captcha.component.image.textpaster.TextPaster;
import com.octo.captcha.component.image.textpaster.textdecorator.LineTextDecorator;
import com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator;
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 java.awt.*;
 

public class CaptchaImageEngine extends ListImageCaptchaEngine {
	
 
    @Override  
    protected void buildInitialFactories() {
 
    	// 自定义要展示的内容
		WordGenerator wgen = new RandomWordGenerator("abcdefghijklmnopqrstuvwxyz");
 
		// 生成随机颜色,参数分别表示RGBA的取值范围
		RandomRangeColorGenerator colors = new RandomRangeColorGenerator(
				new int[] { 0, 255 }, new int[] { 0, 180 },
				new int[] { 0, 210 });
		// Arial,Tahoma,Verdana,Helvetica,宋体,黑体,幼圆
		Font[] fonts = new Font[] { new Font("Arial", 0, 15),
				new Font("Tahoma", 0, 15), new Font("Verdana", 0, 15),
				new Font("Helvetica", 0, 15), new Font("宋体", 0, 15),
				new Font("黑体", 0, 15), new Font("幼圆", 0, 15) };
 
		// 设置字符以及干扰线
		RandomRangeColorGenerator lineColors = new RandomRangeColorGenerator(
				new int[] { 0, 255 }, new int[] { 150, 180 }, new int[] {
				150, 210 });
		// 随机文字多少和颜色,参数1和2表示最少生成多少个文字和最多生成多少个
		TextPaster textPaster = new DecoratedRandomTextPaster(new Integer(4),
				new Integer(4),
				colors,
				true,
				new TextDecorator[] { new LineTextDecorator(new Integer(1), lineColors) });
        //生成背景的大小这里是宽140高45的图片,背景颜色为白色
		BackgroundGenerator backgroundGenerator = new UniColorBackgroundGenerator(new Integer(140), new Integer(45), Color.white);
		// 字体随机生成
		FontGenerator fontGenerator = new RandomFontGenerator(new Integer(30), new Integer(0), fonts);
 
 
		WaterFilter water = new WaterFilter();

		water.setAmplitude(4d);
		water.setAntialias(true);
		water.setPhase(20d);
		water.setWavelength(60d);

	~~ImageDeformation backDef = new ImageDeformationByFilters(
				new ImageFilter[] {});
		ImageDeformation textDef = new ImageDeformationByFilters(
				new ImageFilter[] {});
		ImageDeformation postDef = new ImageDeformationByFilters(
				new ImageFilter[] { water });
	   WordToImage cwti = new      DeformedComposedWordToImage(fontGenerator,backgroundGenerator,textPaster, backDef, textDef, postDef);~~ 
 
		
 
    	this.addFactory(new GimpyFactory(wgen, cwti));
    
    }  
  
} 

这是jdk7,jdk8两者兼容的写法


import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
import com.octo.captcha.component.image.backgroundgenerator.UniColorBackgroundGenerator;
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.DecoratedRandomTextPaster;
import com.octo.captcha.component.image.textpaster.TextPaster;
import com.octo.captcha.component.image.textpaster.textdecorator.LineTextDecorator;
import com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator;
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 java.awt.*;
 
/**
 * @author: Mqs
 * @date 2018/8/30 14:52
 * @description:
 */
public class CaptchaImageEngine extends ListImageCaptchaEngine {
	
 
    @Override  
    protected void buildInitialFactories() {
 
    	// 自定义要展示的内容
		WordGenerator wgen = new RandomWordGenerator("abcdefghijklmnopqrstuvwxyz");
 
		// 生成随机颜色,参数分别表示RGBA的取值范围
		RandomRangeColorGenerator colors = new RandomRangeColorGenerator(
				new int[] { 0, 255 }, new int[] { 0, 180 },
				new int[] { 0, 210 });
		// Arial,Tahoma,Verdana,Helvetica,宋体,黑体,幼圆
		Font[] fonts = new Font[] { new Font("Arial", 0, 15),
				new Font("Tahoma", 0, 15), new Font("Verdana", 0, 15),
				new Font("Helvetica", 0, 15), new Font("宋体", 0, 15),
				new Font("黑体", 0, 15), new Font("幼圆", 0, 15) };
 
		// 设置字符以及干扰线
		RandomRangeColorGenerator lineColors = new RandomRangeColorGenerator(
				new int[] { 0, 255 }, new int[] { 150, 180 }, new int[] {
				150, 210 });
		// 随机文字多少和颜色,参数1和2表示最少生成多少个文字和最多生成多少个
		TextPaster textPaster = new DecoratedRandomTextPaster(new Integer(4),
				new Integer(4),
				colors,
				true,
				new TextDecorator[] { new LineTextDecorator(new Integer(1), lineColors) });
        //生成背景的大小这里是宽140高45的图片,背景颜色为白色
		BackgroundGenerator backgroundGenerator = new UniColorBackgroundGenerator(new Integer(140), new Integer(45), Color.white);
		// 字体随机生成
		FontGenerator fontGenerator = new RandomFontGenerator(new Integer(30), new Integer(0), fonts);
 
		// 生成图片输出
		WordToImage cwti = new ComposedWordToImage(fontGenerator, backgroundGenerator, textPaster);
 
    	this.addFactory(new GimpyFactory(wgen, cwti));
    
    }  
  
} 

解决问题作此记录,希望会帮到以后的人

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值