tags:

views:

513

answers:

2

I have a small problem... There are hundreds of images in a folder. they should scale down to specific height and specific width. so, the problem is... Retrieving images from a folder, and scale down them, and save them into another folder. Is it possible with Java? if yes, please help me, how? This is only for reducing memory size in my application. ( all files should be in JPEG format).

+2  A: 

You can use ImageIO to read and Image, then use the Image.getScaledInstance() method to scale the image. Finally you can use ImageIO to write out the new image.

Oops, I'm not sure what type of Image gets returned when you use the getScaledInstance() method. The ImageIO.write() method needs a RenderedImage, so you may need to create a BufferedImage first and paint the scaled image onto the buffer and then write out the BufferedImage.

camickr
Please, give a short example code...
Jessu
Since you have a 0% accept rate on questions you post, I'm not about to spoon feed the answer to you. So you can start by reading the API for the classes and methods mentioned. Then you try to write the code yourself. If you have problems, then you can post the demo code you tried. Or of course you can also search the web, now that you know the name of some of the methods to use to find examples that already exist.
camickr
A: 

My solution gave images but all images has fixed size, but i need their size in specific ratio which fits my height and width. If any modification, help me..

package vimukti.image;

import java.awt.Graphics; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.HeadlessException; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileFilter; import javax.imageio.ImageIO; import javax.swing.ImageIcon;

public class ImagesScaling { File inputDir; File outputDir; File[] files;

public ImagesScaling(File srcFile, File destFile) throws Exception{
 inputDir = srcFile;
 outputDir = destFile;

 class JPGFileFilter implements FileFilter {

    public boolean accept(File pathname) {

      if (pathname.getName().endsWith(".jpg"))
        return true;
      if (pathname.getName().endsWith(".jpeg"))
        return true;
      return false;
    }
 }
 JPGFileFilter filter = new JPGFileFilter();
 if(!outputDir.exists())
  outputDir.mkdir();

 if(inputDir.isDirectory()){
  files = inputDir.listFiles(filter);
 }
 if(files != null)
  scaling();
}

void scaling()throws Exception{
 for(File f : files){
  Image srcImage = ImageIO.read(f);
  Image destImage = srcImage.getScaledInstance(150, 180, Image.SCALE_AREA_AVERAGING);
  BufferedImage buffImg = toBufferedImage(destImage); 

  ImageIO.write(buffImg, "jpg",
    new File(outputDir.getAbsolutePath() + "\\"+f.getName()));
 }
}

void displayFiles(File dir){
 System.out.println("dir: " + dir);
 String[] files = dir.list();
 System.out.println("Files....");
 if(files != null)
  for(String s  : files)
   System.out.println("\t" +dir.getAbsolutePath()+"\\"+ s);
 System.out.println("end");

}
public static void main(String[] args)throws Exception {
 ImagesScaling imgScale = new ImagesScaling(
    new File("C:\\pictures"),
    new File("C:\\ScaledePictures"));

 imgScale.displayFiles(new File("C:\\ScaledPictures"));
}
////////////////////// Copied ////////////////////////////////////
// This method returns a buffered image with the contents of an image
public BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage)image;
    }

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels; for this method's
    // implementation, see e661 Determining If an Image Has Transparent Pixels


    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(
            image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }

    if (bimage == null) {
        // Create a buffered image using the default color model
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}

}

add comment

Jessu
You're explicitly scaling your images to 150x180 - which is consistent with your question, but in this answer you seem to be saying you want them reduced by a specific ratio instead. If that's what you mean, then instead of inline constants for size, you'd calculate it using a factor - for example, originalWidth/20.As an aside, note that the extension can be any case, so you should probably be using "jpg".equalsIgnoreCase (which of course means you have to find the extension using lastIndexOf). Good luck.
CPerkins