Exemplo n.º 1
0
/** \fn     ImageUtils::GetAllExifValues(const QString &)
 *  \brief  Reads and returns all non empty tags from the given file
 *  \param  fileName The filename that holds the exif data
 *  \return The list of exif tag names and values
 */
QList<QStringList> ImageUtils::GetAllExifValues(const QString &fileName)
{
    // default value, an empty list means no
    // exif tags were found or an error occured
    QList<QStringList> valueList;

    try
    {
        Exiv2::Image::AutoPtr image =
                Exiv2::ImageFactory::open(fileName.toLocal8Bit().constData());

        if (image.get())
        {
            image->readMetadata();
            Exiv2::ExifData &exifData = image->exifData();
            if (!exifData.empty())
            {
                LOG(VB_GENERAL, LOG_DEBUG,
                    QString("Found %1 tag(s) for file %2")
                    .arg(exifData.count())
                    .arg(fileName));

                Exiv2::ExifData::const_iterator end = exifData.end();
                Exiv2::ExifData::const_iterator i = exifData.begin();
                for (; i != end; ++i)
                {
                    QString value = QString::fromStdString(i->value().toString());

                    // Do not add empty tags to the list
                    if (!value.isEmpty())
                    {
                        QStringList values;

                        // These three are the same as i->key()
                        values.append(QString::fromStdString(i->familyName()));
                        values.append(QString::fromStdString(i->groupName()));
                        values.append(QString::fromStdString(i->tagName()));
                        values.append(QString::fromStdString(i->key()));
                        values.append(QString::fromStdString(i->tagLabel()));
                        values.append(QString::fromStdString(i->value().toString()));

                        // Add the exif information to the list
                        valueList.append(values);
                    }
                }
            }
            else
            {
                LOG(VB_GENERAL, LOG_ERR,
                    QString("Exiv2 error: No header, file %1")
                    .arg(fileName));
            }
        }
        else
        {
            LOG(VB_GENERAL, LOG_ERR,
                QString("Exiv2 error: Could not open file, file %1")
                .arg(fileName));
        }
    }
    catch (Exiv2::Error& e)
    {
        LOG(VB_GENERAL, LOG_ERR,
            QString("Exiv2 exception %1, file %2")
            .arg(e.what()).arg(fileName));
    }

    return valueList;
}
Exemplo n.º 2
0
JSONNODE *ImgTagJson::genLitExif(const Exiv2::Image::AutoPtr & image)
{
    //const Exiv2::ExifData &data = image->exifData();
    Exiv2::ExifData &data = image->exifData();
    data.sortByKey();

    if (data.empty()) return NULL;

    Exiv2::ExifData::const_iterator end = data.end();

    JMAP *grpmap = new JMAP();

    JSONNODE *tree = json_new(JSON_NODE);
    json_set_name(tree, "exif");
    for (Exiv2::ExifData::const_iterator i = data.begin(); i != end; i++)
    {
        JSONNODE *grp;

        if (grpmap->find(i->groupName()) == grpmap->end())
        {
            grp = json_new(JSON_NODE);
            json_set_name(grp, i->groupName().c_str());
            grpmap->insert(JMAP::value_type(i->groupName(), grp)); 
        }
        else
           grp = (grpmap->find(i->groupName()))->second;

        Exiv2::ExifData::const_iterator nxt = i;
        nxt++;
        if ((nxt != data.end()) && (i->key() == nxt->key()))
        {
            //cout << "Array Elem! " << i->key() << endl;
            JSONNODE *arr = json_new(JSON_ARRAY);
            json_set_name(arr, i->tagName().c_str());
            json_push_back(arr, json_new_a((i->tagName()).c_str(),
                (i->print()).c_str()));

            while ((nxt != data.end()) && (nxt->key() == i->key()))
            {
                json_push_back(arr, json_new_a((nxt->tagName()).c_str(),
                    (nxt->print()).c_str()));
                nxt++;
            }
            json_push_back(grp, arr);
            if (nxt == data.end()) break;
            nxt--;
            i = nxt;
        }
        else
        {
            json_push_back(grp, json_new_a((i->tagName()).c_str(),
                (i->print()).c_str()));
        }
    }
    JMAP::iterator it;
    for(it = grpmap->begin(); it != grpmap->end(); it++)
    {
        json_push_back(tree, it->second);
        grpmap->erase(it);
    }
        //cout << it->first << endl;
    delete grpmap;
    //cout << json_write_formatted(tree) << endl;
    return tree;
}