1 40 package org.dspace.app.mediafilter; 41 42 import java.awt.Graphics2D ; 43 import java.awt.image.BufferedImage ; 44 import java.io.ByteArrayInputStream ; 45 import java.io.ByteArrayOutputStream ; 46 import java.io.InputStream ; 47 48 import javax.imageio.ImageIO ; 49 50 import org.dspace.core.ConfigurationManager; 51 52 57 public class JPEGFilter extends MediaFilter 58 { 59 public String getFilteredName(String oldFilename) 60 { 61 return oldFilename + ".jpg"; 62 } 63 64 68 public String getBundleName() 69 { 70 return "THUMBNAIL"; 71 } 72 73 76 public String getFormatString() 77 { 78 return "JPEG"; 79 } 80 81 84 public String getDescription() 85 { 86 return "Generated Thumbnail"; 87 } 88 89 95 public InputStream getDestinationStream(InputStream source) 96 throws Exception 97 { 98 BufferedImage buf = ImageIO.read(source); 100 101 float xmax = (float) ConfigurationManager 103 .getIntProperty("thumbnail.maxwidth"); 104 float ymax = (float) ConfigurationManager 105 .getIntProperty("thumbnail.maxheight"); 106 107 float xsize = (float) buf.getWidth(null); 109 float ysize = (float) buf.getHeight(null); 110 111 if (MediaFilterManager.isVerbose) 114 { 115 System.out.println("original size: " + xsize + "," + ysize); 116 } 117 118 if (xsize > xmax) 120 { 121 float scale_factor = xmax / xsize; 123 124 if (MediaFilterManager.isVerbose) 127 { 128 System.out.println("x scale factor: " + scale_factor); 129 } 130 131 xsize = xsize * scale_factor; 134 ysize = ysize * scale_factor; 135 136 if (MediaFilterManager.isVerbose) 139 { 140 System.out.println("new size: " + xsize + "," + ysize); 141 } 142 } 143 144 if (ysize > ymax) 146 { 147 float scale_factor = ymax / ysize; 148 149 xsize = xsize * scale_factor; 152 ysize = ysize * scale_factor; 153 } 154 155 if (MediaFilterManager.isVerbose) 157 { 158 System.out.println("created thumbnail size: " + xsize + ", " 159 + ysize); 160 } 161 162 BufferedImage thumbnail = new BufferedImage ((int) xsize, (int) ysize, 164 BufferedImage.TYPE_INT_RGB); 165 166 Graphics2D g2d = thumbnail.createGraphics(); 168 g2d.drawImage(buf, 0, 0, (int) xsize, (int) ysize, null); 169 170 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 172 173 ImageIO.write(thumbnail, "jpeg", baos); 174 175 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 177 178 return bais; } 180 } 181 | Popular Tags |