private static final int THUMBNAIL_SIZE = 150; private static final BufferedImage SCRATCH_BUFFER = new BufferedImage(THUMBNAIL_SIZE, THUMBNAIL_SIZE, BufferedImage.TYPE_INT_RGB); private static final Graphics2D GRAPHICS = SCRATCH_BUFFER.createGraphics(); // this is an inner class in the java file private final class GenThumbnailTask implements Runnable { private final String source_; private final String destination_; private final String extension_; private GenThumbnailTask(String source, String destination, String extension) { source_ = source; destination_ = destination; extension_ = extension; } private void generateThumbnail() throws IOException { File f = new File(source_); BufferedImage im = ImageIO.read(f); int width = im.getWidth(); int height = im.getHeight(); if (width > THUMBNAIL_SIZE && height > THUMBNAIL_SIZE) { //clear the previous image int dimensionToUse = width; if (height < width) { dimensionToUse = height; } GRAPHICS.setColor(Color.BLACK); GRAPHICS.fillRect(0, 0, THUMBNAIL_SIZE, THUMBNAIL_SIZE); GRAPHICS.drawImage(im, 0, 0, THUMBNAIL_SIZE, THUMBNAIL_SIZE, 0, 0, dimensionToUse, dimensionToUse, null); ImageIO.write(SCRATCH_BUFFER, extension_, new File(destination_)); } } /** {@inheritDoc} */ public void run() { try { File sourceFile = new File(source_); if (sourceFile.exists()) { File thumb = new File(destination_); thumb.getParentFile().mkdirs(); generateThumbnail(); } } catch (IOException e) { e.printStackTrace(); } } }