001package ca.bc.webarts.tools; 002 003import java.awt.image.*; 004import java.awt.*; 005import java.io.*; 006import javax.imageio.*; 007import java.util.*; 008import ca.bc.webarts.widgets.Util; 009 010import net.coobird.thumbnailator.Thumbnails; 011import net.coobird.thumbnailator.name.Rename; 012 013public class ImageThumbnailer 014{ 015 static int DEFAULT_THUMB_WIDTH = 48; 016 static int DEFAULT_THUMB_HEIGHT = 48; 017 protected int thumbWidth_ = DEFAULT_THUMB_WIDTH; // Specify image width in px 018 protected int thumbHeight_ = DEFAULT_THUMB_HEIGHT; // Specify image height in px 019 020 public ImageThumbnailer(String filename) throws IOException 021 { 022 File file = new File(filename); // Specify the input image source file location. 023 String outThumbfileWithoutExt = filename.substring(0, filename.lastIndexOf(".")); 024 025 if (file.canRead() && file.isDirectory()) 026 { 027 Thumbnails.of(file.listFiles()) 028 .size(DEFAULT_THUMB_WIDTH, DEFAULT_THUMB_HEIGHT) 029 .outputFormat("jpg") 030 .toFiles(Rename.PREFIX_DOT_THUMBNAIL); 031 } 032 else 033 { 034 Thumbnails.of(file) 035 .size(DEFAULT_THUMB_WIDTH, DEFAULT_THUMB_HEIGHT) 036 .outputFormat("jpg") 037 .toFiles(Rename.PREFIX_DOT_THUMBNAIL); 038 } 039 040 /* 041 try 042 { 043 FileInputStream fis = new FileInputStream(file); 044 InputStream bis = new BufferedInputStream(fis); 045 FileOutputStream fos = null; 046 Image image = (Image) ImageIO.read(bis); 047 048 int imageWidth = image.getWidth(null); // get image Width 049 int imageHeight = image.getHeight(null); // get image Height 050 051 double thumbRatio = (double) thumbWidth_ / (double) thumbHeight_; 052 double imageRatio = (double) imageWidth / (double) imageHeight; 053 054 if (thumbRatio < imageRatio) 055 { 056 thumbHeight_ = (int) (thumbWidth_ / imageRatio); 057 } 058 else 059 { 060 thumbWidth_ = (int) (thumbHeight_ * imageRatio); 061 } 062 063 BufferedImage thumbImage = new BufferedImage(thumbWidth_, thumbHeight_, BufferedImage.TYPE_INT_RGB); 064 065 Graphics2D graphics = thumbImage.createGraphics(); 066 graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 067 graphics.drawImage(image, 0, 0, thumbWidth_, thumbHeight_, null); 068 069 ByteArrayOutputStream out = new ByteArrayOutputStream(); 070 071 int quality = 300; 072 quality = Math.max(0, Math.min(quality, 500)); 073 074 if (filename.toLowerCase().endsWith("jpg")) 075 { 076 saveAsJPEG(96, outThumbfileWithoutExt + "_thumb.jpg"); 077 078 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 079 System.out.println("Encoder" + encoder); 080 JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage); 081 082 param.setQuality(0.75f, false); 083 084 String format = "jpg"; 085 086 encoder.setJPEGEncodeParam(param); 087 encoder.encode(thumbImage); 088 ImageIO.write(thumbImage, format, new File(outThumbfileWithoutExt + "_thumb.jpg")); 089 } 090 else if (filename.toLowerCase().endsWith("png")) 091 { 092 PNGImageEncoder encoder = PNGCodec.createPNGEncoder(out); 093 System.out.println("Encoder" + encoder); 094 PNGEncodeParam param = encoder.getDefaultPNGEncodeParam(thumbImage); 095 096 param.setQuality(0.75f, false); 097 098 String format = "png"; 099 100 encoder.setPNGEncodeParam(param); 101 encoder.encode(thumbImage); 102 ImageIO.write(thumbImage, format, new File(outThumbfileWithoutExt + "_thumb.png")); 103 } 104 } 105 catch (IOException ioExcep) 106 { 107 ioExcep.printStackTrace(); 108 } 109 catch (Exception excep) 110 { 111 excep.printStackTrace(); 112 } 113 114 */ 115 } 116 117 public static String createThumbnail(String filename) throws IOException 118 { 119 File file = new File(filename); // Specify the input image source file location. 120 String outThumbfileWithoutExt = "thumbnail."+ 121 filename.substring(0, (filename.lastIndexOf(".")==-1?filename.length():filename.lastIndexOf("."))); 122 123 if (file.canRead() && file.isDirectory()) 124 { 125 Thumbnails.of(file.listFiles()) 126 .size(DEFAULT_THUMB_WIDTH, DEFAULT_THUMB_HEIGHT) 127 .outputFormat("jpg") 128 .toFiles(Rename.PREFIX_DOT_THUMBNAIL); 129 } 130 else 131 { 132 Thumbnails.of(file) 133 .size(DEFAULT_THUMB_WIDTH, DEFAULT_THUMB_HEIGHT) 134 .outputFormat("jpg") 135 .toFiles(Rename.PREFIX_DOT_THUMBNAIL); 136 } 137 138 return outThumbfileWithoutExt+".jpg"; 139 } 140 141 142 /** 143 * main method 144 **/ 145 public static void main(String[] args) throws IOException 146 { 147 String path = args[0]; // "/home/devaraj/Image/portraits.jpg"; 148 ImageThumbnailer instance = new ImageThumbnailer(path); 149 150 } // main 151 152}