我曾经使用过com.sun.image.codec.jpeg.JPEGImageEncoder
来处理JPEG图像,例如网站中的图表等。现在,我正在升级我的机器以使用JDK7,但这个版本已经弃用了这个类。以下是我需要更改的代码:
public void processChart(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
out = response.getOutputStream();
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires",0);
try {
int w = Integer.parseInt(request.getParameter("WIDTH"));
int h = Integer.parseInt(request.getParameter("HEIGHT"));
java.awt.image.BufferedImage ChartImage = new java.awt.image.BufferedImage(w,h,java.awt.image.BufferedImage.TYPE_INT_RGB);
java.awt.Graphics2D ChartGraphics = ChartImage.createGraphics();
Chart c = getChart(request);
c.setSize(w,h);
c.paint(ChartGraphics);
// CODE BELOW TO CHANGE...
com.sun.image.codec.jpeg.JPEGImageEncoder encoder =
com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(out);
encoder.encode(ChartImage);
} catch (Exception e) {
e.printStackTrace();
}
}
private Chart getChart(HttpServletRequest request) {
chartLoader loader = new chartLoader(null);
loader.paintDirect = true;
java.util.Enumeration ps = request.getParameterNames();
while (ps.hasMoreElements()) {
String name = (String) ps.nextElement();
loader.setParameter(name,request.getParameter(name));
}
// create Chart
Chart chart = loader.build(false,false);
Chart.tmpImage = new java.awt.image.BufferedImage(200,200,java.awt.image.BufferedImage.TYPE_INT_RGB);
return chart;
}