1
2
3
4
5
6
7 package com.octo.captcha.image.fisheye;
8
9 import com.octo.captcha.image.ImageCaptcha;
10
11 import java.awt.*;
12 import java.awt.image.BufferedImage;
13 import java.util.StringTokenizer;
14
15 /***
16 * FishEye is an ImageCaptcha <ul> <li>Challenge type : image</li> <li>Response type : a point position, in pixels from
17 * the bottom left, can be : a String with two numbers separated with a comma or a java.awt.Point</li> <li>Description :
18 * An image of a distorded picture. User have to point the center of the deformation and to submit it.</li> </ul>
19 *
20 * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue</a>
21 * @version 1.0
22 */
23 public class FishEye extends ImageCaptcha {
24
25 private Point deformationCenter;
26 private Integer tolerance;
27
28 /***
29 * @param question the question
30 * @param challenge the imageChallenge
31 * @param deformationCenter the center of the deformation that has been applied to the image in order to validate
32 * the answer
33 * @param tolerance the max distance to the center of the deformation accepted by the validation routine in
34 * pixels.
35 */
36 protected FishEye(String question, BufferedImage challenge,
37 Point deformationCenter, Integer tolerance) {
38 super(question, challenge);
39 this.deformationCenter = deformationCenter;
40 this.tolerance = tolerance;
41 }
42
43 /***
44 * Validation routine for the response.
45 *
46 * @param response to the question concerning the chalenge
47 *
48 * @return true if the answer is correct, false otherwise.
49 */
50 public Boolean validateResponse(Object response) {
51
52 if (response instanceof Point) {
53 Point point = (Point) response;
54 return validateResponse(point);
55
56 } else if (response instanceof String) {
57 String s = (String) response;
58
59 try {
60
61
62 StringTokenizer token = new StringTokenizer(s, ",");
63
64 Point point = new Point(Integer.parseInt(token.nextToken()),
65 Integer.parseInt(token.nextToken()));
66 return validateResponse(point);
67 } catch (Throwable e) {
68
69 return Boolean.FALSE;
70 }
71 } else {
72 return Boolean.FALSE;
73 }
74
75 }
76
77 /***
78 * Real validation
79 *
80 * @param point the given point
81 *
82 * @return true if distance from the given point and the deformation center is less than tolerance, false otherwise
83 */
84 private Boolean validateResponse(Point point) {
85
86 if (point.distance(deformationCenter) <= tolerance.doubleValue()) {
87 return Boolean.TRUE;
88 }
89 return Boolean.FALSE;
90
91 }
92
93 }