weequ
2012-06-23 17:02:17
Edit: Scroll down for more recent version.

Do you think it's good enough or is there existing solvers that would solve that?
Features:
*Randomly move letters
*Randomly rotate letters
*Randomly shear letters
*Paint outline of letters
*Add randomly generated curve to connect letters
*Randomly add black and white lines and curves to letters.
code:
Do you think it's good enough or is there existing solvers that would solve that?
Features:
*Randomly move letters
*Randomly rotate letters
*Randomly shear letters
*Paint outline of letters
*Add randomly generated curve to connect letters
code:
package captchagenerator;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author Antti
*/
public class CaptchaGenerator extends JFrame{
/**
* @param args the command line arguments
*/
private BufferedImage img;
JPanel panel;
public CaptchaGenerator() {
setTitle("Captcha Generator");
//panel = new JPanel(new FlowLayout());
setLayout(new FlowLayout());
img = generateCaptcha("Captcha testing");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
JButton jbutton = new JButton("Generate Captcha");
add(jbutton);
pack();
setSize(500, 500);
jbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
img = generateCaptcha("Captcha testing");
repaint();
}
});
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawImage(img, 50, 100, null);
}
// public static BufferedImage generateLetter(char c, float angle) {
// Font f = new Font("arial", Font.BOLD, 30);
// FontRenderContext fc = new FontRenderContext(f.getTransform(), true, false);
// Rectangle2D stringBounds = f.getStringBounds(""+c, fc);
// BufferedImage img2 = new BufferedImage((int)stringBounds.getWidth(), (int)stringBounds.getHeight(), BufferedImage.TYPE_INT_RGB);
//
// return null;
// }
public static BufferedImage generateCaptcha(String text) {
Font f = new Font("arial", Font.PLAIN, 30);
FontRenderContext at = new FontRenderContext(f.getTransform(), true, false);
GlyphVector gv = f.createGlyphVector(at, text);
//Rotating letters
double currentAngle = 0;
for (int i = 0; i < gv.getNumGlyphs(); i++) {
Point2D glyphPosition = gv.getGlyphPosition(i);
glyphPosition.setLocation(glyphPosition.getX()-2+Math.random()*4, glyphPosition.getY()-2+Math.random()*4);
gv.setGlyphPosition(i, glyphPosition);
currentAngle+=Math.random()*4-2;
gv.setGlyphTransform(i, AffineTransform.getRotateInstance(Math.toRadians(currentAngle), gv.getVisualBounds().getMaxX()*0.5, gv.getVisualBounds().getMaxY()*0.5));
}
BufferedImage img2 = new BufferedImage((int)gv.getVisualBounds().getWidth()+1, (int)gv.getVisualBounds().getHeight()+1, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img2.createGraphics();
Shape s = gv.getOutline();
g.translate(-gv.getVisualBounds().getMinX(), -gv.getVisualBounds().getMinY());
g.setColor(Color.WHITE);
g.draw(s);
//Adding lines and curves to letters
int next = Math.round(Math.round((Math.random()*13)));
PathIterator pi = gv.getOutline().getPathIterator(null);
double[] coordinates = new double[6];
while (!pi.isDone()) {
int seg = pi.currentSegment(coordinates);
switch (seg) {
case PathIterator.SEG_CLOSE:
next--;
break;
case PathIterator.SEG_MOVETO:
next--;
break;
case PathIterator.SEG_LINETO:
next--;
break;
case PathIterator.SEG_QUADTO:
next -= 2;
break;
case PathIterator.SEG_CUBICTO:
next -= 3;
break;
}
if (next <= 0) {
int r = Math.round(Math.round(Math.random()*-next));
next = 4+(int)(Math.random()*8);
if (Math.random() < 0.5) {
g.setColor(Color.WHITE);
} else {
g.setColor(Color.BLACK);
}
switch (Math.round(Math.round(Math.random()*3))) {
case 1:
g.drawLine(Math.round(Math.round(coordinates[r])), Math.round(Math.round(coordinates[r+1])), Math.round(Math.round(coordinates[r]))-7+Math.round(Math.round(Math.random()*14)), Math.round(Math.round(coordinates[r+1]))-7+Math.round(Math.round(Math.random()*14)));
break;
case 2:
QuadCurve2D.Double qc = new QuadCurve2D.Double(coordinates[r], coordinates[r+1],
Math.round(Math.round(coordinates[r]))-7+Math.round(Math.round(Math.random()*14)), Math.round(Math.round(coordinates[r+1]))-7+Math.round(Math.round(Math.random()*14)),
Math.round(Math.round(coordinates[r]))-7+Math.round(Math.round(Math.random()*14)), Math.round(Math.round(coordinates[r+1]))-7+Math.round(Math.round(Math.random()*14)));
g.draw(qc);
break;
case 3:
CubicCurve2D cc = new CubicCurve2D.Double(coordinates[r], coordinates[r+1],
Math.round(Math.round(coordinates[r]))-7+Math.round(Math.round(Math.random()*14)), Math.round(Math.round(coordinates[r+1]))-7+Math.round(Math.round(Math.random()*14)),
Math.round(Math.round(coordinates[r]))-7+Math.round(Math.round(Math.random()*14)), Math.round(Math.round(coordinates[r+1]))-7+Math.round(Math.round(Math.random()*14)),
Math.round(Math.round(coordinates[r]))-7+Math.round(Math.round(Math.random()*14)), Math.round(Math.round(coordinates[r+1]))-7+Math.round(Math.round(Math.random()*14)));
g.draw(cc);
break;
}
}
pi.next();
}
//g.translate(gv.getVisualBounds().getMinX(), gv.getVisualBounds().getMinY());
img2.flush();
return img2;
}
public static void main(String[] args) {
CaptchaGenerator cg = new CaptchaGenerator();
}
}