KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > mediafilter > BrandedPreviewJPEGFilter


1 /*
2  * BrandedPreviewJPEGFilter.java
3  *
4  * Version: $Revision: 1.1 $
5  *
6  * Date: $Date: 2006/03/10 20:30:03 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.app.mediafilter;
41
42 import java.awt.Graphics2D JavaDoc;
43 import java.awt.image.BufferedImage JavaDoc;
44 import java.awt.Font JavaDoc;
45 import java.io.ByteArrayInputStream JavaDoc;
46 import java.io.ByteArrayOutputStream JavaDoc;
47 import java.io.InputStream JavaDoc;
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 JavaDoc;
56
57 import org.dspace.core.ConfigurationManager;
58
59 /**
60  * Filter image bitstreams, scaling the image to be within the bounds of
61  * thumbnail.maxwidth, thumbnail.maxheight, the size we want our thumbnail to be
62  * no bigger than. Creates only JPEGs.
63  */

64 public class BrandedPreviewJPEGFilter extends MediaFilter
65 {
66     public String JavaDoc getFilteredName(String JavaDoc oldFilename)
67     {
68         return oldFilename + ".preview.jpg";
69     }
70
71     /**
72      * @return String bundle name
73      *
74      */

75     public String JavaDoc getBundleName()
76     {
77         return "BRANDED_PREVIEW";
78     }
79
80     /**
81      * @return String bitstreamformat
82      */

83     public String JavaDoc getFormatString()
84     {
85         return "JPEG";
86     }
87
88     /**
89      * @return String description
90      */

91     public String JavaDoc getDescription()
92     {
93         return "Generated Branded Preview";
94     }
95
96    
97     /**
98      * @param source
99      * source input stream
100      *
101      * @return InputStream the resulting input stream
102      */

103     public InputStream JavaDoc getDestinationStream(InputStream JavaDoc source)
104             throws Exception JavaDoc
105     {
106         // read in bitstream's image
107
BufferedImage JavaDoc buf = ImageIO.read(source);
108
109         // get config params
110
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 JavaDoc brandFont = ConfigurationManager.getProperty("webui.preview.brand.font");
116         int brandFontPoint = ConfigurationManager.getIntProperty("webui.preview.brand.fontpoint");
117         
118         // now get the image dimensions
119
float xsize = (float) buf.getWidth(null);
120         float ysize = (float) buf.getHeight(null);
121
122         // if verbose flag is set, print out dimensions
123
// to STDOUT
124
if (MediaFilterManager.isVerbose)
125         {
126             System.out.println("original size: " + xsize + "," + ysize);
127         }
128
129         // scale by x first if needed
130
if (xsize > xmax)
131         {
132             // calculate scaling factor so that xsize * scale = new size (max)
133
float scale_factor = xmax / xsize;
134
135             // if verbose flag is set, print out extracted text
136
// to STDOUT
137
if (MediaFilterManager.isVerbose)
138             {
139                 System.out.println("x scale factor: " + scale_factor);
140             }
141
142             // now reduce x size
143
// and y size
144
xsize = xsize * scale_factor;
145             ysize = ysize * scale_factor;
146
147             // if verbose flag is set, print out extracted text
148
// to STDOUT
149
if (MediaFilterManager.isVerbose)
150             {
151                 System.out.println("new size: " + xsize + "," + ysize);
152             }
153         }
154
155         // scale by y if needed
156
if (ysize > ymax)
157         {
158             float scale_factor = ymax / ysize;
159
160             // now reduce x size
161
// and y size
162
xsize = xsize * scale_factor;
163             ysize = ysize * scale_factor;
164         }
165
166         // if verbose flag is set, print details to STDOUT
167
if (MediaFilterManager.isVerbose)
168         {
169             System.out.println("created thumbnail size: " + xsize + ", "
170                     + ysize);
171         }
172
173         // create an image buffer for the preview with the new xsize, ysize
174
// we add
175
BufferedImage JavaDoc branded = new BufferedImage JavaDoc((int) xsize, (int) ysize + brandHeight,
176                 BufferedImage.TYPE_INT_RGB);
177
178         // now render the image into the preview buffer
179
Graphics2D JavaDoc 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 JavaDoc(brandFont, Font.PLAIN, brandFontPoint), 5);
183         BufferedImage JavaDoc 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         // now create an input stream for the thumbnail buffer and return it
190
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
191
192         ImageIO.write(branded, "jpeg", baos);
193
194         // now get the array
195
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
196
197         return bais; // hope this gets written out before its garbage collected!
198
}
199 }
200
Popular Tags