Working with EXIF data
2008-10-26 10:21
344 查看
简介
我们继续关注Windows Mobile 5.0关于图像的API特性。前面我们展示了如何使用API加载并显示图像,在这片文章里,我将展示如何读取图像中的Exif数据。
Exif是数码相机中存储照片元数据的地方。它可以存储包括相机制造商、相机型号、相机的参数设置,设置可以存储拍照时刻所在的地理位置,如果你的数码相机支持GPS的话。下面我们就来看看我们如何读取这些数据。
实现
这个测试应用只是展示读取Exif信息
IStream* pStream = CreateStreamByFileName(strFileName);
IStream* CTestExifDlg::CreateStreamByFileName(const CString& strFileName)
{
HANDLE hFile = CreateFile(strFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
return 0;
}
DWORD dwFileSize = GetFileSize(hFile, NULL);
if (dwFileSize == (DWORD)-1)
{
CloseHandle(hFile);
return 0;
}
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
if (hGlobal == NULL) {
CloseHandle(hFile);
return 0;
}
LPVOID pvData = GlobalLock(hGlobal);
if (pvData == NULL) {
GlobalUnlock(hGlobal);
CloseHandle(hFile);
return 0;
}
DWORD dwBytesRead = 0;
BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
GlobalUnlock(hGlobal);
CloseHandle(hFile);
if (!bRead)
{
return 0;
}
IStream* pStream = 0;
if (FAILED(CreateStreamOnHGlobal(hGlobal, TRUE, &pStream))) {
return 0;
}
return pStream;
}
现在我们有一个IStream,可以创建Image Decoder来读取Exif数据了。
IImagingFactory *pImgFactory = 0;
if (SUCCEEDED(CoCreateInstance (CLSID_ImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IImagingFactory, (void **)&pImgFactory))) {
IImageDecoder* pDecoder = 0;
if (SUCCEEDED(pImgFactory->CreateImageDecoder(pStream, DecoderInitFlagNone, &pDecoder))) {
UINT nCount = 0;
if (SUCCEEDED(pDecoder->GetPropertyCount(&nCount)) && nCount) {
PROPID* pIds = new PROPID[nCount];
if (SUCCEEDED(pDecoder->GetPropertyIdList(nCount, pIds))) {
for (UINT i = 0; i < nCount; i++) {
UINT nPropSize = 0;
if (SUCCEEDED(pDecoder->GetPropertyItemSize(pIds[i], &nPropSize))) {
BYTE* pBuff = new BYTE[nPropSize];
PropertyItem* pItem = (PropertyItem*)pBuff;
if (SUCCEEDED(pDecoder->GetPropertyItem(pIds[i], nPropSize, pItem))) {
AddPropertyItemToList(i, pList, pItem);
}
delete[] pBuff;
}
}
}
delete[] pIds;
}
pDecoder->Release();
}
pImgFactory->Release();
}
我们继续关注Windows Mobile 5.0关于图像的API特性。前面我们展示了如何使用API加载并显示图像,在这片文章里,我将展示如何读取图像中的Exif数据。
Exif是数码相机中存储照片元数据的地方。它可以存储包括相机制造商、相机型号、相机的参数设置,设置可以存储拍照时刻所在的地理位置,如果你的数码相机支持GPS的话。下面我们就来看看我们如何读取这些数据。
实现
这个测试应用只是展示读取Exif信息
IStream* pStream = CreateStreamByFileName(strFileName);
IStream* CTestExifDlg::CreateStreamByFileName(const CString& strFileName)
{
HANDLE hFile = CreateFile(strFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
return 0;
}
DWORD dwFileSize = GetFileSize(hFile, NULL);
if (dwFileSize == (DWORD)-1)
{
CloseHandle(hFile);
return 0;
}
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
if (hGlobal == NULL) {
CloseHandle(hFile);
return 0;
}
LPVOID pvData = GlobalLock(hGlobal);
if (pvData == NULL) {
GlobalUnlock(hGlobal);
CloseHandle(hFile);
return 0;
}
DWORD dwBytesRead = 0;
BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
GlobalUnlock(hGlobal);
CloseHandle(hFile);
if (!bRead)
{
return 0;
}
IStream* pStream = 0;
if (FAILED(CreateStreamOnHGlobal(hGlobal, TRUE, &pStream))) {
return 0;
}
return pStream;
}
现在我们有一个IStream,可以创建Image Decoder来读取Exif数据了。
IImagingFactory *pImgFactory = 0;
if (SUCCEEDED(CoCreateInstance (CLSID_ImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IImagingFactory, (void **)&pImgFactory))) {
IImageDecoder* pDecoder = 0;
if (SUCCEEDED(pImgFactory->CreateImageDecoder(pStream, DecoderInitFlagNone, &pDecoder))) {
UINT nCount = 0;
if (SUCCEEDED(pDecoder->GetPropertyCount(&nCount)) && nCount) {
PROPID* pIds = new PROPID[nCount];
if (SUCCEEDED(pDecoder->GetPropertyIdList(nCount, pIds))) {
for (UINT i = 0; i < nCount; i++) {
UINT nPropSize = 0;
if (SUCCEEDED(pDecoder->GetPropertyItemSize(pIds[i], &nPropSize))) {
BYTE* pBuff = new BYTE[nPropSize];
PropertyItem* pItem = (PropertyItem*)pBuff;
if (SUCCEEDED(pDecoder->GetPropertyItem(pIds[i], nPropSize, pItem))) {
AddPropertyItemToList(i, pList, pItem);
}
delete[] pBuff;
}
}
}
delete[] pIds;
}
pDecoder->Release();
}
pImgFactory->Release();
}
相关文章推荐
- Chapter 03 Working with batches of data
- 2006年11月9日 Working with Data and ASP.NET 2.0
- Lesson 05: Working with Disconnected Data - The DataSet and SqlDataAdapter(使用断开数据--DataSet和SqlDataAdapter)
- Working with labelled Data
- android TV-Working with Channel Data
- struct – Working with Binary Data¶
- Working with DataFrames
- [bbk4998]第11集 - Chapter 06- Working with Composite Data Types -00-4998(Record)
- Android TV -5.2- Working with Channel Data
- Working with Binary Data in Python
- Scilkit-Learn:Working With Text Data(文本分类)
- pandas working with text data
- Working with Primitive Data Types
- Working with Data in Apex(在Apex中处理数据)
- AIR:Articles:Working with Binary Data
- [bbk5128]第12集 - Chapter 06- Working with Composite Data Types -01-4998(Record)
- [bbk5153]第15集 - Chapter 06- Working with Composite Data Types(Collection)
- pandas working with missing data
- Performing Data Access->Working with Databases in Visual Web Developer 2005 Express Edition
- 《oracle pl/sql programming》 第7章 working with data