Skip to content

Instantly share code, notes, and snippets.

@giautm
Last active December 27, 2021 23:43
Show Gist options
  • Save giautm/4952205473bb1a94581290afa546deda to your computer and use it in GitHub Desktop.
Save giautm/4952205473bb1a94581290afa546deda to your computer and use it in GitHub Desktop.
Generate captcha for train
*.class
/labeled-raw
/labeled-raw-verify
javac -cp "./simplecaptcha-1.2.jar" GenerateCaptcha.java
java -cp "./simplecaptcha-1.2.jar:./" GenerateCaptcha 2000 ./labeled-raw-verify
java -cp "./simplecaptcha-1.2.jar:./" GenerateCaptcha 10000 ./labeled-raw
import java.io.File;
import javax.imageio.ImageIO;
import nl.captcha.Captcha;
import nl.captcha.gimpy.FishEyeGimpyRenderer;
import nl.captcha.backgrounds.TransparentBackgroundProducer;
public class GenerateCaptcha {
public static void main(String[] args) {
int count = args.length > 0 ? Integer.parseInt(args[0]) : 10;
String outputDirPath = args.length > 1 ? args[1] : "./labeled-raw";
System.out.printf("Generate %d captcha(s) to directory \"%s\"\n", count, outputDirPath);
File outputDir = new File(outputDirPath);
if (!outputDir.exists()) {
outputDir.mkdirs();
System.out.printf("Created directory \"%s\"\n", outputDirPath);
}
for (int i = 0; i < count; i++) {
try {
Captcha captcha = new Captcha.Builder(130, 50)
.addText()
.addBorder()
.addBackground(new TransparentBackgroundProducer())
.gimp(new FishEyeGimpyRenderer())
.build();
String ans = captcha.getAnswer();
String filename = String.format("%s--%d.png", ans, System.currentTimeMillis());
ImageIO.write(captcha.getImage(), "png", new File(outputDir, filename));
System.out.printf("Captcha \"%s\", #%d\n", ans, i);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("Completed!");
}
}
This file has been truncated, but you can view the full file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment