1 22 23 package org.meshcms.core; 24 25 import java.awt.*; 26 import java.awt.image.*; 27 import java.io.*; 28 import javax.imageio.*; 29 30 public class GalleryThumbnail extends AbstractThumbnail { 31 34 public static final int THUMB_SIZE = 96; 35 36 39 public static final int THUMB_IMAGE_SIZE = 94; 40 41 private boolean highQuality; 42 43 public String getSuggestedFileName() { 44 return highQuality ? "meshcms_hq_gallery.jpg" : "meshcms_gallery.jpg"; 45 } 46 47 protected boolean createThumbnail(File imageFile, File thumbnailFile) { 48 BufferedImage image; 49 50 try { 51 image = ImageIO.read(imageFile); 52 } catch (Exception ex) { 53 return false; 54 } 55 56 if (image == null || image.getWidth() < 1) { 57 return false; 58 } 59 60 BufferedImage thumb = new BufferedImage(THUMB_SIZE, THUMB_SIZE, 61 BufferedImage.TYPE_INT_RGB); 62 Graphics2D g = (Graphics2D) thumb.getGraphics(); 63 g.setColor(Color.WHITE); 64 g.fillRect(0, 0, THUMB_SIZE, THUMB_SIZE); 65 66 int w = image.getWidth(); 67 int h = image.getHeight(); 68 int ix, iy, w0, h0; 69 ix = iy = (THUMB_SIZE - THUMB_IMAGE_SIZE) / 2; 70 71 if (w <= THUMB_IMAGE_SIZE && h <= THUMB_IMAGE_SIZE) { 72 w0 = w; 73 h0 = h; 74 ix += (THUMB_IMAGE_SIZE - w0) / 2; 75 iy += (THUMB_IMAGE_SIZE - h0) / 2; 76 } else if (w > h) { 77 w0 = THUMB_IMAGE_SIZE; 78 h0 = w0 * h / w; 79 iy += (THUMB_IMAGE_SIZE - h0) / 2; 80 } else { 81 h0 = THUMB_IMAGE_SIZE; 82 w0 = h0 * w / h; 83 ix += (THUMB_IMAGE_SIZE - w0) / 2; 84 } 85 86 w0 = Math.max(w0, 1); 87 h0 = Math.max(h0, 1); 88 89 AbstractThumbnail.drawResizedImage(g, image, ix, iy, w0, h0, highQuality); 90 image.flush(); 91 g.setColor(DEFAULT_BORDER_COLOR); 92 g.drawRect(0, 0, THUMB_SIZE - 1, THUMB_SIZE - 1); 93 94 OutputStream os = null; 95 96 try { 97 os = new BufferedOutputStream(new FileOutputStream(thumbnailFile)); 98 ImageIO.write(thumb, "jpeg", os); 99 } catch (IOException ex) { 100 ex.printStackTrace(); 101 return false; 102 } finally { 103 thumb.flush(); 104 g.dispose(); 105 106 if (os != null) { 107 try { 108 os.close(); 109 } catch (IOException ex) {} 110 } 111 } 112 113 return true; 114 } 115 116 119 public boolean isHighQuality() { 120 return highQuality; 121 } 122 123 126 public void setHighQuality(boolean highQuality) { 127 this.highQuality = highQuality; 128 } 129 } 130 | Popular Tags |