图片验证码Kaptcha 及 手绘图形(AWT)入门

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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值