Reputation: 85
I am working on a program in which I have to resize the image before saving it into the database in order to save the space on the server. The code has been working fine for years but suddenly it stopped working on the remote server but it is working fine for the localhost.I have also contacted the remote server maintenance team but having got any problem from their side. I am unable to understand the problem. Please help me to solve this.
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;
}
}
After Printing the Stacktrace I have got this.
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)
Upvotes: 0
Views: 199
Reputation: 1589
Maybe the remote server was migrated to Java 7 or later. Starting in Java 7, the com.sun.image.codec.jpeg
package was removed (Source).
Also worth a read: Why Developers Should Not Write Programs That Call 'sun' Packages
Upvotes: 2