首页>代码>jQuery网页验证码插件>/demo/src/com/demo_image/demo1.java
package com.demo_image;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

public class demo1 {

	 // main方法,idea快捷键生成main
    public static void main(String[] args) throws IOException {
        // 宽度
        int width = 150;
        // 高度
        int height = 50;
        // 编写验证码的程序
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        // 目的向图片上画内容
        // 获取到画笔的对象
        Graphics g = image.getGraphics();
        // 给画笔设置颜色,设置灰色
        g.setColor(Color.gray);
        // 给矩形填充效果
        g.fillRect(0,0,width,height);

        // 画笔颜色的问题,重新设置一下
        g.setColor(Color.red);

        // =====================================================
        String word = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
        // 随机获取4个 Java基础 字符串 随机相关的操作  alt + 回城
        int x = 20;
        Random random = new Random();
        // 编写for循环,循环4次,生成for循环
        for (int i = 0; i < 4; i++) {
            // 每次从word字符串中获取到字符
            int index = random.nextInt(word.length());
            // 从字符串中获取字符串
            char c = word.charAt(index);
            // 把c写到图片上
            g.drawString(c+"",x,30);
            // 每循环一次,给x都加20
            x += 25;
        }

        // 把内存中的图片生成真实的图片
        ImageIO.write(image,"jpg",new File("E:\\demo\\abc.jpg"));
    }

}
最近下载更多
听着雨吹着风  LV9 2022年5月8日
dushuzhong  LV7 2022年5月3日
JohnAlex  LV9 2022年1月29日
g1121345342  LV9 2021年6月22日
helloMD  LV8 2020年11月16日
s779766816  LV10 2020年9月22日
G1ccccc1123  LV1 2020年1月8日
styone  LV3 2019年11月5日
gardenia111  LV16 2019年6月25日
mxboo123  LV8 2019年6月10日
最近浏览更多
YiRenHun  LV11 2023年12月5日
18276177062  LV1 2023年6月12日
快乐的小丸子 2022年12月25日
暂无贡献等级
835512308  LV14 2022年7月22日
Taoaqi  LV3 2022年6月10日
芮坎尚  LV1 2022年5月11日
听着雨吹着风  LV9 2022年5月8日
dushuzhong  LV7 2022年5月3日
Jeasonchan898  LV2 2022年4月26日
liuyanlin_sc 2022年4月11日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友