Java类
目前只支持在windows上运行,因为linux上没有对应的字体,
使用默认字体,可以迁移到任意服务器。
class="java" name="code">
package com.xxx.catcha;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.imageio.ImageIO;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.util.Config;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) throws IOException
{
Properties props = new Properties();
//可选字符
props.put("kaptcha.textproducer.char.string", "0123456789一二三四五六七八九");
//验证码字体
props.put("kaptcha.textproducer.font.names", "华文彩云");
//验证码字数
props.put("kaptcha.textproducer.char.length", "4");
Config config = new Config(props);
Producer producer = (Producer)config.getProducerImpl();
String capText = producer.createText();
BufferedImage bi = producer.createImage(capText);
FileOutputStream out = new FileOutputStream("kaptcha.jpg");
ImageIO.write(bi, "jpg", out);
}
}
pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx</groupId>
<artifactId>catcha</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>catcha</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>kaptcha</artifactId>
<version>0.0.9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>