图片验证码
程序员文章站
2022-03-04 10:41:02
...
java画图:
package createImage;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
public class CreateImage {
/**
* 关于图片的类:Image、ImageIO、BufferedImage、Icon、ImageIcon
* @param args
* @throws Exception
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, Exception {
//获得图片缓冲区
BufferedImage bi = new BufferedImage(150, 70, BufferedImage.TYPE_INT_RGB);
//得到绘制环境
Graphics2D g2 = (Graphics2D)bi.getGraphics();
g2.setColor(Color.white);//设置颜色
g2.fillRect(0, 0, 150, 70);//填充整张图,相当于背景色
//相当于设置边框
g2.setColor(Color.RED);
g2.drawRect(0, 0, 150-1, 70-1);
g2.setFont(new Font("宋体", Font.BOLD, 18));//设置字体样式
g2.setColor(Color.BLACK);//设置字体颜色
g2.drawString("HelloWord", 3, 50);//在图片上写字符串
ImageIO.write(bi, "JPEG", new FileOutputStream("D:/word/a.jpg"));//把缓冲区里面的图片写入到指定文件下
}
}
上一篇: WSGI协议