0

I'm trying to create a Captcha image in Java with a phrase or multiple words instead of a single random word.

I've tried with some projects like Cage, simplecaptcha and jcaptcha but all of them work only with one word.

I expect an output like the one in the image. multiple word captcha example

Do you know any other project that can do what I want? or a way to create my own custom images?

Thank you.

  • Why not just call one of the libraries multiple times word by word? – The incredible Jan Dec 12 '22 at 12:34
  • 2
    Also note: this place isn't intended for "here my requirements, now what". And asking for "some link to a library that does that" renders the question fully off topic . I suggest you turn to the [help] to learn how/what to ask here. – GhostCat Dec 12 '22 at 12:35
  • @TheincredibleJan that would generate 4 different CAPTCHA images instead of just one. – dani izquierdo Dec 12 '22 at 12:40
  • @dani izquierdo And why is this a problem? If I put 4 smaller images on a big image with the same backgroud is no difference as if I had just 1 image from the beginning. :) – The incredible Jan Dec 12 '22 at 12:50
  • You could use a `BufferedImage`. You'd have to come up with your own `Graphics2D` code to draw a text `String` wavy like in your picture. – Gilbert Le Blanc Dec 12 '22 at 16:20

1 Answers1

0

Thanks to @TheincredibleJan and @GilbertLeBlanc.

The solution I got was generating x images (one per word) and merge them into a big image. The code is below (change the numbers as you need depending on the size or type of your own images):

String frase = "i ate pizza last night";
int len = frase.replace(" ", "").length();

String[] words = frase.split(" ");
BufferedImage result = new BufferedImage((len * 25) + 10, 50, BufferedImage.TYPE_INT_ARGB);
Graphics g = result.getGraphics();
int x = 0;

for (int i = 0; i < words.length; i++) {
    CaptchaGenerator captchagen = new CaptchaGenerator();
    Captcha captcha = captchagen.createCaptcha(words[i].length() * 25 + 3, 50, words[i]);
    BufferedImage imagen = captcha.getImage();
    g.drawImage(imagen, x, 0, null);
    x += imagen.getWidth() - 5;
}

g.dispose();

try {
    File outputfile = new File("captchaResult.png");
    ImageIO.write(result, "png", outputfile);
} catch (Exception ex) {
}

And this is the generated image: CAPTCHA image result

Community
  • 1
  • 1