D
G
O
L

生成图片验证码

package cn.tx.demo1;

import com.sun.tools.javac.Main;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

public class imagecode {
    static String[] strs={
            "a","b","c","d","e","f","g","h","i","j","k",
            "m","n","p","q","r","s","t","u","v","w","x",
            "y","z","2","3","4","5","6","7","8","9",
    };

    public static void main(String[] args) throws IOException {
        int h=50,w=150;
        int typeIntRgb = BufferedImage.TYPE_INT_RGB;
        //typeIntRgb=1;
        BufferedImage image=new BufferedImage(w,h,typeIntRgb);//画板,最后生成图片
        //修改图片颜色
        //画笔改
        Graphics graphics = image.getGraphics();
        //填充矩形
        int x=25,y=25;
        graphics.setColor(Color.BLUE);
        graphics.fillRect(0,0,w,h);
        Random random = new Random();
        graphics.setColor(Color.green);
        graphics.setFont(new Font("楷体",Font.PLAIN,25));
        for (int i = 0; i < 4; i++,x+=20) {
            int num=random.nextInt(strs.length);
            String s = strs[num];
            graphics.drawString(s,x,y);

        }
        graphics.setColor(Color.BLACK);
        graphics.drawLine(20,25,130,35);
        ImageIO.write(image,"jpg", new File("F:\\tupian_yanzhengma\\imag\\huaban.jpg"));

    }
}

posted @ 2023-02-09 15:49  jinganglang567  阅读(40)  评论(0)    收藏  举报