1 22 23 package org.meshcms.webui; 24 25 import java.awt.*; 26 import java.awt.image.*; 27 import java.io.*; 28 import javax.imageio.*; 29 import org.meshcms.core.*; 30 31 public class FileManagerThumbnail extends AbstractThumbnail { 32 35 public static final int THUMB_WIDTH = 108; 36 37 40 public static final int THUMB_HEIGHT = 120; 41 42 45 public static final int THUMB_SIZE = 100; 46 47 private boolean highQuality; 48 49 public String getSuggestedFileName() { 50 return highQuality ? "filemanager_hq.jpg" : "filemanager.jpg"; 51 } 52 53 protected boolean createThumbnail(File imageFile, File thumbnailFile) { 54 BufferedImage image; 55 56 try { 57 image = ImageIO.read(imageFile); 58 } catch (Exception ex) { 59 return false; 60 } 61 62 if (image == null || image.getWidth() < 1) { 63 return false; 64 } 65 66 BufferedImage thumb = new BufferedImage(THUMB_WIDTH, THUMB_HEIGHT, 67 BufferedImage.TYPE_INT_RGB); 68 Graphics2D g = (Graphics2D) thumb.getGraphics(); 69 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 70 RenderingHints.VALUE_ANTIALIAS_ON); 71 72 g.setColor(Color.WHITE); 73 g.fillRect(0, 0, THUMB_WIDTH, THUMB_HEIGHT); 74 g.setColor(DEFAULT_BORDER_COLOR); 75 g.drawRect(0, 0, THUMB_WIDTH - 1, THUMB_WIDTH - 1); 76 g.fillRect(0, THUMB_WIDTH, THUMB_WIDTH, THUMB_HEIGHT - THUMB_WIDTH); 77 78 int w = image.getWidth(); 79 int h = image.getHeight(); 80 int kb = (int) (imageFile.length() / 1024L) + 1; 81 String label = w + "x" + h + ", " + kb + "KB"; 82 83 g.setColor(Color.BLACK); 84 int fontSize = 12; 85 int labelSize; 86 Font font; 87 FontMetrics fm; 88 89 do { 90 font = new Font("sansserif", Font.PLAIN, --fontSize); 91 fm = g.getFontMetrics(font); 92 labelSize = fm.stringWidth(label); 93 } while (labelSize > THUMB_SIZE); 94 95 g.setFont(font); 96 97 int ix, iy, w0, h0; 98 ix = iy = (THUMB_WIDTH - THUMB_SIZE) / 2; 99 100 if (w <= THUMB_SIZE && h <= THUMB_SIZE) { 101 w0 = w; 102 h0 = h; 103 ix += (THUMB_SIZE - w0) / 2; 104 iy += (THUMB_SIZE - h0) / 2; 105 } else if (w > h) { 106 w0 = THUMB_SIZE; 107 h0 = w0 * h / w; 108 iy += (THUMB_SIZE - h0) / 2; 109 } else { 110 h0 = THUMB_SIZE; 111 w0 = h0 * w / h; 112 ix += (THUMB_SIZE - w0) / 2; 113 } 114 115 w0 = Math.max(w0, 1); 116 h0 = Math.max(h0, 1); 117 118 AbstractThumbnail.drawResizedImage(g, image, ix, iy, w0, h0, highQuality); 119 image.flush(); 120 121 ix = (THUMB_WIDTH - labelSize) / 2; 122 iy = THUMB_HEIGHT - fm.getDescent(); 123 g.drawString(label, ix, iy); 124 125 OutputStream os = null; 126 127 try { 128 os = new BufferedOutputStream(new FileOutputStream(thumbnailFile)); 129 ImageIO.write(thumb, "jpeg", os); 130 } catch (IOException ex) { 131 ex.printStackTrace(); 132 return false; 133 } finally { 134 thumb.flush(); 135 g.dispose(); 136 137 if (os != null) { 138 try { 139 os.close(); 140 } catch (IOException ex) {} 141 } 142 } 143 144 return true; 145 } 146 147 150 public boolean isHighQuality() { 151 return highQuality; 152 } 153 154 157 public void setHighQuality(boolean highQuality) { 158 this.highQuality = highQuality; 159 } 160 } 161 | Popular Tags |