001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.commons.img;
020
021import java.awt.Font;
022import java.awt.image.BufferedImage;
023import java.io.IOException;
024import java.io.OutputStream;
025
026import javax.imageio.ImageIO;
027
028/**
029 * concrete captcha implementation
030 */
031public final class Captcha extends AbstractCaptcha {
032        
033        private static final char[] chars=new char[]{
034                 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
035                ,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
036                //,'0','1','2','3','4','5','6','7','8','9'
037        };
038
039        @Override
040        public Font getFont(String font, Font defaultValue) {
041                return Font.decode(font);
042        }
043
044        /**
045         * write out image object to a output stream
046         * @param image
047         * @param os
048         * @param format
049         * @throws IOException
050         */
051        public static void writeOut(BufferedImage image, OutputStream os, String format) throws IOException {
052                ImageIO.write(image, format, os);
053                
054        }
055
056        /**
057         * creates a random String in given length
058         * @param length length of the string to create
059         * @return
060         */
061        public static String randomString(int length) {
062                StringBuilder sb=new StringBuilder();
063                for(int i=0;i<length;i++) {
064                        sb.append(chars[AbstractCaptcha.rnd(0,chars.length-1)]);
065                }
066                return sb.toString();
067        }
068}