如何替换此代码中的 com.sun.image.codec.jpeg.JPEGImageEncoder?
我在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
ImageIO
,它可以读取/开箱即用地编写 JPEG、PNG、GIF 和 BMP。通常静态
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.Usually the static
write(...)
andread(...)
methods are enough, if you need to control compression or handle image meta data, check out the Java Image I/O API Guide.切勿使用 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.
com.sun.image.codec.jpeg.JPEGImageEncoder 在 Java 8 中工作正常,但在 Java 11 中不起作用
在我的项目中,我使用的是 Java 8 并且我已经升级了 Java 11,这对我来说是同样的问题。
我的代码是:
我已删除上面的代码并将其替换为下面的代码,现在它工作正常
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:
I have removed the above code and replace it with the below code and now, it is working fine