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

发布于 2024-12-14 01:48:40 字数 1768 浏览 1 评论 0原文

我在我的 web 应用程序中使用了 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;
}

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:

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘寄 2024-12-21 01:48:40

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

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

通常静态 write(...)read(...) 方法就足够了,如果您需要控制压缩或处理图像元数据,请查看 < href="http://download.oracle.com/javase/7/docs/technotes/guides/imageio/spec/imageio_guideTOC.fm.html">Java 图像 I/O API 指南。

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

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.

不气馁 2024-12-21 01:48:40

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

阅读 ImageIO 教程

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

Read the ImageIO tutorial.

唯憾梦倾城 2024-12-21 01:48:40

com.sun.image.codec.jpeg.JPEGImageEncoder 在 Java 8 中工作正常,但在 Java 11 中不起作用

在我的项目中,我使用的是 Java 8 并且我已经升级了 Java 11,这对我来说是同样的问题。

我的代码是:

File output = new File(newFilename);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(new FileOutputStream(output));
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);
param.setQuality(1.0f, true);
encoder.setJPEGEncodeParam(param);
encoder.encode(bufferedImage);

我已删除上面的代码并将其替换为下面的代码,现在它工作正常

ImageIO.write(bufferedImage, "jpg", response.getOutputStream());

com.sun.image.codec.jpeg.JPEGImageEncoder is working fine in Java 8 but it is not working in Java 11

In my Project, I was using Java 8 and I had upgraded Java 11 and It was the same issue with me.

My Code was:

File output = new File(newFilename);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(new FileOutputStream(output));
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);
param.setQuality(1.0f, true);
encoder.setJPEGEncodeParam(param);
encoder.encode(bufferedImage);

I have removed the above code and replace it with the below code and now, it is working fine

ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文