public class ImageDecoder {
private static final ByteString JPEG_START_MARKER = ByteString.decodeHex("FF");
private static final ByteString JPEG_BASELINE_MARKER = ByteString.decodeHex("C0");
private static final ByteString JPEG_PROGRESSIVE_MARKER = ByteString.decodeHex("C2");
public Dimension decodeJpegDimension(InputStream in) throws IOException {
// Jpeg stores in big endian
BufferedSource jpegSource = Okio.buffer(Okio.source(in));
Dimension dimension;
while (true) {
ByteString marker = jpegSource.readByteString(JPEG_START_MARKER.size());
if (!marker.equals(JPEG_START_MARKER))
continue;
marker = jpegSource.readByteString(JPEG_START_MARKER.size());
if (marker.equals(JPEG_BASELINE_MARKER) || marker.equals(JPEG_PROGRESSIVE_MARKER)) {
jpegSource.skip(3);
Short h = jpegSource.readShort();
Short w = jpegSource.readShort();
if(h < 0 || w < 0) {
throw new InvalidPropertiesFormatException("Invalid width and height");
}
dimension = new Dimension(Integer.valueOf(w), Integer.valueOf(h));
break;
}
}
return dimension;
}
public Dimension decodePngDimension(InputStream in) throws IOException {
// Png stores in big endian
BufferedSource pngSource = Okio.buffer(Okio.source(in));
pngSource.skip(16);
int w = pngSource.readInt();
int h = pngSource.readInt();
return new Dimension(w, h);
}
public Dimension decodeBmpDimension(InputStream in) throws IOException {
// Bmp stores in little endian
BufferedSource bmpSource = Okio.buffer(Okio.source(in));
bmpSource.skip(18);
int w = bmpSource.readIntLe();
int h = bmpSource.readIntLe();
return new Dimension(w, h);
}
public Dimension decodeGifDimension(InputStream in) throws IOException {
// Gif stores in little endian
BufferedSource gifSource = Okio.buffer(Okio.source(in));
gifSource.skip(6);
Short w = gifSource.readShortLe();
Short h = gifSource.readShortLe();
return new Dimension(Integer.valueOf(w), Integer.valueOf(h));
}
}
解析图片片段信息,获取图片尺寸
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 有时候,我们需要获取一个图片的格式和大小,但是却不想从内存中把它的整个文件从内存中读取出来 因为读取整个文件的内存...
- 第一种方法:直接调用SDWebImage里面的方法进行加载然后拿到图片尺寸 第二种方法:一行代码获取图片尺寸: 在...
- 问题描述 在自适应高度的时候,由于图片尺寸不固定,导致整体高度无法计算。 解决方案 必须先拿到图片尺寸,再进行计算...