如何利用vc代码读取jpg文件的“作者”等属性值。如下图中的wang ming我参考了http://topic.csdn.net/u/20090304/22/e3438075-fa20-4b0c-add4-7b5cf30394ee.html,里面提到调用shell的方法获取“作者”信息,但是没有成功。有哪位知道,请指点下。

解决方案 »

  1.   

    CxImage 有关于读取metadata的函数
      

  2.   

    试试cximage,可以很容易分析文件头内容
      

  3.   

    #include <windows.h>
    #include <stdio.h>
    #include <iostream>
    #include <locale.h>#ifndef ULONG_PTR
    #define ULONG_PTR unsigned long*
    #endif
    #include <GdiPlus.h>
    using namespace Gdiplus;
    #pragma comment(lib,"gdiplus.lib")// Helper function
    void PropertyTypeFromWORD(WORD index, WCHAR* string, UINT maxChars)
    {
    WCHAR* propertyTypes[] = {
    L"Nothing",                   // 0
    L"PropertyTagTypeByte",       // 1
    L"PropertyTagTypeASCII",      // 2
    L"PropertyTagTypeShort",      // 3
    L"PropertyTagTypeLong",       // 4
    L"PropertyTagTypeRational",   // 5
    L"Nothing",                   // 6
    L"PropertyTagTypeUndefined",  // 7
    L"Nothing",                   // 8
    L"PropertyTagTypeSLONG",      // 9
    L"PropertyTagTypeSRational"}; // 10

    wcsncpy(string, propertyTypes[index], maxChars);
    }int main()
    {
    setlocale(LC_ALL,"chs");
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    while(true)
    {
    WCHAR szName[1024];
    printf("请输入一个图像文件的名称:\n");
    if(!std::wcin.getline(szName,sizeof(szName)/sizeof(WCHAR)))
    return -1;
    UINT size = 0;
    UINT count = 0;
    #define MAX_PROPTYPE_SIZE 128
    WCHAR strPropertyType[MAX_PROPTYPE_SIZE] = L""; Bitmap* bitmap = new Bitmap(szName);
    if(Ok != bitmap->GetLastStatus())
    {
    delete bitmap;
    printf("图像文件打开失败!\n");
    continue;
    }
    bitmap->GetPropertySize(&size, &count);
    printf("There are %d pieces of metadata in the file.\n", count);
    printf("The total size of the metadata is %d bytes.\n\n", size); PropertyItem* pPropBuffer = (PropertyItem*)malloc(size);
    // Get the array of PropertyItem objects.
    bitmap->GetAllPropertyItems(size, count, pPropBuffer);
    // For each PropertyItem in the array, display the id, type, and length.
    // For each PropertyItem in the array, display the id, type, and length.
    for(UINT j = 0; j < count; ++j)
    {
    // Convert the property type from a WORD to a string.
    PropertyTypeFromWORD(
    pPropBuffer[j].type, strPropertyType, MAX_PROPTYPE_SIZE); printf("Property Item %d\n", j);
    printf("  id: 0x%X\n", pPropBuffer[j].id);
    wprintf(L"  type: %s\n", strPropertyType);
    printf("  length: %d bytes\n", pPropBuffer[j].length);
    if(PropertyTagTypeASCII == pPropBuffer[j].type)
    printf("  value: %s\n\n", pPropBuffer[j].value);
    else if((0x9C9D == pPropBuffer[j].id)
    && (PropertyTagTypeByte == pPropBuffer[j].type))
    wprintf(L"  作者: %s\n\n", pPropBuffer[j].value);
    else
    printf("\n");
    } free(pPropBuffer);
    delete bitmap;
    }
    GdiplusShutdown(gdiplusToken);
    return 0;
    }
    典型运行结果如下:
    There are 3 pieces of metadata in the file.
    The total size of the metadata is 324 bytes.Property Item 0
      id: 0x9C9D
      type: PropertyTagTypeByte
      length: 20 bytes
      作者: Haty_MiaoProperty Item 1
      id: 0x5090
      type: PropertyTagTypeShort
      length: 128 bytesProperty Item 2
      id: 0x5091
      type: PropertyTagTypeShort
      length: 128 bytes
    /////////////////
    其中Property Item 0中的作者就是你要的~~
      

  4.   

    恩,谢谢3楼,在设置好gdiplus相关包含路径后,测试成功!
    还想问下hastings,关于gdiplus获取图片元数据这部分的文档能否提供一下?想知道具体每个属性所对应的id分别是多少,就像楼上的“作者”这个属性的id为0x9C9D
    谢谢!
      

  5.   

    你可以查看GdiPlusImaging.h这个头文件,里面有很多。看英文就可大概知道id的意思。
    不过没有id为0x9C9D这一项,这个也是我猜的。 - -