class="java" name="code">package test; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Iterator; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; public class bbb { public static void main(String[] args) throws FileNotFoundException, IOException { try { //方法1: File file = new File("d:/ant_logo_large.gif"); Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("gif"); ImageReader reader = (ImageReader)readers.next(); ImageInputStream iis = ImageIO.createImageInputStream(file); reader.setInput(iis, true); System.out.println("width:"+reader.getWidth(0)); System.out.println("height:"+reader.getHeight(0)); //方法2: File picture = new File("d:/ant_logo_large.gif"); BufferedImage sourceImg =ImageIO.read(new FileInputStream(picture)); System.out.println(sourceImg.getWidth()); System.out.println(sourceImg.getHeight()); } catch (IOException e) { e.printStackTrace(); } } }
?链接:http://immortal2512.iteye.com/blog/406744