FreeImage 라이브러리를 이용하여 JPEG 파일의 메타데이타 중 UserComment 태그의 값을 추출하여 출력하는 예
FreeImage 라이브러리 다운로드 및 프로젝트에 설정하는 방법 참조
Exiv2 유틸리티를 이용하여 JPEG 파일의 메타데이타를 변경하는 예제 참조
#include <iostream>
#include "FreeImage.h"
#pragma comment(lib, "FreeImage.lib")
using namespace std;
/** Generic image loader
@param lpszPathName Pointer to the full file name
@param flag Optional load flag constant
@return Returns the loaded dib if successful, returns NULL otherwise
*/
FIBITMAP* GenericLoader(const char* lpszPathName, int flag) {
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
// check the file signature and deduce its format
// (the second argument is currently not used by FreeImage)
fif = FreeImage_GetFileType(lpszPathName, 0);
if(fif == FIF_UNKNOWN) {
// no signature ?
// try to guess the file format from the file extension
fif = FreeImage_GetFIFFromFilename(lpszPathName);
}
// check that the plugin has reading capabilities ...
if((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) {
// ok, let's load the file
FIBITMAP *dib = FreeImage_Load(fif, lpszPathName, flag);
// unless a bad file format, we are done !
return dib;
}
return NULL;
}
/**
FreeImage error handler
@param fif Format / Plugin responsible for the error
@param message Error message
*/
void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message) {
cout << "\n*** ";
if(fif != FIF_UNKNOWN) {
cout << FreeImage_GetFormatFromFIF(fif) << " Format\n";
}
cout << message;
cout << " ***\n";
}
/**
특정 태그에 대한 순차적 검색 예, 검색된 태그의 이름과 값을 출력함
*/
char* GetValue1(const char *tagName, FIBITMAP *dib, FREE_IMAGE_MDMODEL model) {
FITAG *tag = NULL;
FIMETADATA *mdhandle = NULL;
mdhandle = FreeImage_FindFirstMetadata(model, dib, &tag);
char* _tagValue = NULL;
if(mdhandle) {
do {
char* _tagName = (char*) FreeImage_GetTagKey(tag);
if(strcmp(_tagName, tagName)==0)
{
FREE_IMAGE_MDTYPE tagType = FreeImage_GetTagType(tag);
// 7 = FIDT_UNDEFINED, An 8-bit byte that may contain anything, depending on the definition of the field.
_tagValue = (char*) FreeImage_TagToString(model, tag);
//cout << _tagName << " : " << _tagValue << " : " << tagType << endl;
return _tagValue;
}else continue;
} while(FreeImage_FindNextMetadata(mdhandle, &tag));
return NULL;
}
return NULL;
}
/**
특정 태그에 대한 비순차적 검색의 예, 검색된 태그의 이름과 값을 출력함
*/
char* GetValue2(const char *tagName, FIBITMAP *dib, FREE_IMAGE_MDMODEL model) {
FITAG *tagUserComment = NULL;
FreeImage_GetMetadata(model, dib, tagName, &tagUserComment);
if(tagUserComment != NULL) {
char* _tagName = (char*) FreeImage_GetTagKey(tagUserComment);
FREE_IMAGE_MDTYPE tagType = FreeImage_GetTagType(tagUserComment);
// 7 = FIDT_UNDEFINED, An 8-bit byte that may contain anything, depending on the definition of the field.
char* _tagValue = (char*) FreeImage_TagToString(model, tagUserComment);
//cout << _tagName << " : " << _tagValue << " : " << tagType << endl;
return _tagValue;
}
return NULL;
}
int main(int argc, char *argv[]) {
// call this ONLY when linking with FreeImage as a static library
FreeImage_Initialise();
// initialize your own FreeImage error handler
FreeImage_SetOutputMessage(FreeImageErrorHandler);
// print version & copyright infos
cout << "FreeImage " << FreeImage_GetVersion() << "\n";
cout << FreeImage_GetCopyrightMessage() << "\n\n";
char* jpgFileName = "D:\\test\\sample1.jpg";
// Load the bitmap
FIBITMAP *dib = GenericLoader(jpgFileName, 0);
if(!dib) {
cout << "이미지 로드 실패" << endl;
return 0;
}
cout << endl << "********* UserComment 태그 검색 테스트 **************" << endl;
char* tagName = "UserComment";
char* tagValue = GetValue1(tagName, dib, FIMD_EXIF_EXIF);
cout << tagName << " : " << tagValue << endl;
cout << "*******************************************************" << endl;
// Unload the bitmap
FreeImage_Unload(dib);
// call this ONLY when linking with FreeImage as a static library
FreeImage_DeInitialise();
return 0;
}