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));
    
    }  
  
} 

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

你可能感兴趣的:(JavaEE)