/** * Add a Thumbnailer-Class to the list of available Thumbnailers Note that the order you add * Thumbnailers may make a difference: First added Thumbnailers are tried first, if one fails, the * next (that claims to be able to treat such a document) is tried. (Thumbnailers that claim to * treat all MIME Types are tried last, though.) * * @param thumbnailer Thumbnailer to add. */ public void registerThumbnailer(Thumbnailer thumbnailer) { String[] acceptMIME = thumbnailer.getAcceptedMIMETypes(); if (acceptMIME == null) thumbnailers.put(ALL_MIME_WILDCARD, thumbnailer); else { for (String mime : acceptMIME) thumbnailers.put(mime, thumbnailer); } allThumbnailers.add(thumbnailer); thumbnailer.setImageSize(thumbWidth, thumbHeight, thumbOptions); }
/** * Set the image size of all following thumbnails. * * <p>ThumbnailManager delegates this to all his containing Thumbailers. */ public void setImageSize(int width, int height, int imageResizeOptions) { thumbHeight = height; thumbWidth = width; thumbOptions = imageResizeOptions; if (thumbWidth < 0) thumbWidth = 0; if (thumbHeight < 0) thumbHeight = 0; for (Thumbnailer thumbnailer : allThumbnailers) thumbnailer.setImageSize(thumbWidth, thumbHeight, thumbOptions); }
/** * Instead of a deconstructor: De-initialize ThumbnailManager and its thumbnailers. * * <p>This functions should be called before termination of the program, and Thumbnails can't be * generated after calling this function. */ public void close() { if (allThumbnailers == null) return; // Already closed for (Thumbnailer thumbnailer : allThumbnailers) { try { thumbnailer.close(); } catch (IOException e) { mLog.error("Error during close of Thumbnailer:", e); } } thumbnailers = null; allThumbnailers = null; }
/** * Helper function for Thumbnail generation: execute all thumbnailers of a given MimeType. * * @param useMimeType Which MIME Type the thumbnailers should be taken from * @param input Input File that should be processed * @param output Output file where the image shall be written. * @param detectedMimeType MIME Type that was returned by automatic MIME Detection * @return True on success (1 thumbnailer could generate the output file). * @throws IOException Input file cannot be read, or output file cannot be written, or necessary * temporary files could not be created. */ private boolean executeThumbnailers( String useMimeType, File input, File output, String detectedMimeType) throws IOException { for (Thumbnailer thumbnailer : thumbnailers.getIterable(useMimeType)) { try { thumbnailer.generateThumbnail(input, output, detectedMimeType); return true; } catch (ThumbnailerException e) { // This Thumbnailer apparently wasn't suitable, so try next mLog.warn( "Warning: " + thumbnailer.getClass().getName() + " could not handle the file " + input.getName() + " (trying next)", e); } } return false; }