Posted by
markthien on
URL: http://imagej.273.s1.nabble.com/Image-corrupted-sometimes-tp3699628.html
Hi guys,
Below is my code:
------------------------------------------------------
package com.stufftolet.util;
import ij.ImagePlus;
import ij.io.FileSaver;
import ij.io.Opener;
import ij.process.ImageProcessor;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
public class ImageUtil {
/**
* Logger for this classImageUtil.java
*/
private static final Logger logger = Logger.getLogger(ImageUtil.class);
public static final String JPEG = "image/jpeg";
public static final String PJPEG = "image/pjpeg";
public static final String GIF = "image/gif";
public static final String PNG = "image/x-png";
public static final String JPEG_EXT = "jpg";
public static final String PJPEG_EXT = "jpg";
public static final String GIF_EXT = "gif";
public static final String PNG_EXT = "png";
public static final String TIFF_EXT = "tiff";
public static boolean convertImage(String sourceFile, String targetFile, int convertRatio, String path){
Opener opener = new Opener();
ImagePlus imageplus = opener.openImage(sourceFile);
ImageProcessor imageprocessor = null;
FileSaver filesaver = null;
try{
if(imageplus != null){
boolean convertWidthRatio = false;
imageprocessor = imageplus.getProcessor();
int imgHeight = imageprocessor.getHeight();
int imgWidth = imageprocessor.getWidth();
if(imgWidth > imgHeight)
convertWidthRatio = true;
if(convertWidthRatio){ // convert image based on it width
if(imgWidth > convertRatio){ // no need to resize if imgWidth is less than the convertRatio
imgHeight = (imgHeight * convertRatio) / imgWidth; // for keeping the file size ratio
imageplus.setProcessor(null, imageprocessor.resize(convertRatio, imgHeight));
}
}else{ // convert image based on it height
if(imgHeight > convertRatio){
imgWidth = (imgWidth * convertRatio) / imgHeight; // for keeping the file size ration
imageplus.setProcessor(null, imageprocessor.resize(imgWidth, convertRatio));
}
}
filesaver = new FileSaver(imageplus);
String ext = StringUtils.substringAfterLast(targetFile,".");
if(ext.equalsIgnoreCase(GIF_EXT))
filesaver.saveAsGif(targetFile);
else
filesaver.saveAsJpeg(targetFile);
}else
return false;
}catch(Exception ex){
ex.printStackTrace();
return false;
}finally{
opener = null;
imageplus = null;
imageprocessor = null;
filesaver = null;
}
System.gc();
return true;
}
}
------------------------------------------------------
I'm running my application in JBoss 4.0.4. User upload image like 800x600 then my program will compress it to a smaller image and smaller size too. Some image will get corrupted after compression sometimes if you see the image at the link below. I really have no idea why it will get corrupted. I have define jboss run.conf like below:
http://www.stufftolet.com.cn/postingpages/view/display-all-images.html?totalImages=11&imageURL1=http://stufftolet.com/uploaded/images/7628/9387692253/1s2e2mj5Bc84aa9_500.jpg&imageURL2=http://stufftolet.com/uploaded/images/7628/9387692253/4EygkwTUFDdnTZB_500.jpg&imageURL3=http://stufftolet.com/uploaded/images/7628/9387692253/CusuJ8AHnOR1r7u_500.jpg&imageURL4=http://stufftolet.com/uploaded/images/7628/9387692253/KmIHTYk2QDCrttS_500.jpg&imageURL5=http://stufftolet.com/uploaded/images/7628/9387692253/KmrTEOTdJTa5To7_500.jpg&imageURL6=http://stufftolet.com/uploaded/images/7628/9387692253/NqEUcUEP5MZIHCp_500.jpg&imageURL7=http://stufftolet.com/uploaded/images/7628/9387692253/R9Tp7IX7l6avawk_500.jpg&imageURL8=http://stufftolet.com/uploaded/images/7628/9387692253/Sj4HVbHUrMXjq5a_500.jpg&imageURL9=http://stufftolet.com/uploaded/images/7628/9387692253/ppHiseIrRwSWmZU_500.jpg&imageURL10=http://stufftolet.com/uploaded/images/7628/9387692253/qBsmyTvnKa9djTo_500.jpg&imageURL11=http://stufftolet.com/uploaded/images/7628/9387692253/rey5GJqvHyHuNYW_500.jpg#
# Specify options to pass to the Java VM.
#
if [ "x$JAVA_OPTS" = "x" ]; then
JAVA_OPTS="-Xms512m -Xmx640m -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rm i.dgc.server.gcInterval=3600000"
fi
Is it because not enough memory? The OS is CentOS and memory available is about 700MB. Please help, Thanks !