http://arturkustra.blogspot.kr/2012/07/how-to-get-captcha-as-image-in-java.html

How to get captcha as image in Java
Google's captcha is very powerfull tool to check if "other side" is human or not. It is very popular and commonly used method, mostly on web pages. But sometimes, we want to make something more secured i.e. when you don't want to delete database tables, or maybe when your app works as thin client and you don't want to let someone to POST without, you know what.. :)
To get started we have to get:
1. Private and public keys to the service - you can register in the link above.
2. Get recaptcha bindings for Java from here.
3. Finally run he code:

ReCaptcha c = ReCaptchaFactory.
    newReCaptcha("[ your publicKey here]", "[your privateKey here]", false);
        String text = c.createRecaptchaHtml(null, null);

        try {
            URL imageURL = new URL(getTextBetween(text, "src=\"", "\"><"));
            BufferedReader in = new BufferedReader(new InputStreamReader(imageURL.openStream()));

            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                int start = inputLine.indexOf("challenge");
                if (start > 0) {
                    int end = inputLine.indexOf("',");
                    String part = inputLine.substring(start + 13, end);

                    String imageUri = "http://www.google.com/recaptcha/api/image?c=" + part;

                    URL url = new URL(imageUri);
                    Image image = ImageIO.read(url);
                    jLabel1.setIcon(new ImageIcon(image));
                    jLabel1.validate();
                }
            }
            in.close();

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }

Of course you can use regex but this is just an example...
The helper function for finding text is:

private String getTextBetween(String text, String start, String end) {
    int startIdx = text.indexOf(start);
    int endIdx = text.indexOf(end, startIdx);

    return text.substring(startIdx + start.length(), endIdx);
}
And the result presents as follows:


Posted by [czar]
,