40

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;
}
Lahiru Ashan
  • 767
  • 9
  • 16
Edilmar Alves
  • 401
  • 1
  • 4
  • 3
  • Hi, can you please share the version of library you are using for this image conversion, im facing the same problem but looking some issue of library, it will be great if you share the maven dependency. – NoNaMe Apr 27 '16 at 07:49

3 Answers3

48

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.

Philipp Reichart
  • 20,771
  • 6
  • 58
  • 65
16

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

Read the ImageIO tutorial.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
3

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());