獲取圖片的尺寸有三種方法
做過簡陋的測試
ImageInfo
/**
* @author kodeyang
*/
public class ImageInfo {
private int height;
private int width;
public ImageInfo(int height
super();
this
this
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
@Override
public String toString() {
return
}
}
import java
import java
/**
* @author yang
*
*/
public class JpegInfoReader {
private static final byte TAG_START = (byte)
private static final byte START_OF_IMAGE = (byte)
private static final byte END_OF_IMAGE = (byte)
private static final byte START_OF_FRAME = (byte)
private static final byte RESTART_MODULO_START = (byte)
private static final byte RESTART_MODULO_END = (byte)
private static final byte START_OF_SCAN = (byte)
public static ImageInfo getImageInfo(InputStream in) throws IOException {
//
//
//
//
byte[] seg = new byte[
// read JPEG START_OF_IMAGE tag
if (in
return null;
}
// if the first two bytes is not
// that is the image format is not JPEG
if (seg[
return null;
}
while (true) {
// read JPEG data tag
if (in
return null;
}
// if tag does not start with
// the image format is not JPEG
if (seg[
return null;
}
// Ignore JPEG RESTART_MODULO tag
if (seg[
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[
return null;
}
// find JPEG format END_OF_IMAGE tag
if (seg[
return null;
}
// read JPEG data tag length
if (in
return null;
}
// find START_OF_FRAME tag
if (seg[
break;
}
// skip JPEG data segement
byte[] skip = new byte[toInt(seg
if (in
return null;
}
}
// ignore JPEG image precision byte
if (in
return null;
}
// read JPEG image height and width bytes
if (in
return null;
}
return new ImageInfo(toInt(seg
}
private static int toInt(byte[] bys
return ((bys[start] &
}
}
import java
import java
import java
/**
* @author yang
*
*/
public class JpegInfoReaderUsage {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
InputStream in = null;
try {
in = new FileInputStream(
ImageInfo info = JpegInfoReader
System
} finally {
if (in != null) {
in
}
}
}
}
提問
回答
如果有需要使用到外部類中數據的話
在面向對象程序設計中
為什麼 int[
我很能理解你認為返回 int[] 的理由是什麼
實際上並不是這樣的
在 HotSpot 的 JVM 中一個 new 操作只占用了十幾個 CPU 指令的時間
另外
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25816.html