`

java自动生成图片

 
阅读更多
 

[转载]
  在Web应用中,经常需要动态生成图片,比如实时股市行情,各种统计图等等,这种情况下,图片只能在服务器内存中动态生成并发送给用户,然后在浏览器中显示出来。

  本质上,浏览器向服务器请求静态图片如jpeg时,服务器返回的仍然是标准的http响应,只不过http头的contentType不是text/html,而是image/jpeg而已,因此,我们在Servlet中只要设置好contentType,然后发送图像的数据流,浏览器就能正确解析并显示出图片。

  在Java中,java.awt和java.awt.image包提供了基本的绘制图像的能力,我们可以在内存中绘制好需要的图形,然后编码成jpeg或其他图像格式,最后发送相应给浏览器即可。下面是使用Servlet动态创建图像的详细步骤:

  1.创建BufferedImage对象,该对象存在内存中,负责保存绘制的图像;

  2.创建Graphics2D对象,该对象负责绘制所需的图像;

  3.当绘制完成后,调用com.sun.image.codec.jpeg包的JPEG编码器对其编码;

  4.最后将编码后的数据输出至HttpResponse即可。

  注意com.sun.image.codec.jpeg包位于JDK目录的rt.jar包中,它不是公开的API,需要将rt.jar复制到web应用程序的WEB-INF/lib下。

  我们先创建一个最简单的Servlet:

public class CreateImageServlet extends HttpServlet {
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException
 {
  response.setContentType("image/jpeg");
 }
}


  我们首先设置了response的contentType为image/jpeg,这样浏览器就可以正确识别。
  然后,创建一个大小为100x100的BufferedImage对象,准备绘图:

int width = 100;
int height = 100;
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);


  接着,BufferedImage对象中获取Graphics2D对象并绘图:

Graphics2D g = bi.createGraphics(); // 创建Graphics2D对象
// 填充背景为白色:
g.setBackground(Color.BLUE);
g.clearRect(0, 0, width, height);
// 设置前景色:
g.setColor(Color.RED);
// 开始绘图:
g.drawLine(0, 0, 99, 99); // 绘制一条直线
// 绘图完成,释放资源:
g.dispose();
bi.flush();


  然后,对BufferedImage进行JPEG编码:

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(, false);
encoder.setJPEGEncodeParam(param);
try {
 encoder.encode(bi);
}
catch(IOException ioe) {
 ioe.printStackTrace();
}


  编码后的JPEG图像直接输出到了out对象中,我们只要传入response. getOutputStream()就可以直接输出到HttpResponse中。

  下面是完整的代码:

package com.crackj2ee.web.util;

import java.io.*;
import java.awt.*;
import java.awt.image.*;

import javax.servlet.*;
import javax.servlet.http.*;

import com.sun.image.codec.jpeg.*;

/**
* @author Liao Xue Feng
*/
public class CreateImageServlet extends HttpServlet {

 protected void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException
 {
  response.setContentType("image/jpeg");
  createImage(response.getOutputStream());
 }

 private void createImage(OutputStream out) {
  int width = 100;
  int height = 100;
  BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  Graphics2D g = bi.createGraphics();
  // set background:
  g.setBackground(Color.BLUE);
  g.clearRect(0, 0, width, height);
  // set fore color:
  g.setColor(Color.RED);
  // start draw:
  g.drawLine(0, 0, 99, 199);
  // end draw:
  g.dispose();
  bi.flush();
  // encode:
  JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
  param.setQuality(, false);
  encoder.setJPEGEncodeParam(param);
  try {
   encoder.encode(bi);
  }
  catch(IOException ioe) {
   ioe.printStackTrace();
  }
 }
}


  最后将这个Servlet编译,注册到web.xml中,映射路径为/CreateImage,写一个简单的index.html测试:

<html><head></head>
<body>
<img src="CreateImage">
</body></html>


  如能正确显示,大功告成!

 

 

 

import java.io.*;

import java.util.*;

import com.sun.image.codec.jpeg.*;

import java.awt.image.*;

import java.awt.*;

public class GenGif {

BufferedImage image;

// 创建 jpg 文件到指定路径下

public void createJpg(String path) {

try {

FileOutputStream fos = new FileOutputStream(path);

BufferedOutputStream bos = new BufferedOutputStream(fos);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);

encoder.encode(image);

bos.close();

} catch(FileNotFoundException fnfe) {

System.out.println(fnfe);

} catch(IOException ioe) {

System.out.println(ioe);

}

}

public static void main(String[] args) {

int width=400, height=300;

int xLength=300, yLength=150;

int count=5;

 

Vector data=new Vector();

data.addElement(new Integer(100));

data.addElement(new Integer(120));

data.addElement(new Integer(150));

data.addElement(new Integer(40));

data.addElement(new Integer(5));

 

GenGif jg = new GenGif();

jg.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

Graphics g = jg.image.getGraphics();

 

// 画坐标

g.setColor(Color.white);

g.fillRect(0, 0, width, height);

g.setColor(Color.blue);

g.drawLine(10,height-10,10,height-10-yLength);

g.drawLine(10,height-10,10+xLength,height-10);

// 连线

int yTo;

int yFrom = ((Integer)(data.elementAt(0))).intValue();

for (int i=1; i<count; i++) {

yTo=((Integer)(data.elementAt(i))).intValue();

g.drawLine(10+i*xLength/count,height-10,10+i*xLength/count,height-15);

g.drawLine(10+(i-1)*xLength/count,yFrom,10+i*xLength/count,yTo);

yFrom=yTo;

}

 

jg.createJpg("c:/aaa.jpg");

}

}

 

JDK1.7及以上时,API内部类com.sun.image.codec.jpeg.JPEGImageEncoder类及所属包不能再被引用,使用标准JDK编译将不通过,改用如下类:javax.imageio.ImageIO类同样可以传递图片流

        try {
            ImageIO.write(bi, "JPEG", outputStream);
            outputStream.flush();
            outputStream.close();
        }  catch (IOException e) {
            e.printStackTrace();
        }

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics