001: // ymageCaptcha.java
002: // -----------------------
003: // part of YaCy
004: // (C) by Marc Nause
005: //
006: // $LastChangedDate: 2007-04-03 22:56:00 +0100 (Di, 04 Apr 2007) $
007: // $LastChangedRevision: 3542 $
008: // $LastChangedBy: low012 $
009: //
010: // This program is free software; you can redistribute it and/or modify
011: // it under the terms of the GNU General Public License as published by
012: // the Free Software Foundation; either version 2 of the License, or
013: // (at your option) any later version.
014: //
015: // This program is distributed in the hope that it will be useful,
016: // but WITHOUT ANY WARRANTY; without even the implied warranty of
017: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018: // GNU General Public License for more details.
019: //
020: // You should have received a copy of the GNU General Public License
021: // along with this program; if not, write to the Free Software
022: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
023: //
024: // Using this software in any meaning (reading, learning, copying, compiling,
025: // running) means that you agree that the Author(s) is (are) not responsible
026: // for cost, loss of data or any harm that may be caused directly or indirectly
027: // by usage of this softare or this documentation. The usage of this software
028: // is on your own risk. The installation and usage (starting/running) of this
029: // software may allow other people or application to access your computer and
030: // any attached devices and is highly dependent on the configuration of the
031: // software which must be done by the user of the software; the author(s) is
032: // (are) also not responsible for proper configuration and usage of the
033: // software, even if provoked by documentation provided together with
034: // the software.
035: //
036: // Any changes to this file according to the GPL as documented in the file
037: // gpl.txt aside this file in the shipment you received can be done to the
038: // lines that follows this copyright notice here, but changes must not be
039: // done inside the copyright notive above. A re-distribution must contain
040: // the intact and unchanged copyright notice.
041: // Contributions and changes to the program code must be marked as such.
042:
043: package de.anomic.ymage;
044:
045: import java.util.Random;
046: import javax.imageio.ImageIO;
047:
048: public class ymageCaptcha extends ymageMatrix {
049:
050: public ymageCaptcha(int width, int height, byte displayMode,
051: String code) {
052: super (width, height, displayMode, "FFFFFF");
053: this .create(code);
054: }
055:
056: private void create(String code) {
057:
058: Random random = new Random();
059:
060: int width = this .getWidth();
061: int height = this .getHeight();
062: int chars = code.length();
063:
064: int x;
065: int y;
066: int ub = 0;
067: int widthPerChar = width / chars;
068: int pixels = width * height;
069:
070: //printing code
071: for (int i = 0; i < chars; i++) {
072: y = random.nextInt(height / 2) + (height / 4);
073: setColor(((random.nextInt(128) + 64) << 16)
074: + ((random.nextInt(128) + 64) << 8)
075: + random.nextInt(128) + 64);
076: ymageToolPrint.print(this , widthPerChar * i
077: + random.nextInt(widthPerChar / 2), y, 0, code
078: .substring(i, i + 1), -1);
079: }
080:
081: //adding some noise
082:
083: //random pixels
084: ub = pixels / 100;
085: for (int i = 0; i < ub; i++) {
086: setColor(((random.nextInt(128) + 64) << 16)
087: + ((random.nextInt(128) + 64) << 8)
088: + random.nextInt(128) + 64);
089: x = random.nextInt(width);
090: y = random.nextInt(height);
091: plot(x, y);
092: }
093:
094: //random lines
095: ub = pixels / 1000;
096: for (int i = 0; i < ub; i++) {
097: setColor(((random.nextInt(128) + 64) << 16)
098: + ((random.nextInt(128) + 64) << 8)
099: + random.nextInt(128) + 64);
100: x = random.nextInt(width);
101: y = random.nextInt(height);
102: line(x, y, x + random.nextInt(5), y + random.nextInt(5));
103: }
104:
105: }
106:
107: public static void main(String[] args) {
108: // go into headless awt mode
109: System.setProperty("java.awt.headless", "true");
110:
111: ymageCaptcha m = new ymageCaptcha(200, 70,
112: ymageMatrix.MODE_REPLACE, args[1]);
113: try {
114: ImageIO.write(m.getImage(), "png",
115: new java.io.File(args[0]));
116: } catch (java.io.IOException e) {
117: e.printStackTrace();
118: }
119:
120: }
121:
122: }
|