【高效获取jpeg图片的尺寸】

获取图片的尺寸有三种方法:

  1.将整个图片文件加载成 BufferedImage 后获取其尺寸;

  2.用 ImageReader 快捷获取

  3.即windows xp sp3纯净安装版xp系统下载下文所陈述的方式

  做过简陋的测试,效率自 1 至 3 逐步递增。

  ImageInfo.java

  /**

  * @author kodeyang

  */

  public class ImageInfo {

  private int height;

  private int width;

  public ImageInfo(int height, int width) {

  super();

  this.height = height;

  this.width = width;

  }

  public int getWidth() {

  return width;

  }

  public int getHeight() {

  return height;

  }

  @Override

  public String toString() {

  return "height: " + height + ", width: " + width;

  }

  }

  import java.io.IOException;

  import java.io.InputStream;

  /**

  * @author yang3wei

  *

  */

  public class JpegInfoReader {

  private static final byte TAG_START = (byte) 0xff;

  private static final byte START_OF_IMAGE = (byte) 0xd8;

  private static final byte END_OF_IMAGE = (byte) 0xd9;

  private static final byte START_OF_FRAME = (byte) 0xc0;

  private static final byte RESTART_MODULO_START = (byte) 0xd0;

  private static final byte RESTART_MODULO_END = (byte) 0xd7;

  private static final byte START_OF_SCAN = (byte) 0xda;

  public static ImageInfo getImageInfo(InputStream in) throws IOException {

  // 0-1: store JPEG tag

  // 2-3: store JPEG tag length

  // 4-5: store JPEG image height

  // 6-7: store JPEG image width

  byte[] seg = new byte[8];

  // read JPEG START_OF_IMAGE tag

  if (in.read(seg, 0, 2) == -1) {

  return null;

  }

  // if the first two bytes is not 0xff, 0xd8,

  // that is the image format is not JPEG

  if (seg[0] != TAG_START || seg[1] != START_OF_IMAGE) {

  return null;

  }

  while (true) {

  // read JPEG data tag, offset 0 must be 0xff

  if (in.read(seg, 0, 2) == -1) {

  return null;

  }

  // if tag does not start with 0xff,

  // the image format is not JPEG

  if (seg[0] != TAG_START) {

  return null;

  }

  // Ignore JPEG RESTART_MODULO tag

  if (seg[1] >= RESTART_MODULO_START && seg[1] <= RESTART_MODULO_END) {

  continue;

  }

  // find JPEG format START_OF_SCAN part,

  // data that starts with poisition is JPEG compression image data,

  // that never contains image meta information

  if (seg[1] == START_OF_SCAN) {

  return null;

  }

  // find JPEG format END_OF_IMAGE tag, finish scan

  if (seg[1] == END_OF_IMAGE) {

  return null;

  }

  // read JPEG data tag length

  if (in.read(seg, 2, 2) == -1) {

  return null;

  }

  // find START_OF_FRAME tag

  if (seg[1] == START_OF_FRAME) {

  break;

  }

  // skip JPEG data segement

  byte[] skip = new byte[toInt(seg, 2) - 2];

  if (in.read(skip) == -1) {

  return null;

  }

  }

posted @ 2013-08-25 14:35  豆豆逗逗  阅读(305)  评论(0编辑  收藏  举报