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.runtime.img; 020 021import java.awt.Color; 022import java.awt.Font; 023import java.awt.image.BufferedImage; 024 025import lucee.commons.img.AbstractCaptcha; 026import lucee.commons.img.CaptchaException; 027import lucee.commons.lang.font.FontUtil; 028import lucee.runtime.exp.ExpressionException; 029import lucee.runtime.img.filter.MarbleFilter; 030 031public class MarpleCaptcha extends AbstractCaptcha { 032 033 public static final int DIFFICULTY_LOW=0; 034 public static final int DIFFICULTY_MEDIUM=1; 035 public static final int DIFFICULTY_HIGH=2; 036 037 public BufferedImage generate(String text,int width, int height, String[] fonts, boolean useAntiAlias, Color fontColor,int fontSize, int difficulty) throws CaptchaException { 038 MarbleFilter mf = new MarbleFilter(); 039 try { 040 mf.setEdgeAction("WRAP"); 041 } catch (ExpressionException e1) {} 042 mf.setAmount(0.1F); 043 BufferedImage src=super.generate(text, width, height, fonts, useAntiAlias, fontColor, fontSize, difficulty); 044 045 if(difficulty==DIFFICULTY_LOW) mf.setTurbulence(0.0f); 046 else if(difficulty==DIFFICULTY_MEDIUM) mf.setTurbulence(0.10f); 047 else mf.setTurbulence(0.2f); 048 049 try { 050 mf.setInterpolation("NEAREST_NEIGHBOUR"); 051 } catch (ExpressionException e) {} 052 053 BufferedImage dst = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 054 mf.filter(src, dst); 055 return dst; 056 } 057 058 public Font getFont(String font, Font defaultValue) { 059 return FontUtil.getFont(font,defaultValue); 060 } 061}