Permalink
Browse files

Added: wflow-commons - Add create thumbnail utility method in FileMan…

…ager

git-svn-id: http://dev.joget.org/svn/jw-community/trunk@405 1bff935d-2fe2-40b7-b00f-c2aca69a7612
  • Loading branch information...
1 parent e6ea2be commit a5725da9afbf798845e58ff5c65735cfdd131d7b owen committed Jun 12, 2012
Showing with 54 additions and 0 deletions.
  1. +54 −0 wflow-commons/src/main/java/org/joget/commons/util/FileManager.java
@@ -1,11 +1,17 @@
package org.joget.commons.util;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URLDecoder;
+import javax.imageio.ImageIO;
import org.springframework.web.multipart.MultipartFile;
public class FileManager {
+ public final static Integer THUMBNAIL_SIZE = 60;
+ public final static String THUMBNAIL_EXT = ".thumb.jpg";
public static String getBaseDirectory() {
return SetupManager.getBaseDirectory() + File.separator + "app_tempfile" + File.separator;
@@ -78,4 +84,52 @@ public static void deleteFile(File file) {
}
file.delete();
}
+
+ public static void createThumbnail(String path, Integer thumbWidth, Integer thumbHeight) {
+ if (thumbWidth == null) {
+ thumbWidth = THUMBNAIL_SIZE;
+ }
+ if (thumbHeight == null) {
+ thumbHeight = THUMBNAIL_SIZE;
+ }
+
+ BufferedOutputStream out = null;
+
+ try{
+ Image image = Toolkit.getDefaultToolkit().getImage(getBaseDirectory() + URLDecoder.decode(path, "UTF-8"));
+ MediaTracker mediaTracker = new MediaTracker(new Container());
+ mediaTracker.addImage(image, 0);
+ mediaTracker.waitForID(0);
+
+ double thumbRatio = (double) thumbWidth / (double) thumbHeight;
+ int imageWidth = image.getWidth(null);
+ int imageHeight = image.getHeight(null);
+ double imageRatio = (double) imageWidth / (double) imageHeight;
+ if (thumbRatio < imageRatio) {
+ thumbHeight = (int) (thumbWidth / imageRatio);
+ } else {
+ thumbWidth = (int) (thumbHeight * imageRatio);
+ }
+
+ BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
+ Graphics2D graphics2D = thumbImage.createGraphics();
+ graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
+ graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
+
+ out = new BufferedOutputStream(new FileOutputStream(getBaseDirectory() + URLDecoder.decode(path, "UTF-8") + THUMBNAIL_EXT));
+ ImageIO.write(thumbImage, "jpeg", out);
+
+ out.flush();
+ } catch (Exception ex) {
+ LogUtil.error(FileManager.class.getName(), ex, "");
+ } finally {
+ try {
+ if (out != null) {
+ out.close();
+ }
+ } catch (Exception ex) {
+ LogUtil.error(FileManager.class.getName(), ex, "");
+ }
+ }
+ }
}

0 comments on commit a5725da

Please sign in to comment.