带有 GDIplus 的 Qt 无法读取 jpeg 图像的 exif 值(id、类型、长度工作)

Qt with GDIplus can't read the exif value of a jpeg image (id, type, length working)

我想在 qt 中使用 windows 应用程序读取 jpeg 图像的 exif 数据。 我使用这个函数:Image.GetAllPropertyItems class

我可以加载所有 属性 项并将其显示在文本浏览器中进行测试:

PropertyItem* pAllItems = (PropertyItem*)malloc(totalBufferSize);
image->GetAllPropertyItems(totalBufferSize, numProperties, pAllItems);
... loop ...
ui->textBrowser->append(QString::number(pAllItems[xxx].id));    
ui->textBrowser->append(QString::number(pAllItems[xxx].type));
ui->textBrowser->append(QString::number(pAllItems[xxx].length));

所以我可以看到我的 exif 数据的 ID、类型和长度,效果很好。

但是我无法读取 PropertyItem 的值。数据类型不同,可以用类型检查。 我想读取exif数据的时间(id == 0x9003),类型为2(have a look),长度为20。

微软写道:

Specifies that Value is a null-terminated ASCII string. If you set the type data member to ASCII type, you should set the Len property to the length of the string including the null terminator. For example, the string "Hello" would have a length of 6.

我试过很多这样的方法:

QByteArray propItem = pAllItems[xxx].value;

但我不知道我做错了什么。 Qt 没有编译它:

C2240: "Initalisation": 'void *' cannot convert to 'QByteArray'

我想我知道编译器是什么意思,但我不知道如何解决它。如果有人能帮助我,我会很高兴。谢谢。

当类型为字符串时,您应该将 value 重新解释为指向以 null 结尾的 ASCII 字符串的指针,正如您引用的文档所述:

for (auto i = 0; i < numProperties; ++i) {
  auto const & property = pAllItems[i];
  ui->textBrowser->append(QString::number(property.id));    
  ui->textBrowser->append(QString::number(property.type));
  ui->textBrowser->append(QString::number(property.length));
  auto valueStr{QStringLiteral("value of an unhandled type")};
  switch (property.type) {
  case PropertyTagTypeASCII: 
    valueStr = QString::fromLatin1(reinterpret_cast<const char*>(property.value),
                                   property.length);
    break;
  case PropertyTagTypeByte:
    valueStr = QString::number(*reinterpret_cast<qint8*>(property.value));
    break;
  // etc.
  default:
    break;
  }
  ui->textBrowser->append(valueStr);
}