Image from char data

Hey.

I’ve been playing around with the TagLib library trying to extract album artwork from a file. I’m trying to build a music file browser.

I’ve managed to extract the char data from the file, and if I save it to a .txt (and then rename it .JPG) I am able to get the artwork I’m after.

My question is: how can I use this to display the artwork image within JUCE?

Here’s some code if its useful to anyone else:

// Adapted from:
// http://stackoverflow.com/questions/4752020/how-do-i-use-taglib-to-read-write-coverart-in-different-audio-formats

using namespace TagLib::ID3v2;

TagLib::MPEG::File mp3File(file.getFullPathName().toRawUTF8());
Tag * mp3Tag;
FrameList listOfMp3Frames;
AttachedPictureFrame * pictureFrame;

mp3Tag = mp3File.ID3v2Tag();

if(mp3Tag)
{
    listOfMp3Frames = mp3Tag->frameListMap()["APIC"];
    if(!listOfMp3Frames.isEmpty())
    {
        FrameList::ConstIterator it= listOfMp3Frames.begin();
        for(; it != listOfMp3Frames.end() ; it++)
        {
            pictureFrame = static_cast<AttachedPictureFrame *> (*it);
            
            File artwork;
            artwork.replaceWithData(pictureFrame->picture().data(), pictureFrame->picture().size());
            
        }
    }
    else
    {
        printf("No Artwork in File\n");
    }
}

You can read the data into an juce Image via ImageCache and draw in the paint routine using the given Graphics context:

Image myImage = ImageCache::getFromMemory (pictureFrame->picture().data(), 
                                           pictureFrame->picture().size());
g.drawImageAt (myImage, 0, 0);

HTH

2 Likes