用Servlet来实现这种效果是非常简单的。看看下面的代码:
import java.awt.Color;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream; import javax.servlet.Servlet;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageDecoder;import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class CreateImage extends HttpServlet implements Servlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Cache-Control","no-store"); response.setContentType("image/jpeg"); ServletOutputStream out=response.getOutputStream(); String imagePath=request.getSession().getServletContext().getRealPath("images")+"\\"+getRandom()+".jpg"; InputStream imageIn = new FileInputStream(new File(imagePath)); JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn); BufferedImage image = decoder.decodeAsBufferedImage(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); } public String getRandom(){ int i1 = (int)(java.lang.Math.random()*10); int i2 = (int)(java.lang.Math.random()*10); return String.valueOf(i1)+String.valueOf(i2); }}
在Images目录下放入100个从00-99命名的jpg文件,然后你在页面中使用<image src="CreateImage">来调用,每次刷新就会出现不同的图片,当然偶尔也会出现相同的:)。其实这个原理很简单,将Servlet的类型设为图片型,然后每次随机以流的形式取出一副jpg文件,然后再输出出去。一般论坛上面使用的也都是这种类型的,不信你把把这副图片的网络地址以插入图片的形式加到你的贴子里试试。
当然有时我们可能会想在图片上动态加入一些文字,那应该怎么,其实也很简单,就是利用Graphics这个类来对图片内容进行修改,如下:
import java.awt.Color;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;
import javax.servlet.Servlet;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageDecoder;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class CreateImage extends HttpServlet implements Servlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Cache-Control","no-store"); response.setContentType("image/jpeg"); ServletOutputStream out=response.getOutputStream(); String imagePath=request.getSession().getServletContext().getRealPath("images")+"\\01.jpg"; InputStream imageIn = new FileInputStream(new File(imagePath)); JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn); BufferedImage image = decoder.decodeAsBufferedImage(); Graphics g = image.getGraphics(); g.setColor(new Color(255,0,0)); //更改输出字体的颜色 g.setFont(new Font("Default",Font.BOLD,30)); //更改字体大小 g.drawString("Hello",30,100); //在图片上坐标0,13的位置上输出"Hello everyone!" JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); } |