Child pages
  • Implements JCaptcha Sound
Skip to end of metadata
Go to start of metadata





1 Comment

  1. The code below is working, It reads the resource bundle, sound_en_US.properties, and picks the words randomly for the audio captcha.

    Before you go please mail sure you have few additional classes in your jcaptcha classes latest release, Looks like jcaptcha has missed bundling these ones in their latest release, you can open the archive and put in the classes and get it bundled again, you should be able to find them from previous releases.

    Classes you need are
    com.octo.captcha.component.sound.soundconfigurator.FreeTTSSoundConfigurator;
    com.octo.captcha.component.sound.wordtosound.FreeTTSWordToSound;
    com.octo.captcha.sound.gimpy.GimpySoundFactory;

    Also, please make sure the FreeTTS jars and CMU jars which comes with the bundle are present.

    CaptchaServiceSingelton .java:
    ============================================================
    import java.awt.Color;

    import com.octo.captcha.CaptchaFactory;
    import com.octo.captcha.component.image.backgroundgenerator.FunkyBackgroundGenerator;
    import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
    import com.octo.captcha.component.image.textpaster.RandomTextPaster;
    import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;
    import com.octo.captcha.component.sound.soundconfigurator.FreeTTSSoundConfigurator;
    import com.octo.captcha.component.sound.wordtosound.FreeTTSWordToSound;
    import com.octo.captcha.component.word.FileDictionary;
    import com.octo.captcha.component.word.wordgenerator.DictionaryWordGenerator;
    import com.octo.captcha.component.word.wordgenerator.RandomWordGenerator;
    import com.octo.captcha.engine.GenericCaptchaEngine;
    import com.octo.captcha.image.gimpy.GimpyFactory;
    import com.octo.captcha.service.captchastore.FastHashMapCaptchaStore;
    import com.octo.captcha.service.multitype.GenericManageableCaptchaService;
    import com.octo.captcha.sound.gimpy.GimpySoundFactory;

    public class CaptchaServiceSingelton {

    private static GenericManageableCaptchaService instance;

    private static String voiceName = "kevin16";
    private static String voicePackage = "com.sun.speech.freetts.en.us.cmu_time_awb.AlanVoiceDirectory,com.sun.speech.freetts.en.us.cmu_us_kal .KevinVoiceDirectory";

    private static CaptchaFactory soundFactory[] = {
    new GimpySoundFactory(
    new DictionaryWordGenerator(new FileDictionary("sound")),
    new FreeTTSWordToSound(new FreeTTSSoundConfigurator(voiceName, voicePackage, 1.0f, 100, 100), 4, 10)
    )
    };

    private static CaptchaFactory characterFactory[] = {
    new GimpyFactory(
    new RandomWordGenerator("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
    new ComposedWordToImage(new RandomFontGenerator(new Integer(12), new Integer(16)),
    new FunkyBackgroundGenerator(new Integer(100), new Integer(50)),
    new RandomTextPaster(new Integer(6), new Integer(10), Color.BLACK))
    )
    };

    public static synchronized GenericManageableCaptchaService getInstance() {

    if(instance==null) {
    GenericCaptchaEngine engine = new GenericCaptchaEngine(characterFactory);
    instance = new GenericManageableCaptchaService(new FastHashMapCaptchaStore(), engine, 80, 100000, 75000);
    }

    return instance;

    }

    public static void setSoundEngine() {

    GenericCaptchaEngine engine = new GenericCaptchaEngine(soundFactory);
    instance.setCaptchaEngine(engine);
    }

    public static void setCharacterEngine() {

    GenericCaptchaEngine engine = new GenericCaptchaEngine(characterFactory);
    instance.setCaptchaEngine(engine);
    }

    ============================================================

    sound_en_US.properties
    ============================================================
    words=orange;apple;plus;exceed;holiday;human;hopefully;excellence;normally;
    ============================================================

    Main.java
    ============================================================
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.Locale;
    import java.util.Random;

    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;

    import org.apache.log4j.Logger;
    import com.octo.captcha.service.CaptchaServiceException;
    import com.octo.captcha.service.multitype.GenericManageableCaptchaService;
    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;

    public class Main {

    private static Logger logger = Logger.getLogger(Main.class);

    public static void main(String[] args)
    throws IllegalArgumentException, CaptchaServiceException, IOException, Exception {

    String captchaId = new Random().nextInt(99999) + "";

    byte bufCharacters[] = getCaptchaCharacters(captchaId, "en", "US");
    byte bufSound[] = getCaptchaSound(captchaId);

    }

    /** Captcha impl **/

    public static byte[] getCaptchaCharacters(String captchaId, String language, String country)
    throws IllegalArgumentException, CaptchaServiceException, IOException, Exception {

    System.setProperty("java.awt.headless", "true");

    byte[] captchaChallengeAsJpeg = null;
    ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();

    Locale locale = new Locale(language, country);

    GenericManageableCaptchaService captchaService = CaptchaServiceSingelton.getInstance();
    CaptchaServiceSingelton.setCharacterEngine();

    BufferedImage challengeCharcater = (BufferedImage) captchaService.getImageChallengeForID(captchaId, locale);

    JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(jpegOutputStream);
    jpegEncoder.encode(challengeCharcater);

    captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

    OutputStream outputStream = new FileOutputStream("captcha.jpg");
    outputStream.write(captchaChallengeAsJpeg);
    outputStream.close();

    System.setProperty("java.awt.headless", "false");

    return captchaChallengeAsJpeg;

    }

    /** Captcha impl **/

    public static byte[] getCaptchaSound(String captchaId)
    throws IllegalArgumentException, CaptchaServiceException, IOException, Exception {

    System.setProperty("java.awt.headless", "true");

    byte[] captchaChallengeAsAudio = null;

    Locale locale = new Locale("en", "US");

    GenericManageableCaptchaService captchaService = CaptchaServiceSingelton.getInstance();
    CaptchaServiceSingelton.setSoundEngine();

    AudioInputStream challangeAudio = captchaService.getSoundChallengeForID(captchaId);

    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    AudioSystem.write(challangeAudio, javax.sound.sampled.AudioFileFormat.Type.WAVE, byteOutputStream);

    captchaChallengeAsAudio = byteOutputStream.toByteArray();

    OutputStream outputStream = new FileOutputStream("captcha.wav");
    outputStream.write(captchaChallengeAsAudio);
    outputStream.close();

    System.setProperty("java.awt.headless", "false");

    return captchaChallengeAsAudio;

    }
    }
    ============================================================