1 40 package org.dspace.app.mediafilter; 41 42 import java.awt.Graphics2D ; 43 import java.awt.image.BufferedImage ; 44 import java.awt.Font ; 45 import java.io.ByteArrayInputStream ; 46 import java.io.ByteArrayOutputStream ; 47 import java.io.InputStream ; 48 49 import org.dspace.content.Bitstream; 50 import org.dspace.content.BitstreamFormat; 51 import org.dspace.content.Bundle; 52 import org.dspace.content.Item; 53 import org.dspace.core.Context; 54 55 import javax.imageio.ImageIO ; 56 57 import org.dspace.core.ConfigurationManager; 58 59 64 public class BrandedPreviewJPEGFilter extends MediaFilter 65 { 66 public String getFilteredName(String oldFilename) 67 { 68 return oldFilename + ".preview.jpg"; 69 } 70 71 75 public String getBundleName() 76 { 77 return "BRANDED_PREVIEW"; 78 } 79 80 83 public String getFormatString() 84 { 85 return "JPEG"; 86 } 87 88 91 public String getDescription() 92 { 93 return "Generated Branded Preview"; 94 } 95 96 97 103 public InputStream getDestinationStream(InputStream source) 104 throws Exception 105 { 106 BufferedImage buf = ImageIO.read(source); 108 109 float xmax = (float) ConfigurationManager 111 .getIntProperty("webui.preview.maxwidth"); 112 float ymax = (float) ConfigurationManager 113 .getIntProperty("webui.preview.maxheight"); 114 int brandHeight = ConfigurationManager.getIntProperty("webui.preview.brand.height"); 115 String brandFont = ConfigurationManager.getProperty("webui.preview.brand.font"); 116 int brandFontPoint = ConfigurationManager.getIntProperty("webui.preview.brand.fontpoint"); 117 118 float xsize = (float) buf.getWidth(null); 120 float ysize = (float) buf.getHeight(null); 121 122 if (MediaFilterManager.isVerbose) 125 { 126 System.out.println("original size: " + xsize + "," + ysize); 127 } 128 129 if (xsize > xmax) 131 { 132 float scale_factor = xmax / xsize; 134 135 if (MediaFilterManager.isVerbose) 138 { 139 System.out.println("x scale factor: " + scale_factor); 140 } 141 142 xsize = xsize * scale_factor; 145 ysize = ysize * scale_factor; 146 147 if (MediaFilterManager.isVerbose) 150 { 151 System.out.println("new size: " + xsize + "," + ysize); 152 } 153 } 154 155 if (ysize > ymax) 157 { 158 float scale_factor = ymax / ysize; 159 160 xsize = xsize * scale_factor; 163 ysize = ysize * scale_factor; 164 } 165 166 if (MediaFilterManager.isVerbose) 168 { 169 System.out.println("created thumbnail size: " + xsize + ", " 170 + ysize); 171 } 172 173 BufferedImage branded = new BufferedImage ((int) xsize, (int) ysize + brandHeight, 176 BufferedImage.TYPE_INT_RGB); 177 178 Graphics2D g2d = branded.createGraphics(); 180 g2d.drawImage(buf, 0, 0, (int) xsize, (int) ysize, null); 181 182 Brand brand = new Brand((int) xsize, brandHeight, new Font (brandFont, Font.PLAIN, brandFontPoint), 5); 183 BufferedImage brandImage = brand.create(ConfigurationManager.getProperty("webui.preview.brand"), 184 ConfigurationManager.getProperty("webui.preview.brand.abbrev"), 185 item == null ? "" : "hdl:" + item.getHandle()); 186 187 g2d.drawImage(brandImage, (int)0, (int)ysize, (int) xsize, (int) 20, null); 188 189 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 191 192 ImageIO.write(branded, "jpeg", baos); 193 194 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 196 197 return bais; } 199 } 200 | Popular Tags |