验证码图片的实现

验证码图片实现主要用到java中的类 BufferedImage和Graphics类。


BufferedImage 子类描述具有可访问图像数据缓冲区的 Image。
构造方法:
public BufferedImage(int width, int height,int imageType,IndexColorModel cm)
构造一个类型为预定义图像类型之一的BufferedImage:TYPE_BYTE_BINARY 或 TYPE_BYTE_INDEXED

TYPE_INT_RGB:
public static final int TYPE_INT_RGB表示一个图像,它具有合成整数像素的 8 位 RGB 颜色分量。该图像具有不带 alpha 的 DirectColorModel。当具有透明 alpha 的数据存储在此类型的图像中时,必须将颜色数据调整为非预乘形式并丢弃 alpha,如 AlphaComposite 文档中的描述。

方法:
getGraphics
public Graphics getGraphics()此方法返回 Graphics2D,但此处是出于向后兼容性的考虑。createGraphics 更为方便,因为它被声明为返回 Graphics2D。


类 Graphics
Graphics 类是所有图形上下文的抽象基类,允许应用程序在组件(已经在各种设备上实现)以及闭屏图像上进行绘制。
方法:
drawRect:
public void drawRect(int x, int y,int width, int height)
绘制指定矩形的边框。矩形的左边缘和右边缘分别位于 x 和 x + width。上边缘和下边缘分别位于 y 和 y + height。使用图形上下文的当前颜色绘制该矩形。
参数:
x - 要绘制矩形的 x 坐标。
y - 要绘制矩形的 y 坐标。
width - 要绘制矩形的宽度。
height - 要绘制矩形的高度。

fillOval:
public abstract void fillOval(int x, int y, int width,int height)
使用当前颜色填充外接指定矩形框的椭圆。
参数:
x - 要填充椭圆的左上角的 x 坐标。
y - 要填充椭圆的左上角的 y 坐标。
width - 要填充椭圆的宽度。
height - 要填充椭圆的高度。


类 ImageIO
该类包含一些用来查找 ImageReader 和 ImageWriter 以及执行简单编码和解码的静态便捷方法。
方法
write
public static boolean write(RenderedImage im, String formatName, File output)throws IOException使用支持给定格式的任意 ImageWriter 将一个图像写入 File。如果已经有一个 File 存在,则丢弃其内容。
参数:
im - 要写入的 RenderedImage。
formatName - 包含格式非正式名称的 String。
output - 将在其中写入数据的 File。



简单的演示

package com.img;
/*
 * 实现验证码图片
 */
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

public class MyImg {
    private String valueCode;// 验证码图片上的字符串
    private int codeLength = 4;// 产生数字的长度
    private int width = codeLength * 20;// 验证码图片的宽度
    private int height = 20;// 验证码图片的高度

    /**
     * 画验证码图片
     * @return
     */
    public BufferedImage getImage() {
        valueCode = randomString();// 调用获取字符串的方法
        Random r = new Random();
        BufferedImage bi = null;
        bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        // 调用Graphics类提供基本绘图方法
        Graphics g = bi.getGraphics();

        // 填充指定的矩形,x - 要填充矩形的 x 坐标。y - 要填充矩形的 y 坐标。width - 要填充矩形的宽度。height - 要填充矩形的高度。
        g.fillRect(0, 0, width, height);

        // 设置字体类型,大小
        g.setFont(new Font("Times New Raman", Font.ITALIC, 18));

        // 画点
        int x, y;
        for (int i = 0; i < 180; i++) {// 画180个干扰点
            // 设置点的随机颜色,rgb格式
            g.setColor(new Color(r.nextInt(225), r.nextInt(225), r.nextInt(225)));
            // 设置干扰点所在的区域
            x = r.nextInt(width);
            y = r.nextInt(height);

            // 画点,点的大小一个像素
            g.drawOval(x, y, 1, 1);
        }

        // 把随机产生的字符串画到图片上
        for (int i = 0; i < valueCode.length(); i++) {
            g.setColor(new Color(r.nextInt(180), r.nextInt(180), r.nextInt(180)));
            g.drawString(valueCode.substring(i, i + 1), i * 20 + 6, 16);
        }
        // 释放资源
        g.dispose();

        return bi;
    }

    /***
     * 随机产生需要长度的字符串
     * 
     * @return
     */
    private String randomString() {
        String s = "";
        for (int i = 0; i < codeLength; i++) {
            s += randomChar();
        }
        return s;
    }

    /**
     * 随机生成字符(数字和大小写字母)
     * 
     * @return
     */
    private char randomChar() {
        char c = '0';
        Random r = new Random();
        int t = r.nextInt(62);
        if (t < 10) {
            c = (char) (t + '0');
        } else if (t < 36) {
            c = (char) (t - 10 + 'A');
        } else {
            c = (char) (t - 36 + 'a');
        }
        return c;
    }

    public static void main(String[] args) throws Exception {
        MyImg mi = new MyImg();
        OutputStream os = new FileOutputStream("d:\xx.jpg");
        //ImageIO流
        ImageIO.write(mi.getImage(), "JPEG", os);
        System.out.println("图片生成...");
    }
}

这里写图片描述

原文地址:https://www.cnblogs.com/wangqilong/p/9417548.html