import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class CreateImag {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
testKaptcha();
}
/**
* @deprecated : 调用谷歌kaptcha生成验证码
* maven依赖
* <dependency>
* <groupId>com.google.code.kaptcha</groupId>
* <artifactId>kaptcha</artifactId>
* <version>2.3</version>
* </dependency>
*/
private static void testKaptcha() throws IOException {
//配置相关详细
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
Properties properties = new Properties();
properties.setProperty("kaptcha.border", "yes");
properties.setProperty("kaptcha.border.color", "105,179,90");
properties.setProperty("kaptcha.textproducer.font.color", "blue");
properties.setProperty("kaptcha.image.width", "200");
properties.setProperty("kaptcha.image.height", "100");
properties.setProperty("kaptcha.session.key", "code");
properties.setProperty("kaptcha.textproducer.char.length", "4");
properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
BufferedImage image = defaultKaptcha.createImage("456 + 779 =");
ImageIO.write(image, "png", new FileOutputStream(new File("d:/1.png")));
}
/**
* 手动调用awt绘制图形
*
* @throws IOException
*/
private static void handAnimation() throws IOException {
//创建一个BufferedImage图片缓冲区对象并指定它宽高和类型,这样相当于创建一个画板,然后可以在上面画画
BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
//输出磁盘位置
String type = "png";
File file = new File("D:/awttest." + type);
//拿到画笔
Graphics graphics = image.getGraphics();
//画一个图形系统默认是白色
graphics.fillRect(0, 5, 200, 100);
//设置画笔颜色
graphics.setColor(new Color(12, 123, 88));
//设置画笔画出的字体风格
graphics.setFont(new Font("隶书", Font.ITALIC, 30));
//写一个字符串
graphics.drawString("我是IO流图片", 10, 100);
//释放画笔
graphics.dispose();
FileOutputStream output = new FileOutputStream(file);
boolean write = ImageIO.write(image, type, output);
output.close();
}
}
02-08
4476