-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleImageInfo.java
More file actions
158 lines (139 loc) · 4.66 KB
/
SimpleImageInfo.java
File metadata and controls
158 lines (139 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.image.info;
import java.io.*;
public class SimpleImageInfo {
private int height;
private int width;
private long size;
private String mimeType;
public SimpleImageInfo(File file) throws IOException {
try (InputStream is = new FileInputStream(file)) {
size = file.length();
processStream(is);
} catch (IOException ex) {
System.out.println("Couldn't get size,width,height for " + file.getName());
}
}
public SimpleImageInfo(byte[] bytes) throws IOException {
try (InputStream is = new ByteArrayInputStream(bytes)) {
size = bytes.length;
processStream(is);
}
}
private void processStream(InputStream is) throws IOException {
int c1 = is.read();
int c2 = is.read();
int c3 = is.read();
mimeType = null;
width = height = -1;
if (c1 == 'G' && c2 == 'I' && c3 == 'F') { // GIF
is.skip(3);
width = readInt(is, 2, false);
height = readInt(is, 2, false);
mimeType = "image/gif";
} else if (c1 == 0xFF && c2 == 0xD8) { // JPG
while (c3 == 255) {
int marker = is.read();
int len = readInt(is, 2, true);
if (marker == 192 || marker == 193 || marker == 194) {
is.skip(1);
height = readInt(is, 2, true);
width = readInt(is, 2, true);
mimeType = "image/jpeg";
break;
}
is.skip(len - 2);
c3 = is.read();
}
} else if (c1 == 137 && c2 == 80 && c3 == 78) { // PNG
is.skip(15);
width = readInt(is, 2, true);
is.skip(2);
height = readInt(is, 2, true);
mimeType = "image/png";
} else if (c1 == 66 && c2 == 77) { // BMP
is.skip(15);
width = readInt(is, 2, false);
is.skip(2);
height = readInt(is, 2, false);
mimeType = "image/bmp";
} else {
int c4 = is.read();
if ((c1 == 'M' && c2 == 'M' && c3 == 0 && c4 == 42)
|| (c1 == 'I' && c2 == 'I' && c3 == 42 && c4 == 0)) { // TIFF
boolean bigEndian = c1 == 'M';
int ifd = 0;
int entries;
ifd = readInt(is, 4, bigEndian);
is.skip(ifd - 8);
entries = readInt(is, 2, bigEndian);
for (int i = 1; i <= entries; i++) {
int tag = readInt(is, 2, bigEndian);
int fieldType = readInt(is, 2, bigEndian);
long count = readInt(is, 4, bigEndian);
int valOffset;
if ((fieldType == 3 || fieldType == 8)) {
valOffset = readInt(is, 2, bigEndian);
is.skip(2);
} else {
valOffset = readInt(is, 4, bigEndian);
}
if (tag == 256) {
width = valOffset;
} else if (tag == 257) {
height = valOffset;
}
if (width != -1 && height != -1) {
mimeType = "image/tiff";
break;
}
}
}
}
if (mimeType == null) {
throw new IOException("Unsupported image type");
}
}
private int readInt(InputStream is, int noOfBytes, boolean bigEndian) throws IOException {
int ret = 0;
int sv = bigEndian ? ((noOfBytes - 1) * 8) : 0;
int cnt = bigEndian ? -8 : 8;
for (int i = 0; i < noOfBytes; i++) {
ret |= is.read() << sv;
sv += cnt;
}
return ret;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public String getMimeType() {
return mimeType;
}
public void setMimeType(String mimeType) {
this.mimeType = mimeType;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
@Override
public String toString() {
return "SimpleImageInfo{" +
"height=" + height +
", width=" + width +
", size=" + size +
", mimeType='" + mimeType + '\'' +
'}';
}
}