gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:23:38 26 4
gpt4 key购买 nike

我在我的 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;
}

最佳答案

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

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

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

关于java - 如何替换此代码中的 com.sun.image.codec.jpeg.JPEGImageEncoder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61244989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com