Reputation: 3011
I am developing an application where i need to save the thumbnails of the image in a folder inside the folder where the images are . The image folder is selected by means of a file chooser.
I am having a problem saving the thumbnails. It says an error message a file not found exception rather.
The code that i have written is :
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;
}
It throws a FileNotFoundException
, NullPointerException
in the createOutputFile()
method, at the f.createNewFile()
point .
The input file is the image in the folder selected . I have to place a thumbnail of this image inside a folder created inside the folder selected.
For example,
Selected image folder is D:\pictures
Then i need to place a thumbnail of every picture inside D:\pictures
in D:\pictures\thumbnails
.
please point out the mistake that i am doing and how to correct it .
Upvotes: 0
Views: 1580
Reputation: 30133
Rather than writing all of that code yourself, you might look into using the Thumbnailator library. Your entire example can be written in the following few lines, which expresses your intent much better.
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);
}
}
Upvotes: 2
Reputation: 13574
I wanted to play with a final static THUMBNAIL, and G2D... It didn't work, but this does if you can live with the ill-proportioned results of making all images the same size, and therefore shape, regardless of there original dimensions ;-)
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();
}
}
}
I'm glad you got it sorted out ;-)
Cheers. Keith.
Upvotes: 1