// putImg()方法里的参数:_str就是传进来的参数;
import com.sun.image.codec.jpeg.*;
import java.io.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.text.*;
import java.util.*;public class ValidateImg {
String str = "";
BufferedImage image = null;
    Graphics2D g = null;
    FileOutputStream output = null;
    JPEGImageEncoder encoder = null;
    JPEGEncodeParam jep = null;
    
    String strPath = "";
    
public ValidateImg()
{
image =new BufferedImage(60, 15, BufferedImage.TYPE_INT_RGB);
        g = image.createGraphics();
        g.setBackground(new Color(0xEFEFEF));
        g.clearRect(0, 0, 60, 15);
        g.setColor(Color.BLUE);
}

public void putImg(String _path, String _str) throws FileNotFoundException, IOException
{
strPath = _path;
Font font = new Font("黑体",Font.PLAIN,18);
        g.setFont(font);
        g.drawString(_str, 1, 14);        
output = new FileOutputStream (_path);
        encoder = JPEGCodec.createJPEGEncoder(output);
        jep = JPEGCodec.getDefaultJPEGEncodeParam(image);
        jep.setQuality(1.0F, true);
        encoder.encode(image,jep);
        if(output != null)
{
output.close();
}
System.out.println();
        System.out.println("written " + _path);        
    }
    
    public void delImg(String _path) throws FileNotFoundException, IOException
    {
     try
     {
     _path = _path.toString();
File f = new File(_path);
f.delete();
     System.out.println("deleted " + _path);
    }catch(SecurityException se){
     System.out.println(se.toString());
    }
    }
     

protected void finalize() throws FileNotFoundException, IOException
{
if(output != null)
{
output.close();
}
delImg(strPath);
}
}