-
Notifications
You must be signed in to change notification settings - Fork 2
/
ShowImage.pde
318 lines (293 loc) · 9.14 KB
/
ShowImage.pde
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// show cover image from MP3
final boolean DEBUG = true;
String[] PICTURE_TYPES =
{
"Other",
"32x32 pixels 'file icon' (PNG only)",
"Other file icon",
"Cover (front)",
"Cover (back)",
"Leaflet page",
"Media (e.g. label side of CD)",
"Lead artist/lead performer/soloist",
"Artist/performer",
"Conductor",
"Band/Orchestra",
"Composer",
"Lyricist/text writer",
"Recording Location",
"During recording",
"During performance",
"Movie/video screen capture",
"A bright coloured fish",
"Illustration",
"Band/artist logotype",
"Publisher/Studio logotype"
};
void tryToShowCoverImage()
{
final String path = namesFiles[indexFile];
File file = new File(path);
try
{
InputStream is = new FileInputStream(file);
BufferedImage image = getID3Image(is);
is.close();
if (image != null)
{
mp3Image = new PImage(image.getWidth(), image.getHeight(), PConstants.ARGB);
image.getRGB(0, 0, mp3Image.width, mp3Image.height, mp3Image.pixels, 0, mp3Image.width);
mp3Image.updatePixels();
mp3Image.resize(200, 0);
}
else
println("image was null");
}
catch (IOException e)
{
println(e);
}
}
// Adapted from http://supportforums.blackberry.com/t5/Java-Development/how-to-retrieve-ID3-art-Image-from-an-mp3-file/m-p/358490#M67015
// Minor fixes / improvements, and getting rid of the RIM Blackberry dependency by PhiLho.
BufferedImage getID3Image(InputStream dataStream)
throws UnsupportedEncodingException, IOException
{
/*---------------------------
* ID3 v1 tags cannot contain images, so no need to process them.
* ID3 v1 tags are also placed at the end of the MP3 so checking the beginning of the file won't do anything useful.
*---------------------------
*/
// Read the tags, searching for the album artwork
long currentPosition = 0;
byte[] imageData = null;
String mimeType = null;
byte[] buffer = new byte[10];
if (dataStream.read(buffer, 0, 10) != 10 || !(new String(buffer, 0, 3).equals("ID3")))
{
// Not on start of ID3 data, nothing to find, just don't try
if (DEBUG)
System.out.println("Not ID3");
return null;
}
currentPosition += 10;
// Found a ID3 version 2 or greater tag
// Now to actually parse a tag
int majorVersion = buffer[3] & 0xFF;
byte minorVersion = buffer[4];
byte[] destinationArray = new byte[4];
System.arraycopy(buffer, 6, destinationArray, 0, 4);
// Read a 28-bit int for size
int size = read28bit(destinationArray);
long end = currentPosition + size;
long dataLength = end - 11L;
if (DEBUG) System.out.println("Version: " + majorVersion + "." + minorVersion);
boolean v2_2 = false;
if (majorVersion == 2)
{
// ID3 v2.2
v2_2 = true;
}
else if (majorVersion == 3 || majorVersion == 4)
{
// ID3 v2.3 / ID3 v2.4
// Extra data seems might exist, go through
boolean hasExtendedHeader = (buffer[5] & 0x40) == 0x40;
if (hasExtendedHeader)
{
byte[] extendedHeaderBuffer = new byte[4];
dataStream.read(extendedHeaderBuffer, 0, 4);
currentPosition += 4;
int extendedHeaderLength = read32bit(extendedHeaderBuffer);
byte[] extendedHeaderData = new byte[extendedHeaderLength + 4];
System.arraycopy(extendedHeaderBuffer, 0, extendedHeaderData, 4, extendedHeaderLength);
dataStream.read(extendedHeaderData, 4, extendedHeaderLength);
currentPosition += extendedHeaderLength;
// No use for this data in the picture so just ignore it
}
v2_2 = false;
}
while (currentPosition < dataLength)
{
// Get the frame header and make sure that it is a valid frame.
byte[] frameBuffer = new byte[v2_2 ? 6 : 10];
if (dataStream.read(frameBuffer, 0, frameBuffer.length) != frameBuffer.length || (frameBuffer[0] & 0xFF) <= 0)
break;
currentPosition += frameBuffer.length;
String frameId = new String(frameBuffer, 0, v2_2 ? 3 : 4);
if (DEBUG)
System.out.println("Found frame " + frameId);
destinationArray = new byte[v2_2 ? 3 : 4];
System.arraycopy(frameBuffer, destinationArray.length, destinationArray, 0, destinationArray.length);
int frameSize = 0;
switch (majorVersion)
{
case 2:
// 24-bit
frameSize = read24bit(destinationArray);
break;
case 3:
// 32-bit
frameSize = read32bit(destinationArray);
break;
case 4:
// 28-bit
frameSize = read28bit(destinationArray);
break;
default:
continue;
}
// Now read the data and check to see if it is a picture
frameBuffer = new byte[frameSize];
if (dataStream.read(frameBuffer, 0, frameSize) == frameSize)
{
currentPosition += frameSize;
if (frameId.equals("PIC") || frameId.equals("APIC"))
{
// Got the frame data
int refPoint = 0;
// First we get the encoding type
int encodingType = (frameBuffer[refPoint++] & 0xFF); // 0=ISO-8859-1, 1=UTF-16, 2=UTF-16BE, 3=UTF-8
if (encodingType < 0 || encodingType > 3)
// Do we need to stop reading the image because we can't get the description?
// On the other hand, it should not happen anyway, perhaps the data is corrupt?
throw new UnsupportedEncodingException("Cannot get picture description. Frame Encoding is invalid.");
// Second we get the mime type
int indexPoint = refPoint;
while (frameBuffer[refPoint++] != 0)
;
int mimeLength = refPoint - indexPoint;
if (mimeLength > 1)
{
mimeType = new String(frameBuffer, indexPoint, mimeLength - 1);
if (DEBUG) System.out.println("Mime type: " + mimeType);
}
// Third we get the picture type
int pictureType = frameBuffer[refPoint++] & 0xFF;
if (DEBUG) System.out.println("Picture type: " + pictureType + " - " + PICTURE_TYPES[pictureType]);
// Fourth we load the picture description
// Check length
int scanPoint = refPoint;
switch (encodingType)
{
case 0:
case 3:
// 8-bit string
while (scanPoint < frameBuffer.length && frameBuffer[scanPoint++] != 0)
;
break;
case 1:
case 2:
// 16-bit string
do
{
byte b1 = frameBuffer[scanPoint++];
byte b2 = frameBuffer[scanPoint++];
if (b1 == 0 && b2 == 0)
break;
}
while (scanPoint < frameBuffer.length - 1);
break;
}
// And read the string
int descriptionLength = scanPoint - refPoint;
byte[] descriptionBuffer = new byte[descriptionLength];
int pos = 0;
switch (encodingType)
{
case 0:
case 3:
// 8-bit string
while (pos < descriptionLength)
{
descriptionBuffer[pos++] = frameBuffer[refPoint++];
}
break;
case 1:
case 2:
// 16-bit string
do
{
byte b1 = frameBuffer[refPoint++];
byte b2 = frameBuffer[refPoint++];
if (encodingType == 1 && b1 == 0xFF && b2 == 0xFE)
continue; // Skip BOM
descriptionBuffer[pos++] = b1;
descriptionBuffer[pos++] = b2;
}
while (pos < descriptionLength);
break;
}
String encoding = null;
switch (encodingType)
{
case 0:
encoding = "ISO-8859-1";
break;
case 1:
encoding = "UTF-16";
break;
case 2:
encoding = "UTF-16BE";
break;
case 3:
encoding = "UTF-8";
break;
}
String description = new String(descriptionBuffer, encoding);
if (DEBUG) System.out.println("Description: " + description);
// Finally, THE MAIN EVENT, the image data
int imageSize = frameBuffer.length - refPoint;
imageData = new byte[imageSize];
System.arraycopy(frameBuffer, refPoint, imageData, 0, imageSize);
break;
}
}
}
if (imageData != null)
{
//We found the image
System.out.println("Got image: " + mimeType + " / " + imageData.length + " bytes");
/*
if (mimeType != null && mimeType.length() > 0)
{
// Save some time in searching for image type
return net.rim.device.api.system.EncodedImage.createEncodedImage(imageData, 0, imageData.length, mimeType);
}
else
{
return net.rim.device.api.system.EncodedImage.createEncodedImage(imageData, 0, imageData.length);
}
*/
InputStream imageStream = new ByteArrayInputStream(imageData);
BufferedImage image = ImageIO.read(imageStream);
imageStream.close();
return image;
}
// No image found
return null;
}
int read32bit(byte[] data)
{
return
((data[0] & 0xFF) << 24) |
((data[1] & 0xFF) << 16) |
((data[2] & 0xFF) << 8) |
(data[3] & 0xFF);
}
int read28bit(byte[] data)
{
return
((data[0] & 0xFF) << 21) |
((data[1] & 0xFF) << 14) |
((data[2] & 0xFF) << 7) |
(data[3] & 0xFF);
}
int read24bit(byte[] data)
{
return
((data[1] & 0xFF) << 16) |
((data[2] & 0xFF) << 8) |
(data[3] & 0xFF);
}
//