KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JPEGFilter.java
3  *
4  * Version: $Revision: 1.8 $
5  *
6  * Date: $Date: 2005/07/29 15:56:07 $
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.io.ByteArrayInputStream JavaDoc;
45 import java.io.ByteArrayOutputStream JavaDoc;
46 import java.io.InputStream JavaDoc;
47
48 import javax.imageio.ImageIO JavaDoc;
49
50 import org.dspace.core.ConfigurationManager;
51
52 /**
53  * Filter image bitstreams, scaling the image to be within the bounds of
54  * thumbnail.maxwidth, thumbnail.maxheight, the size we want our thumbnail to be
55  * no bigger than. Creates only JPEGs.
56  */

57 public class JPEGFilter extends MediaFilter
58 {
59     public String JavaDoc getFilteredName(String JavaDoc oldFilename)
60     {
61         return oldFilename + ".jpg";
62     }
63
64     /**
65      * @return String bundle name
66      *
67      */

68     public String JavaDoc getBundleName()
69     {
70         return "THUMBNAIL";
71     }
72
73     /**
74      * @return String bitstreamformat
75      */

76     public String JavaDoc getFormatString()
77     {
78         return "JPEG";
79     }
80
81     /**
82      * @return String description
83      */

84     public String JavaDoc getDescription()
85     {
86         return "Generated Thumbnail";
87     }
88
89     /**
90      * @param source
91      * source input stream
92      *
93      * @return InputStream the resulting input stream
94      */

95     public InputStream JavaDoc getDestinationStream(InputStream JavaDoc source)
96             throws Exception JavaDoc
97     {
98         // read in bitstream's image
99
BufferedImage JavaDoc buf = ImageIO.read(source);
100
101         // get config params
102
float xmax = (float) ConfigurationManager
103                 .getIntProperty("thumbnail.maxwidth");
104         float ymax = (float) ConfigurationManager
105                 .getIntProperty("thumbnail.maxheight");
106
107         // now get the image dimensions
108
float xsize = (float) buf.getWidth(null);
109         float ysize = (float) buf.getHeight(null);
110
111         // if verbose flag is set, print out dimensions
112
// to STDOUT
113
if (MediaFilterManager.isVerbose)
114         {
115             System.out.println("original size: " + xsize + "," + ysize);
116         }
117
118         // scale by x first if needed
119
if (xsize > xmax)
120         {
121             // calculate scaling factor so that xsize * scale = new size (max)
122
float scale_factor = xmax / xsize;
123
124             // if verbose flag is set, print out extracted text
125
// to STDOUT
126
if (MediaFilterManager.isVerbose)
127             {
128                 System.out.println("x scale factor: " + scale_factor);
129             }
130
131             // now reduce x size
132
// and y size
133
xsize = xsize * scale_factor;
134             ysize = ysize * scale_factor;
135
136             // if verbose flag is set, print out extracted text
137
// to STDOUT
138
if (MediaFilterManager.isVerbose)
139             {
140                 System.out.println("new size: " + xsize + "," + ysize);
141             }
142         }
143
144         // scale by y if needed
145
if (ysize > ymax)
146         {
147             float scale_factor = ymax / ysize;
148
149             // now reduce x size
150
// and y size
151
xsize = xsize * scale_factor;
152             ysize = ysize * scale_factor;
153         }
154
155         // if verbose flag is set, print details to STDOUT
156
if (MediaFilterManager.isVerbose)
157         {
158             System.out.println("created thumbnail size: " + xsize + ", "
159                     + ysize);
160         }
161
162         // create an image buffer for the thumbnail with the new xsize, ysize
163
BufferedImage JavaDoc thumbnail = new BufferedImage JavaDoc((int) xsize, (int) ysize,
164                 BufferedImage.TYPE_INT_RGB);
165
166         // now render the image into the thumbnail buffer
167
Graphics2D JavaDoc g2d = thumbnail.createGraphics();
168         g2d.drawImage(buf, 0, 0, (int) xsize, (int) ysize, null);
169
170         // now create an input stream for the thumbnail buffer and return it
171
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
172
173         ImageIO.write(thumbnail, "jpeg", baos);
174
175         // now get the array
176
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
177
178         return bais; // hope this gets written out before its garbage collected!
179
}
180 }
181
Popular Tags