I am working on my HTTP server, and I am currently implementing the ability to read and display media tags and information of files(such as mp4, m4a, wav, etc.) to clients. So far I have tags such as title, track number, year, album, artist, copyright, etc. working perfectly for multiple file extensions using JAudioTagger(binaries available here, website here).
What I am now trying to do is implement the ability to read and convert the image data, or album artwork/cover data, and send that data to clients as png, jpeg, etc. separately. I have visited and read the official section on the APIC tag here, but I can't figure out how to convert the data or where the data actually starts in the tag.
Here is the code I wrote to retrieve the album artwork data from a file containing it:
public static final byte[] readFileArtwork(File file) {
if(file == null || !file.isFile()) {
return null;
}
AudioFile afile = null;
try {
afile = AudioFileIO.read(file);
} catch(CannotReadException e) {
System.err.print("Unable to read file: ");
e.printStackTrace();
} catch(IOException e) {
System.err.print("An I/O Exception occurred: ");
e.printStackTrace();
} catch(TagException e) {
System.err.print("Unable to read file's tag data: ");
e.printStackTrace();
} catch(ReadOnlyFileException e) {//???
System.err.print("Unable to read file: File is read only: ");
e.printStackTrace();
} catch(InvalidAudioFrameException e) {
System.err.print("Unable to read file's audio frame data: ");
e.printStackTrace();
}
byte[] data = new byte[0];
if(afile == null) {
return data;
}
Iterator<TagField> tags = afile.getTag().getFields();
while(tags.hasNext()) {
TagField tag = tags.next();
if(tag.isBinary()) {
if(tag.getId().equals("APIC")) {
try {
data = tag.getRawContent();
} catch(UnsupportedEncodingException e) {
System.err.print("Unable to read file's image data: ");
e.printStackTrace();
}
}
}
}
return data == null ? new byte[0] : data;
}
Did you take a look at the Artwork support in JAudioTagger and the Java ImageIO class? To get access to the pictures and convert them to PNGs you should be able to do something like:
for (Artwork artwork : afile.getTag().getArtworkList()) {
BufferedImage bi = (BufferedImage) artwork.getImage();
ImageIO.write(bi, "png", outputstream);
}
The getPictureType(), getMimeType(), etc. accessors on Artwork can be used to access the other picture metadata described in the ID3 spec.