Last active
December 27, 2021 23:43
-
-
Save giautm/4952205473bb1a94581290afa546deda to your computer and use it in GitHub Desktop.
Generate captcha for train
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.class | |
/labeled-raw | |
/labeled-raw-verify |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment