myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lu4...@apache.org
Subject svn commit: r638114 - /myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAImageGenerator.java
Date Mon, 17 Mar 2008 22:46:00 GMT
Author: lu4242
Date: Mon Mar 17 15:45:56 2008
New Revision: 638114

URL: http://svn.apache.org/viewvc?rev=638114&view=rev
Log:
fix TOMAHAWK-1212 CAPTCHA component - Enhancement #1 (thanks to Hazem Saleh)

Modified:
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAImageGenerator.java

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAImageGenerator.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAImageGenerator.java?rev=638114&r1=638113&r2=638114&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAImageGenerator.java
(original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/captcha/util/CAPTCHAImageGenerator.java
Mon Mar 17 15:45:56 2008
@@ -19,6 +19,7 @@
 package org.apache.myfaces.custom.captcha.util;
 import java.awt.Color;
 import java.awt.Font;
+import java.awt.GradientPaint;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.RenderingHints;
@@ -34,24 +35,26 @@
 
 
 /**
- * This class is responsible for generating the 
+ * This class is responsible for generating the
  * CAPTCHA image.
  */
 public class CAPTCHAImageGenerator {
 
 	private static final int CAPTCHA_WIDTH = 290;
-	private static final int CAPTCHA_HEIGHT = 81;	
+	private static final int CAPTCHA_HEIGHT = 81;
 	private static final double PI = 3.1415926535897932384626433832795;
-	private static final int TEXT_X_COORDINATE = 50;	
+	private static final int TEXT_X_COORDINATE = 50;
 	private static final int TEXT_Y_COORDINATE = 60;
-	
+	private static final Color startingColor = new Color(150, 50, 150);
+	private static final Color endingColor = new Color(255, 255, 255);
+
 	/*
 	 * A helper method to draw the captcha text on the generated image.
 	 */
 	private void drawTextOnImage(Graphics2D graphics, String captchaText) {
-		
-		
-		
+
+
+
 		Font font = null;
 		TextLayout textLayout = null;
 		double currentFontStatus = Math.random();
@@ -62,7 +65,7 @@
 		} else {
 			font = new Font("Arial", Font.BOLD, 60);
 		}
-		
+
 		graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
 				RenderingHints.VALUE_ANTIALIAS_ON);
 		textLayout = new TextLayout(captchaText, font, graphics
@@ -70,72 +73,84 @@
 
 		textLayout.draw(graphics, TEXT_X_COORDINATE, TEXT_Y_COORDINATE);
 	}
-	
+
 	/*
 	 * A helper method to apply noise on the generated image.
 	 */
 	private void applyNoiseOnImage(Graphics2D graphics, int bufferedImageWidth,
 			int bufferedImageHeight) {
-		
+
 		// Applying shear.
 		applyShear(graphics, bufferedImageWidth, bufferedImageHeight);
 
 		// Drawing a broken line on the image.
 		drawBrokenLineOnImage(graphics);
 	}
-	
+
+	/*
+	 * This helper method is used for applying current gradient paint
+	 * to the Graphics2D object.
+	 */
+	private static void applyCurrentGradientPaint(Graphics2D graphics,
+			int width, int height) {
+
+		GradientPaint gradientPaint = new GradientPaint(0, 0, startingColor,
+				width, height, endingColor);
+
+		graphics.setPaint(gradientPaint);
+	}
+
 	/**
 	 * This method generates the CAPTCHA image.
-	 * @param response 
+	 * @param response
 	 * @param captchaText
 	 * @throws IOException
 	 */
 	public void generateImage(HttpServletResponse response, String captchaText)
 			throws IOException {
-		
-		int bufferedImageWidth = 0;
-		int bufferedImageHeight = 0;	
+
 		BufferedImage bufferedImage = null;
 		Graphics2D graphics = null;
 		PNGEncodeParam param = null;
 		PNGImageEncoder captchaPNGImage = null;
-		
-		
+
+
 		// Create the CAPTCHA Image.
 		bufferedImage = new BufferedImage(CAPTCHA_WIDTH,
 				CAPTCHA_HEIGHT, BufferedImage.TYPE_BYTE_INDEXED);
 
+		// Setup the graphics object.
 		graphics = bufferedImage.createGraphics();
 
+		applyCurrentGradientPaint(graphics, bufferedImage.getWidth(),
+				bufferedImage.getHeight());
+
 		graphics.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage
 				.getHeight());
 
 		graphics.setColor(Color.black);
 
-		bufferedImageWidth = bufferedImage.getWidth();
-		bufferedImageHeight = bufferedImage.getHeight();
-
 		// Draw text on the CAPTCHA image.
 		drawTextOnImage(graphics, captchaText);
 
 		// Apply noise on the CAPTCHA image.
-		applyNoiseOnImage(graphics, bufferedImageWidth, bufferedImageHeight);
-		
+		applyNoiseOnImage(graphics, bufferedImage.getWidth(), bufferedImage.getHeight());
+
 		// Draw the image border.
-		drawBorders(graphics, bufferedImageWidth, bufferedImageHeight);
-		
+		drawBorders(graphics, bufferedImage.getWidth(), bufferedImage.getHeight());
+
 		// Set the reponse content type to jpeg.
 		response.setContentType("image/jpg");
-		
-		
+
+
 		param = PNGEncodeParam
 		.getDefaultEncodeParam(bufferedImage);
 		captchaPNGImage = new PNGImageEncoder(response
 		.getOutputStream(), param);
-		
+
 		captchaPNGImage.encode(bufferedImage);
 	}
