1 5 package com.raptus.owxv3.api; 6 7 import java.io.*; 8 9 40 41 42 public class PictureProperties extends Object 43 { 44 45 private String fullpathtofile=null; 46 private String IMAGE_GIF=".gif"; 47 private String IMAGE_JPG=".jpg"; 48 private String IMAGE_PNG=".png"; 49 private String IMAGE_BMP=".bmp"; 50 51 private int width=0; 52 private int height=0; 53 54 55 58 public PictureProperties(String file){ 59 if(file.indexOf(".")==-1) return; 60 61 try{ 62 String extension=file.substring( file.lastIndexOf(".") ); 63 64 FileInputStream stream=new FileInputStream(file); 65 if(extension.compareToIgnoreCase(IMAGE_GIF)==0) 66 { 67 68 70 71 setGIFProperties(stream); 72 73 74 } 75 else if(extension.compareToIgnoreCase(IMAGE_PNG)==0) 76 { 77 setPNGProperties(stream); 79 80 } 81 else if(extension.compareToIgnoreCase(IMAGE_BMP)==0) 82 { 83 85 setBMPProperties(stream); 86 87 } 88 else if(extension.compareToIgnoreCase(IMAGE_JPG)==0) 89 { 90 92 setJPGProperties(stream); 93 } 94 95 stream.close(); 96 97 98 99 }catch(Exception e){}; 100 101 } 103 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 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 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 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; } 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 } } 183 184 187 private int getShortBigEndian(byte[] a, int offs) { 188 return 189 (a[offs] & 0xff) << 8 | 190 (a[offs + 1] & 0xff); 191 } 192 193 194 197 public int getWidth(){ 198 return width; 199 } 200 201 202 205 public int getHeight(){ 206 return height; 207 } 208 } | Popular Tags |