如何在此代码中替换com.sun.image.codec.jpeg.JPEGImageEncoder?

时间:2023-01-24 21:22:01

I have used com.sun.image.codec.jpeg.JPEGImageEncoder to handle JPEG images, like charts and others, in my webapp. Now, I am updating my machine to use JDK7, but this version deprecated this class. Below is the code that I need to change:

我使用com.sun.image.codec.jpeg.JPEGImageEncoder来处理我的webapp中的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;
}

2 个解决方案

#1


37  

Use ImageIO, it can read/write JPEG, PNG, GIF, and BMP out of the box.

使用ImageIO,它可以开箱即用地读/写JPEG,PNG,GIF和BMP。

ImageIO.write(ChartImage, "jpeg", out);

Usually the static write(...) and read(...) methods are enough, if you need to control compression or handle image meta data, check out the Java Image I/O API Guide.

通常静态write(...)和read(...)方法就足够了,如果需要控制压缩或处理图像元数据,请查看Java Image I / O API Guide。

#2


15  

Classes from the com.sun.** packages should never be used. Use ImageIO to encode images into bytes.

不应使用com.sun。**包中的类。使用ImageIO将图像编码为字节。

Read the ImageIO tutorial.

阅读ImageIO教程。

#1


37  

Use ImageIO, it can read/write JPEG, PNG, GIF, and BMP out of the box.

使用ImageIO,它可以开箱即用地读/写JPEG,PNG,GIF和BMP。

ImageIO.write(ChartImage, "jpeg", out);

Usually the static write(...) and read(...) methods are enough, if you need to control compression or handle image meta data, check out the Java Image I/O API Guide.

通常静态write(...)和read(...)方法就足够了,如果需要控制压缩或处理图像元数据,请查看Java Image I / O API Guide。

#2


15  

Classes from the com.sun.** packages should never be used. Use ImageIO to encode images into bytes.

不应使用com.sun。**包中的类。使用ImageIO将图像编码为字节。

Read the ImageIO tutorial.

阅读ImageIO教程。