KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > owxv3 > api > PictureProperties


1 /*
2  * eAdmin/OWX
3  * Copyright (C) 1996-2003 OWX-Project Team <owx-team@gmx.net>
4  */

5 package com.raptus.owxv3.api;
6
7 import java.io.*;
8
9 /**
10  *
11  * <hr>
12  * <table width="100%" border="0">
13  * <tr>
14  * <td width="24%"><b>Filename</b></td><td width="76%">PictureProperties</td>
15  * </tr>
16  * <tr>
17  * <td width="24%"><b>Author</b></td><td width="76%">REEA</td>
18  * </tr>
19  * <tr>
20  * <td width="24%"><b>Date</b></td><td width="76%">25th of September 2001</td>
21  * </tr>
22  * </table>
23  * <hr>
24  * <table width="100%" border="0">
25  * <tr>
26  * <td width="24%"><b>Date / Author</b></td><td width="76%"><b>Changes</b></td>
27  * </tr>
28  * </table>
29  * <hr>
30  * <table width="100%" border="0">
31  * <tr>
32  * <td>
33  * This class is a util class for getting the width and height of a picture.<br>
34  * It recognises the following formats: GIF,JPG,PNG,BMP
35  * </td>
36  * </tr>
37  * </table>
38  * <hr>
39  */

40
41
42 public class PictureProperties extends Object JavaDoc
43 {
44
45     private String JavaDoc fullpathtofile=null;
46     private String JavaDoc IMAGE_GIF=".gif";
47     private String JavaDoc IMAGE_JPG=".jpg";
48     private String JavaDoc IMAGE_PNG=".png";
49     private String JavaDoc IMAGE_BMP=".bmp";
50
51     private int width=0;
52     private int height=0;
53
54
55     /**
56      *The constructor, the filename with an absolute path should be provided
57      */

58     public PictureProperties(String JavaDoc file){
59         if(file.indexOf(".")==-1) return;
60
61         try{
62             String JavaDoc extension=file.substring( file.lastIndexOf(".") );
63
64             FileInputStream stream=new FileInputStream(file);
65             if(extension.compareToIgnoreCase(IMAGE_GIF)==0)
66             {
67
68                 //the image is gif
69

70
71                 setGIFProperties(stream);
72
73
74             }
75             else if(extension.compareToIgnoreCase(IMAGE_PNG)==0)
76             {
77                 //the image is png
78
setPNGProperties(stream);
79
80             }
81             else if(extension.compareToIgnoreCase(IMAGE_BMP)==0)
82             {
83                 //the image is bmp
84

85                 setBMPProperties(stream);
86
87             }
88              else if(extension.compareToIgnoreCase(IMAGE_JPG)==0)
89              {
90                 //the image is jpg
91

92                  setJPGProperties(stream);
93              }
94
95             stream.close();
96
97
98
99         }catch(Exception JavaDoc e){};
100
101     }//end constructor
102

103     /**
104      *Method for getting the width and height of a GIF image
105      */

106     private void setGIFProperties(FileInputStream stream) throws IOException
107     {
108
109          stream.skip(6);
110          int i=stream.read();
111          int j=stream.read();
112          width=j*256+i;
113
114          i=stream.read();
115          j=stream.read();
116          height=j*256+i;
117
118     }
119
120     /**
121      *Method for getting the width and height of a PNG image
122      */

123     private void setPNGProperties(FileInputStream stream) throws IOException
124     {
125          stream.skip(18);
126          int i=stream.read();
127          int j=stream.read();
128          width=i*256+j;
129
130          stream.skip(2);
131          i=stream.read();
132          j=stream.read();
133          height=i*256+j;
134     }
135
136     /**
137      *Method for getting the width and height of a BMP image
138      */

139     private void setBMPProperties(FileInputStream stream) throws IOException
140     {
141         stream.skip(18);
142         int i=stream.read();
143         int j=stream.read();
144         width=j*256+i;
145
146         stream.skip(2);
147         i=stream.read();
148         j=stream.read();
149         height=j*256+i;
150     }
151
152
153     /**
154      *Method for getting the width and height of a JPG image
155      */

156     private void setJPGProperties(FileInputStream stream) throws IOException
157     {
158         stream.skip(2);
159         byte[] data = new byte[6];
160         while (true) {
161                 if (stream.read(data, 0, 4) != 4) {
162                         return;
163                 }
164                 int marker = getShortBigEndian(data, 0);
165                 int size = getShortBigEndian(data, 2);
166                 if ((marker & 0xff00) != 0xff00) {
167                         return; // not a valid marker
168
}
169                 if (marker >= 0xffc0 && marker <= 0xffcf && marker != 0xffc4 && marker != 0xffc8) {
170                         if (stream.read(data) != 6) {
171                                 return;
172                         }
173
174                         width = getShortBigEndian(data, 3);
175                         height = getShortBigEndian(data, 1);
176                         return;
177                 } else {
178                         stream.skip(size - 2);
179                 }
180         }//end while
181
}//end setJPG
182

183
184     /**
185      *Method for getting the short BigEndian
186      */

187     private int getShortBigEndian(byte[] a, int offs) {
188         return
189             (a[offs] & 0xff) << 8 |
190             (a[offs + 1] & 0xff);
191     }
192
193
194     /**
195      *Method for getting the width of the supplied picture file
196      */

197     public int getWidth(){
198         return width;
199     }
200
201
202     /**
203      *Method for getting the height of the supplied picture file
204      */

205     public int getHeight(){
206         return height;
207     }
208 }//end class
209
Popular Tags