Search code examples
c++winapifilesystemsmetadata

Get camera model from picture file in c++


I hope this isn't a duplicate, but I wasn't able to find something similar.

I was wondering how I could get the picture details for a specific file in windows.enter image description here

When searching for ways to do this, I was mainly pointed towards the following code, but I'm not sure how to use it/if it's the best option:

std::string ImageProcessor::GetCameraType(std::string filePath) 
{
WIN32_FILE_ATTRIBUTE_DATA fInfo; 
auto stemp = std::wstring(filePath.begin(), filePath.end());
GetFileAttributesEx(stemp.c_str(), GetFileExInfoStandard, &fInfo);
//PrintFileAttributes(fInfo.dwFileAttributes);
}

Can I even use these file attributes to get those custom camera details? Or do I need to go about it in another way? (FIY, the picture is a CR2 file)

I'm new to c++ so all help would be helpfull!


Solution

  • There are multiple ways to do this. The most simple is to use the Shell API and query for the System.Photo.CameraModel property, something like this:

    #include <windows.h>
    #include <shobjidl_core.h>
    #include <propkey.h>
    #include <stdio.h>
    
    int main()
    {
        CoInitialize(NULL);
        IShellItem2* item;
        if (SUCCEEDED(SHCreateItemFromParsingName(L"c:\\path\\photo.jpg", NULL, IID_PPV_ARGS(&item))))
        {
            LPWSTR str;
            if (SUCCEEDED(item->GetString(PKEY_Photo_CameraModel, &str)))
            {
                wprintf(L"Model: %s\n", str);
                CoTaskMemFree(str);
            }
            item->Release();
        }
        CoUninitialize();
        return 0;
    }