Batch image compression problem. Help!  
Author Message
Chris





PostPosted: 2005-11-18 3:09:00 Top

java-programmer, Batch image compression problem. Help! 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);


 
Chris





PostPosted: 2005-11-18 3:10:00 Top

java-programmer >> Batch image compression problem. Help! 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);


 
Oliver Wong





PostPosted: 2005-11-18 3:49:00 Top

java-programmer >> Batch image compression problem. Help!
"Chris" <email***@***.com> wrote in message
news:dlimb7$2bl$email***@***.com...
>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);

I noticed that you method throws "Exception". Maybe you can make the
throw cause more specific than that. I.e. only declare your method as
throwing exceptions that you actually throw.

I noticed also that your method starts with a try statement, implying
that you might be using a giant try catch block. Do you do anything with the
exception that you catch at the end, or do you just ignore it or what?

- Oliver


 
 
Andrew Thompson





PostPosted: 2005-11-18 7:28:00 Top

java-programmer >> Batch image compression problem. Help! Chris wrote:

> 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. ]

--
Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew
Currently accepting short and long term contracts - on Earth.