使用patchca生成验证码

本文详细介绍了如何自定义验证码的生成与配置过程,包括颜色、字体、字符选择、背景、滤镜、文字渲染和图片大小等关键参数的设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class ValidationCode {
	
	private ColorFactory colorFactory = null;  
    private RandomFontFactory fontFactory = null;  
    private RandomWordFactory wordFactory = null;  
    private TextRenderer textRenderer = null;  
    
    public ConfigurableCaptchaService getConfigurableCaptchaService(){  
    	ConfigurableCaptchaService configurableCaptchaService  = new ConfigurableCaptchaService();  
        // 颜色创建工厂,使用一定范围内的随机色  
        colorFactory = new RandomColorFactory();  
        configurableCaptchaService.setColorFactory(colorFactory);  
        // 随机字体生成器  
        fontFactory = new RandomFontFactory();  
        fontFactory.setMaxSize(32);  
        fontFactory.setMinSize(28);  
        configurableCaptchaService.setFontFactory(fontFactory); 
        // 随机字符生成器,去除掉容易混淆的字母和数字,如o和0等  
        wordFactory = new RandomWordFactory();  
        wordFactory.setCharacters("abcdefghkmnpqstwxyz23456789");  
        wordFactory.setMaxLength(5);  
        wordFactory.setMinLength(4);  
        configurableCaptchaService.setWordFactory(wordFactory);  
        // 自定义验证码图片背景  
        MyCustomBackgroundFactory backgroundFactory = new MyCustomBackgroundFactory();  
        configurableCaptchaService.setBackgroundFactory(backgroundFactory);  
        // 图片滤镜设置  
        ConfigurableFilterFactory filterFactory = new ConfigurableFilterFactory();  
        List<BufferedImageOp> filters = new ArrayList<BufferedImageOp>();  
        WobbleImageOp wobbleImageOp = new WobbleImageOp();  
        wobbleImageOp.setEdgeMode(AbstractImageOp.EDGE_MIRROR);  
        wobbleImageOp.setxAmplitude(2.0);  
        wobbleImageOp.setyAmplitude(1.0);  
        filters.add(wobbleImageOp);  
        filterFactory.setFilters(filters);  
        configurableCaptchaService.setFilterFactory(filterFactory);  
        // 文字渲染器设置  
        textRenderer = new BestFitTextRenderer();  
        textRenderer.setBottomMargin(3);  
        textRenderer.setTopMargin(3);  
        configurableCaptchaService.setTextRenderer(textRenderer);  
        // 验证码图片的大小  
        configurableCaptchaService.setWidth(82);  
        configurableCaptchaService.setHeight(32);  
        return configurableCaptchaService;
    }  
    /** 
     * 自定义验证码图片背景,主要画一些噪点和干扰线 
     */  
    private class MyCustomBackgroundFactory implements BackgroundFactory {  
        private Random random = new Random();  
        public void fillBackground(BufferedImage image) {  
            Graphics graphics = image.getGraphics();  
            // 验证码图片的宽高  
            int imgWidth = image.getWidth();  
            int imgHeight = image.getHeight();  
            // 填充为白色背景  
            graphics.setColor(Color.WHITE);  
            graphics.fillRect(0, 0, imgWidth, imgHeight);  
            // 画100个噪点(颜色及位置随机)  
            for(int i = 0; i < 100; i++) {  
                // 随机颜色  
                int rInt = random.nextInt(255);  
                int gInt = random.nextInt(255);  
                int bInt = random.nextInt(255);  
                graphics.setColor(new Color(rInt, gInt, bInt)); 
                // 随机位置  
                int xInt = random.nextInt(imgWidth - 3);  
                int yInt = random.nextInt(imgHeight - 2);  
                // 随机旋转角度  
                int sAngleInt = random.nextInt(360); 
                int eAngleInt = random.nextInt(360); 
                // 随机大小  
                int wInt = random.nextInt(6);  
                int hInt = random.nextInt(6); 
                graphics.fillArc(xInt, yInt, wInt, hInt, sAngleInt, eAngleInt);  
                // 画5条干扰线  
                if (i % 20 == 0) {  
                    int xInt2 = random.nextInt(imgWidth);  
                    int yInt2 = random.nextInt(imgHeight);  
                    graphics.drawLine(xInt, yInt, xInt2, yInt2);  
                }  
            }  
        }
    }
    
    public static void main(String[] args) {
    	ValidationCode validationCode = new ValidationCode();
    	
    	// 得到验证码对象,有验证码图片和验证码字符串 
    	Captcha captcha = validationCode.getConfigurableCaptchaService().getCaptcha();
        // 取得验证码字符串  
        String validationCodeTest = captcha.getChallenge();
        // 取得验证码图片并输出  
        BufferedImage bufferedImage = captcha.getImage(); 
    	System.out.println("验证码字符串:"+validationCodeTest);
    	String code = "code.jpg";
    	try {
			ImageIO.write(bufferedImage, "jpg", new File("D:/img/"+code));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
	}
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值