Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Batch image compression problem.  Help!

Thread view: 
Chris - 17 Nov 2005 19:09 GMT
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);
Andrew Thompson - 17 Nov 2005 23:28 GMT
> 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
[quoted text clipped - 4 lines]
> 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. ]

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.