保存文件夹中的图像时出现问题

时间:2011-05-15 06:57:55

标签: java image-processing file-io

我正在开发一个应用程序,我需要将图像的缩略图保存在图像所在文件夹内的文件夹中。通过文件选择器选择图像文件夹。

我在保存缩略图时遇到问题。它表示错误消息是文件未找到异常。

我写的代码是:

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;

import java.io.IOException;
import javax.imageio.ImageIO;

public class ThumbnailFactory {

    public ThumbnailFactory() {
    }

    public void run(String folder) {
        savepath = folder+"\\thumbnails";
        File dir = new File(folder);
        for (File file : dir.listFiles()) {
            createThumbnail(file);
        }
    }

    private void createThumbnail(File file) {
        try {
            // BufferedImage is the best (Toolkit images are less flexible)
            BufferedImage img = ImageIO.read(file);
            BufferedImage thumb = createEmptyThumbnail();

            // BufferedImage has a Graphics2D
            Graphics2D g2d = (Graphics2D) thumb.getGraphics();
            g2d.drawImage(img, 0, 0,
                    thumb.getWidth() - 1,
                    thumb.getHeight() - 1,
                    0, 0,
                    img.getWidth() - 1,
                    img.getHeight() - 1,
                    null);
            g2d.dispose();
            ImageIO.write(thumb, "PNG", createOutputFile(file));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private File createOutputFile(File inputFile) throws IOException {
       System.out.println(savepath+"\\"+inputFile.getName());
       File f =  new File(savepath+"\\"+inputFile.getName()+".png");
       if(!f.exists())
       {
           System.out.println("Creating the file in thumbnail directory");
           f.createNewFile();
       }
       return new File(savepath+"\\"+inputFile.getName()+".png") ;
    }

    private BufferedImage createEmptyThumbnail() {
        return new BufferedImage(100, 200,
                BufferedImage.TYPE_INT_RGB);
    }
    private String savepath;
}

它会在FileNotFoundException方法NullPointerException处抛出createOutputFile()f.createNewFile()

输入文件是所选文件夹中的图像。我必须将此图像的缩略图放在所选文件夹内创建的文件夹中。

例如,  所选图像文件夹为D:\pictures 然后我需要在D:\pictures中的D:\pictures\thumbnails内放置每张图片的缩略图。

请指出我正在做的错误以及如何纠正错误。

2 个答案:

答案 0 :(得分:2)

您可以考虑使用Thumbnailator库,而不是自己编写所有代码。您的整个示例可以用以下几行编写,这样可以更好地表达您的意图。

public class ThumbnailFactory {

    public void run(String folder) {
        Thumbnails.of(new File(folder+"\\thumbnails").listFiles())
            .size(100,200)
            .outputFormat("png")
            .asFiles(Rename.SUFFIX_HYPTHEN_THUMBNAIL);
    }
}

答案 1 :(得分:1)

我想玩最后的静态THUMBNAIL和G2D ...它不起作用,但如果你能忍受使所有图像尺寸相同,因此形状不均匀的结果,这就行了有原始尺寸; - )

package forums;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

public class ThumbnailFactory
{
  private static final String THUMBNAILS_SUBDIR_NAME = File.separator + "thumbnails";

  private final File _thumbsSubdir;
  private final File _picsDir;

  public ThumbnailFactory(String picsDirectoryPath) {
    _picsDir = new File(picsDirectoryPath);
    _thumbsSubdir = new File(thumbDirectoryPath(_picsDir));
  }

  private static String thumbDirectoryPath(File picsDir) {
    return picsDir.getAbsolutePath()+THUMBNAILS_SUBDIR_NAME;
  }

  public void createThumbnails() throws IOException {
    if (!_thumbsSubdir.exists()) {
      _thumbsSubdir.mkdir();
    }
    for (File picFile : _picsDir.listFiles(
      new FilenameFilter() {
        public boolean accept(File f, String s) {
          return s.toLowerCase().endsWith(".jpg");
        }
      }
    )) {
      if ( !createThumbnail(picFile, new File(thumbFilename(picFile))) )
        break;
    }
  }

  private String thumbFilename(File pictureFile) {
    return _thumbsSubdir.getAbsolutePath()
           + File.separator 
           + pictureFile.getName()
           + ".png";
  }

  private boolean createThumbnail(File pictureFile, File thumbFile)
    throws IOException
  {
    boolean retval = false;
    BufferedImage image = new BufferedImage(100, 200, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = (Graphics2D) image.getGraphics();
    BufferedImage picture = ImageIO.read(pictureFile);
    if (picture!=null) {
      g2d.drawImage(
          picture
        , 0, 0, image.getWidth()-1, image.getHeight()-1
        , 0, 0, picture.getWidth()-1, picture.getHeight()-1
        , null
      );
      retval = ImageIO.write(image, "PNG", thumbFile);
      System.out.println(thumbFile);
    }
    return retval;
  }

  public static void main(String... args) {
    try {
      ThumbnailFactory factory = new ThumbnailFactory("C:/Users/Administrator/Pictures");
      factory.createThumbnails();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

我很高兴你把它整理好了; - )

干杯。基思。