-	
+
 	/*
 	 * This helper method is used for drawing a thick line on the image.
 	 */
@@ -147,11 +162,11 @@
 		int xPoints[] = new int[4];
 		int yPoints[] = new int[4];
 		int thickness = 2;
-		
+
 		double lineLength = Math.sqrt(dX * dX + dY * dY);
 		double scale = (double) (thickness) / (2 * lineLength);
 		double ddx = -scale * (double) dY;
-		double ddy = scale * (double) dX;		
+		double ddy = scale * (double) dX;
 
 		graphics.setColor(Color.black);
 
@@ -171,25 +186,25 @@
 
 		graphics.fillPolygon(xPoints, yPoints, 4);
 	}
-		
+
 	/*
 	 * This helper method is used for drawing a broken line on the image.
 	 */
-	private void drawBrokenLineOnImage(Graphics graphics) {
+	private void drawBrokenLineOnImage(Graphics2D graphics) {
 
 		int yPoint1 = 0;
 		int yPoint2 = 0;
-		int yPoint3 = 0;		
+		int yPoint3 = 0;
 		int yPoint4 = 0;
 		int yPoint5 = 0;
 		Random random = new Random();
-		
+
 		// Random Y Points.
 		yPoint1 = random.nextInt(CAPTCHA_HEIGHT);
 		yPoint2 = random.nextInt(CAPTCHA_HEIGHT);
-		yPoint3 = CAPTCHA_HEIGHT / 2;		
+		yPoint3 = CAPTCHA_HEIGHT / 2;
 		yPoint4 = random.nextInt(CAPTCHA_HEIGHT);
-		yPoint5 = random.nextInt(CAPTCHA_HEIGHT);		
+		yPoint5 = random.nextInt(CAPTCHA_HEIGHT);
 
 		// Draw the random broken line.
 		drawThickLineOnImage(graphics, 0, yPoint1, CAPTCHA_WIDTH / 4, yPoint2);
@@ -200,21 +215,21 @@
 		drawThickLineOnImage(graphics, 3 * CAPTCHA_WIDTH / 4, yPoint4,
 				CAPTCHA_WIDTH, yPoint5);
 	}
-	
+
 	/*
-	 * This helper method is used for calculating the delta of the shearing 
+	 * This helper method is used for calculating the delta of the shearing
 	 * equation.
 	 */
 	private double getDelta(int period, double i, double phase, double frames) {
 		return (double) (period / 2)
 				* Math.sin((double) i / (double) period
 						+ (2 * PI * (double) phase) / (double) frames);
-	}	
+	}
 
 	/*
 	 * This helper method is used for applying shear on the image.
 	 */
-	private void applyShear(Graphics graphics, int bufferedImageWidth,
+	private void applyShear(Graphics2D graphics, int bufferedImageWidth,
 			int bufferedImageHeight) {
 
 		int periodValue = 20;
@@ -223,7 +238,8 @@
 		double deltaX = 0;
 		double deltaY = 0;
 
-		graphics.setColor(Color.WHITE);
+		applyCurrentGradientPaint(graphics, bufferedImageWidth,
+				bufferedImageHeight);
 
 		for (int i = 0; i < bufferedImageWidth; ++i) {
 			deltaX = getDelta(periodValue, i, phaseNumber, numberOfFrames);
@@ -241,17 +257,17 @@
 					bufferedImageWidth, i);
 		}
 	}
-	
+
 	/*
 	 * This helper method is used for drawing the borders the image.
 	 */
 	private void drawBorders(Graphics2D graphics, int width, int height) {
 		graphics.setColor(Color.black);
-		
+
 		graphics.drawLine(0, 0, 0, width - 1);
-		graphics.drawLine(0, 0, width - 1, 0);		
+		graphics.drawLine(0, 0, width - 1, 0);
 		graphics.drawLine(0, height - 1, width, height - 1);
 		graphics.drawLine(width - 1, height - 1, width - 1, 0);
-	}		
-	
+	}
+
 }



Mime
View raw message