Package org.apdplat.module.security.service

Source Code of org.apdplat.module.security.service.CaptchaEngine

/**
*
* APDPlat - Application Product Development Platform
* Copyright (c) 2013, 杨尚川, yang-shangchuan@qq.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*/

package org.apdplat.module.security.service;

import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
import com.octo.captcha.component.image.color.RandomListColorGenerator;
import com.octo.captcha.component.image.deformation.ImageDeformation;
import com.octo.captcha.component.image.deformation.ImageDeformationByFilters;
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.TextDecorator;
import com.octo.captcha.component.image.wordtoimage.DeformedComposedWordToImage;
import com.octo.captcha.component.image.wordtoimage.WordToImage;
import com.octo.captcha.component.word.FileDictionary;
import com.octo.captcha.component.word.wordgenerator.ComposeDictionaryWordGenerator;
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.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.ImageFilter;
import org.springframework.stereotype.Service;

@Service
public class CaptchaEngine extends ListImageCaptchaEngine {

  @Override
  protected void buildInitialFactories() {
    int minWordLength = 4;
    int maxWordLength = 4;
    int fontSize = 30;
    final int imageWidth = 180;
    final int imageHeight = 50;

    //word generator
    WordGenerator dictionnaryWords = new ComposeDictionaryWordGenerator(new FileDictionary("toddlist"));

    //word2image components
    TextPaster randomPaster = new DecoratedRandomTextPaster(minWordLength, maxWordLength,
        new RandomListColorGenerator(new Color[] { new Color(23, 170, 27), new Color(220, 34, 11),
            new Color(23, 67, 172) }), new TextDecorator[] {});
    BackgroundGenerator background = new BackgroundGenerator(){
                    @Override
                    public int getImageHeight() {
                        return imageWidth;
                    }

                    @Override
                    public int getImageWidth() {
                        return imageHeight;
                    }

                    @Override
                    public BufferedImage getBackground() {
                        BufferedImage image = new BufferedImage(imageWidth, imageHeight,BufferedImage.TRANSLUCENT);
                        Graphics2D g2d = image.createGraphics();
                        image = g2d.getDeviceConfiguration().createCompatibleImage(imageWidth, imageHeight, Transparency.TRANSLUCENT);
                        g2d.dispose();
                        return image;
                    }
                   
                };
    FontGenerator font = new RandomFontGenerator(fontSize-5, fontSize, new Font[] {
        new Font("nyala", Font.BOLD, fontSize)});

    ImageDeformation postDef = new ImageDeformationByFilters(new ImageFilter[] {});
    ImageDeformation backDef = new ImageDeformationByFilters(new ImageFilter[] {});
    ImageDeformation textDef = new ImageDeformationByFilters(new ImageFilter[] {});

    WordToImage word2image = new DeformedComposedWordToImage(font, background, randomPaster, backDef, textDef,
        postDef);
    addFactory(new GimpyFactory(dictionnaryWords, word2image));
  }

}
TOP

Related Classes of org.apdplat.module.security.service.CaptchaEngine

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.