Java Image Resize代码无效

时间:2017-03-19 06:58:17

标签: java servlets image-resizing

我正在开发一个程序,我必须在将图像保存到数据库之前调整图像大小以节省服务器上的空间。代码已经运行了好几年但突然停止在远程服务器上工作,但它对localhost工作正常。我也联系了远程服务器维护团队,但他们有任何问题。我无法理解这个问题。请帮我解决这个问题。

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.*;
public class Image_Rendar
{
public byte[] resizeImageAsJPG(byte[] pImageData, int pMaxWidth) throws IOException
    {

            // Create an ImageIcon from the image data
            ImageIcon imageIcon = new ImageIcon(pImageData);
            int width = imageIcon.getIconWidth();
            int height = imageIcon.getIconHeight();
            //mLog.info("imageIcon width: #0  height: #1", width, height);
            // If the image is larger than the max width, we need to resize it
            if (pMaxWidth > 0 && width > pMaxWidth) 
            {
                // Determine the shrink ratio
                double ratio = (double) pMaxWidth / imageIcon.getIconWidth();
              //  mLog.info("resize ratio: #0", ratio);
                height = (int) (imageIcon.getIconHeight() * ratio);
                width = pMaxWidth;
              //  mLog.info("imageIcon post scale width: #0  height: #1", width, height);
            }
            // Create a new empty image buffer to "draw" the resized image into
            BufferedImage bufferedResizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            // Create a Graphics object to do the "drawing"
            Graphics2D g2d = bufferedResizedImage.createGraphics();
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            // Draw the resized image
            g2d.drawImage(imageIcon.getImage(), 0, 0, width, height, null);
            g2d.dispose();
            // Now our buffered image is ready
            // Encode it as a JPEG
            ByteArrayOutputStream encoderOutputStream = new ByteArrayOutputStream();
            //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(encoderOutputStream);
            //encoder.encode(bufferedResizedImage);
            byte[] resizedImageByteArray = encoderOutputStream.toByteArray();
            ImageIO.write(bufferedResizedImage, "jpeg", encoderOutputStream);
            return resizedImageByteArray;
            }
   }

打印完堆栈跟踪后,我有了这个。

java.lang.NullPointerException
at sun.awt.image.FetcherInfo.getFetcherInfo(Unknown Source)
at sun.awt.image.ImageFetcher.add(Unknown Source)
at sun.awt.image.InputStreamImageSource.startProduction(Unknown Source)
at sun.awt.image.InputStreamImageSource.addConsumer(Unknown Source)
at sun.awt.image.InputStreamImageSource.startProduction(Unknown Source)
at sun.awt.image.ImageRepresentation.startProduction(Unknown Source)
at sun.awt.image.ToolkitImage.addWatcher(Unknown Source)
at sun.awt.image.ToolkitImage.getProperty(Unknown Source)
at javax.swing.ImageIcon.<init>(Unknown Source)
at Image_Rendar.resizeImageAsJPG(Image_Rendar.java:15)
at Profile_img_upload.doPost(Profile_img_upload.java:103)

1 个答案:

答案 0 :(得分:2)

可能远程服务器已迁移到Java 7或更高版本。从Java 7开始,com.sun.image.codec.jpeg包已被删除(Source)。

另外值得一读:Why Developers Should Not Write Programs That Call 'sun' Packages