1 package com.imcode.util;
2
3 import javax.imageio.stream.ImageInputStream;
4 import javax.imageio.ImageIO;
5 import javax.imageio.ImageReader;
6 import java.io.InputStream;
7 import java.io.IOException;
8 import java.io.Serializable;
9 import java.util.Iterator;
10
11 public class ImageSize implements Serializable {
12
13 private int width;
14 private int height;
15
16 public ImageSize( int width, int height ) {
17 this.width = width;
18 this.height = height;
19 }
20
21 public int getWidth() {
22 return width ;
23 }
24
25 public int getHeight() {
26 return height ;
27 }
28
29 public String toString() {
30 return width+"*"+height ;
31 }
32
33 public static ImageSize fromInputStream( InputStream inputStream ) throws IOException {
34 ImageInputStream imageInputStream = ImageIO.createImageInputStream( inputStream );
35 Iterator imageReadersIterator = ImageIO.getImageReaders( imageInputStream );
36 if ( !imageReadersIterator.hasNext() ) {
37 throw new IOException( "Can't read image format." ) ;
38 }
39 ImageReader imageReader = (ImageReader)imageReadersIterator.next();
40 imageReader.setInput( imageInputStream, true, true );
41 int width = imageReader.getWidth( 0 );
42 int height = imageReader.getHeight( 0 );
43 imageReader.dispose();
44 return new ImageSize( width, height );
45 }
46 }