用gdi+获取图像的附加信息(metadata)--如jpg照片的标题,相机,曝光时间等

来源:互联网 发布:对讲机需要网络吗 编辑:程序博客网 时间:2024/04/27 20:58

随着数码相机的普及,需要对数码照片进行操作的程序也越来越多,其中难免要涉及到对数码照片的附加信息进行存取。你固然可以自己写一套存取这些附加信息的函数,但是本着“不要再次发明车轮”的思想,本文介绍大家用windows gdi+ sdk来完成这个工作。

数码照片一般都会存储为JPEG图像格式,同时随着照片各种相机也会存储一些相关的附加信息(Metadata),有人也把这个叫做“图片文件的摘要”。这些信息一般包括照片的标题,相机型号,曝光时间等等。
在gdi+里面,这样的一条信息称作一个PropertyItem,一个典型的照片可能会包含四五十条这样的Item。

下面通过一个C++程序来介绍怎样通过GDI+ SDK提供的函数来存取PropertyItem。



#include <windows.h>
#include "gdiplus.h"
#include <stdio.h>

using namespace Gdiplus;
//#pragma comment(lib, "gdiplus.lib")

INT main()
{
   // Start up Gdi+
   GdiplusStartupInput gdiplusStartupInput;
   ULONG_PTR gdiplusToken;
   GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

   Image* image = new Image(L"a.jpg");
   UINT itemsize = 0;
   PropertyItem* item = NULL;

   UINT size, count;
   image->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", size);
  
   // 获取设备制造商这一item,该属性的id为:PropertyItemEquipMake。
   // 先获取该item的大小
   itemsize = image->GetPropertyItemSize(PropertyTagEquipMake);
   // 依大小分配存放该item的内存
   item = (PropertyItem*)malloc(itemsize);
   // 获取该item
   if ( Ok == image->GetPropertyItem(PropertyTagEquipMake, itemsize, item) )
     { // 每个 PropertyItem 对象有四个属性,id,length,type,value。
       printf("The length of the property item is %u./n", item->length);
       printf("The data type of the property item is %u./n", item->type);
       // 注意:对value的具体处理方法依type的不同而不同。
       printf("The value of the property item is %s./n", item->value);
     }
   else
       printf("Fail! :( /n");//想知道发生了什么可以参看:GdiplusTypes.h里的对Status的枚举
   free(item);

   // 获取曝光时间,该属性的id为:PropertyTagExifExposureTime。
   itemsize = image->GetPropertyItemSize(PropertyTagExifExposureTime);
   item = (PropertyItem*)malloc(itemsize);
   if ( Ok == image->GetPropertyItem(PropertyTagExifExposureTime, itemsize, item) )
     {
       printf("The length of the property item is %u./n", item->length);
       printf("The data type of the property item is %u./n", item->type);
       // 注意:对value的具体处理方法依type的不同而不同。
       int* ptrLong = (int*)(item->value);
       printf("The exposure time is %d/%d./n", ptrLong[0],ptrLong[1]);
     }
   else
       printf("Fail! :( /n");
   free(item);  


   delete image;
   GdiplusShutdown(gdiplusToken);

   system("pause");
   return 0;
}

该程序在通过下面”注意“里的准备后,可以在Borland C++ Builder 6 下调试通过。
我没有在vc下调试过,如果想用在vc下,下面的注意也许仍会有帮助。


注意:要想把这个程序运行成功,要经过下面的一些准备工作。这应该是由于vc与bcb的不同导致的。

第一步:
从BCB安装的文件夹下的include文件夹里把所有gdiplus开头的头文件全都拷到你的工程文件夹(因为这里面提供了需要的数据结构和函数原型,并且需要对它们作小小的改动)。注意不要使用ms sdk里的头文件和lib(这是对BCB用户而言)。

第二步:
把GdiplusGraphics.h的第33行改为:
return new Graphics(hdc,0);(这样改应该有潜在的错误,但没有发现。知其然不知其所以然呐。)
在GdiplusTypes.h的第87、88行插入下面两行:
REAL min(REAL a, REAL b){return a<=b?a:b;}
REAL max(REAL a, REAL b){return a>=b?a:b;}

第三步:
从MS的网站下载gdiplus.dll,(网址:http://www.microsoft.com/downloads/details.aspx?FamilyID=6a63ab9c-df12-4d41-933c-be590feaa05a&DisplayLang=en )
运行BCB的bin文件夹里的 implib gdiplus gdiplus.dll生成gdiplus.lib
把gdiplus.lib放在工程文件夹内,用Shift+F11把gdiplus.lib加入到工程。
(或者使用#pragma comment(lib, "gdiplus.lib")语句)
注意不能使用ms sdk里的lib(那个是供VC使用的,COFF格式,而BCB采用OMF格式)。

然后就可以了。



更多请参见:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/GDIPlus/usingGDIPlus/usingimagesbitmapsandmetafiles/readingandwritingmetadata.asp

Kevin Pisarsky有一篇介绍delphi下获取metadata的文章,我不知道名字,
但有个关键词是:EXIF information
你可以在网上检索一下。





后记:

前面看到的是这篇文章的第三版,是我后来改写的比较完整的文章了,第一版写得比较详细,但由于下面的原因,除了那个程序,别的什么也没有保留下来。为了发表在blog上,就草草写了第二版。直到今天才有恐写了第三版。

”第一次写文章就遇到了毁灭性的打击,我痛恨着拙劣的blog!
辛辛苦苦写到了凌晨四点,发表的时候没写文章摘要(damn it!),得到提示的时候竟然正文全没了!
我用的是该死的firefox,点击后退也没有任何作用,我怎么会一次也没有保存呢!见鬼了!
我不会抓内存,只有去死了。“



最近听说有了一种新的获取很多种文件摘要的方式,不再局限于jpeg,txt、doc等等都可以,请大家在网上搜索关于dsofile.dll,还可以参见http://www.microsoft.com/china/technet/community/columns/scripts/sg1103.mspx


原创粉丝点击