以下是具体代码:
package com.jinzhi.tes2;
import java.awt.Color;
import java.awt.Font;import java.awt.Graphics;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;
//用Java的类来画简单的二维码
public class ImgDemo { public static void main(String[] args) { //和图片相关的类 Image 、ImageIO、 BufferedImage、 Icon、ImageIcon //验证码作用:登录:防止重复提交 //构造图片缓冲区-画框 BufferedImage bi = new BufferedImage(70, 35, BufferedImage.TYPE_INT_RGB); //得到画图的笔-画笔(从画框上得到画笔) Graphics2D g2 = (Graphics2D)bi.getGraphics(); //填充一个矩形 g2.setColor(Color.WHITE);// g2.fillOval(0, 0, 150, 70);//从0,0开始全部填充 设置背景为白色 g2.setFont(new Font("楷体", Font.BOLD, 18)); g2.setColor(Color.BLACK);//设置字体颜色 //向图片上写字符串 g2.drawString("mama", 15, 25); try { ImageIO.write(bi, "JPEG", new FileOutputStream("E:/a.jpg")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}