Skip to main content

java class for JPEG image manipulation

2 replies [Last post]
shelen
Offline
Joined: 2006-11-13
Points: 0

java class for JPEG image manipulation
that will allow me to take a JPEG at one resolution and rescale it to a different resolution

When the image files are uploaded, my program area doesn't want the higher resolutions

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
cjstehno
Offline
Joined: 2003-07-24
Points: 0

I recently wrote a simple little app to scale a set of images... perhaps this will help.

public static void main(String[] args) {
try {
final File srcdir = new File(args[0]);
final File destdir = new File(args[1]);
final String extension = args[2];
final float scale = Float.parseFloat(args[3]);

final File[] srcfiles = srcdir.listFiles(new FileFilter(){
public boolean accept(File f){
return(f.isFile() && f.getName().toLowerCase().endsWith(extension.toLowerCase()));
}
});

for (File file : srcfiles) {
final BufferedImage image = ImageIO.read(file);
final int w = (int)((float)image.getWidth() * scale);
final int h = (int)((float)image.getHeight() * scale);
final Image scaledimg = image.getScaledInstance(w, h, Image.SCALE_SMOOTH);

final BufferedImage scaledbi = new BufferedImage(scaledimg.getWidth(null),scaledimg.getHeight(null),BufferedImage.TYPE_INT_RGB);
scaledbi.getGraphics().drawImage(scaledimg, 0, 0, null);

final File destfile = new File(destdir,FilenameUtils.removeExtension(file.getName()) + "_" + w + "x" + h + extension);
ImageIO.write(scaledbi, "jpeg",destfile);

System.out.println("Scaled: " + file + " --> " + destfile);
}

} catch(Exception ex){ex.printStackTrace();}
}

You should notice the getScaledInstance() method... that is probably what you are looking for. This is all standard Java 5 core API.

Good luck.

PS: sorry about the code formatting.

shelen
Offline
Joined: 2006-11-13
Points: 0

Thanks cjstehno.
Shelen