maandag 6 augustus 2012

resizing images on the fly

Intro:

After determining where the files are and how to move them into another folder as I showed in my previous post Howto create a set of files on the fly, It is fun to resize the files which are images in this case (makes it easier).

The problem:

We have a set of images, which might change and dont want to resize them by hand everytime.

The solution:

The solution lies hidden in Java as many times. The following example shows howto resize:



private void createIcons(String path){
    int width=100;
    int height = 100;
    File imageInput = File(path);
   ImageIcon imageIcon = new ImageIcon(path);
   BufferedImage imageResize =
  new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  Graphics2D graphics2d = imageResize.createGraphics();
  graphics2d.addRenderingHints(
   new RenderingHints(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY));
   graphics2d.drawImage(imageIcon.getImage(), 0, 0, height, width,
   null);
   String iconName = path.replaceAll(
   IMAGES, ICONS);
   try {
      ImageIO.write(imageResize,
      "png", new File(iconName));
    }
    catch (IOException ioe) {
      LOGGER.error("The image " + iconName + " could not be saved!");
    }
}

 In my previous post I had a line "String filePath =path.replace(IMAGES, ICONS); " all you have to do is to replace that line with:  "createIcons(image.getAbsolutePath());" and you be on your way with a full subset of folders and resized images. The only kind of images I had it never working with was BMP.

A working version or the sources you can download from:
http://sourceforge.net/projects/imageicon/files/imageicon-1.0-SOURCES.jar/download

Or check out the sources from:
ssh://git.code.sf.net/p/imageicon/code

 have fun.

Geen opmerkingen:

Een reactie posten