Batch image compression problem. Help!

C

Chris

I am using Java to develop a web application that lets user to upload images
to the web server, and the server will automatically resize and compress the
images. It lets users to upload maximum 5 images at one time. The system
works fine. But sometimes a very strange problem occurs. When the users
upload the image, the images are able to be uploaded to the system
successfully, but the system is hanged on the image compression stage. The
system message returns that the uploaded images are under compression, but
never ended, even one hour.

I have checked the system log and find there is nothing mentioned in the log
file. This problem doesn't happen regularly, but it does happen sometimes.

Please kindly advise me what mistake I have made.

Thanks,
Chris


Herebelow is the part of source code:
==============================================================
/**
* create small image.
* @param oldImage,give a image to create small image.
* @param newImage,the image you want to create.
* @param maxWidth,the max width.
* @param maxHeight, the max height.
* @return the image's name.
* @throws java.lang.Exception
*/
public String createThumbnail(String srcImg,String objImg, int
maxWidth,int maxHeight) throws Exception{
try {
EuwLog.EuwPrint("Begin Thumbnail"+objImg);
ImageIcon ii = new ImageIcon(srcImg);
Image i = ii.getImage();
ii = null;
Image temp = null;
BufferedImage bufferedImage=null;
int iWidth = i.getWidth(null);
int iHeight = i.getHeight(null);
double ratio = getRatio(iWidth,iHeight,maxWidth,maxHeight);
iWidth = (int)(ratio*iWidth);
iHeight = (int)(ratio*iHeight);
if (ratio<1.0f){
temp =
i.getScaledInstance(iWidth,iHeight,Image.SCALE_AREA_AVERAGING);
i.flush();
i = null;

temp = new ImageIcon(temp).getImage();
// Create the buffered image.
bufferedImage = new BufferedImage(iWidth, iHeight,
BufferedImage.TYPE_INT_RGB);

// Copy image to buffered image.
Graphics g = bufferedImage.createGraphics();
// Clear background and paint the image.
g.setColor(Color.white);
g.fillRect(0, 0, iWidth,iHeight);
g.drawImage(temp, 0, 0, null);
g.dispose();
g = null;

// sharpen
float[] sharpenArray = { -0.125f, -0.125f, -0.125f, -0.125f,2,
-0.125f, -0.125f, -0.125f, -0.125f };
Kernel kernel = new Kernel(3, 3, sharpenArray);
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP,
null);
bufferedImage = cOp.filter(bufferedImage, null);
cOp = null;
kernel = null;
/* write the jpeg to a file */
//File file = new File(objImg);
FileOutputStream out = new FileOutputStream(objImg);
/* encodes image as a JPEG data stream */
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param =
encoder.getDefaultJPEGEncodeParam(bufferedImage);
// writeParam = new JPEGImageWriteParam(null);
//
writeParam.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
//writeParam.setProgressiveMode(JPEGImageWriteParam.MODE_DEFAULT);
param.setQuality(0.9f, true);
encoder.setJPEGEncodeParam(param);
encoder.encode(bufferedImage);
param = null;
encoder = null;

temp.flush();
temp = null;

bufferedImage.flush();
bufferedImage = null;

out.close();
out = null;
EuwLog.EuwPrint("End Thumbnail"+objImg);
 
A

Andrew Thompson

Chris said:
I am using Java to develop a web application that lets user to upload images
to the web server, and the server will automatically resize and compress the
images. It lets users to upload maximum 5 images at one time. The system
works fine. But sometimes a very strange problem occurs. When the users
upload the image, the images are able to be uploaded to the system
successfully, but the system is hanged on the image compression stage. The
system message returns that the uploaded images are under compression, but
never ended, even one hour. ....
Herebelow is the part of source code:
public String createThumbnail(String srcImg,String objImg, int
maxWidth,int maxHeight) throws Exception{
try {
EuwLog.EuwPrint("Begin Thumbnail"+objImg);
ImageIcon ii = new ImageIcon(srcImg);

The JVM generally caches images aggressively, and using ImageIcons
is one of the surest ways to invoke image caching.

[ I suspect you are hitting an OutOfMemoryError. ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top