• 로그인 함 해보끄나?

  • Sarangnamu.net June 17, 2003
    Home Login Profile Study Open Project Q&A Google Code
    exif CExifManager
    Last Modify : 13 December, 2010(12:16)
    /**
     * @file CExifManager.h
     * @author cheol-dong choi <aucd29@gmail.com>
     * @version 1.0
     * @date 2010-12-13 (12:05:40)
     * @section LICENSE
     *
     * Copyright (c) 2003-2010, cheol-dong choi, (http://www.sarangnamu.net)
     *
     * Permission is hereby granted, free of charge, to any person
     * obtaining a copy of this software and associated documentation
     * files (the "Software"), to deal in the Software without
     * restriction, including without limitation the rights to use,
     * copy, modify, merge, publish, distribute, sublicense, and/or sell
     * copies of the Software, and to permit persons to whom the
     * Software is furnished to do so, subject to the following
     * conditions:
     *
     * The above copyright notice and this permission notice shall be
     * included in all copies or substantial portions of the Software.
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
     * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
     * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
     * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,ARISING
     * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     * OTHER DEALINGS IN THE SOFTWARE.
     * 
     * @section DESCRIPTION
     *
     * Thumbnail
     * @code
     * CExifManager objExif;
     * objExif.Load("/temp/SNC00003.jpg");
     * objExif.SaveThumb("/temp/SNC00003"); * thumbnail file save
     * @endcode
     *
     * -------
     * @code
     * CExifManager objExif;
     * objExif.Load("/temp/SNC00003.jpg");
     * FILE* fp = fopen("/temp/thumb.jpg", "wb");
     * fwrite(objExif.GetBuffer(), 1, objExif.GetLength(), fp);
     * fclose(fp);
     * objExif.Unload();
     * @endcode
     *
     * Exif Text Data
     * -------
     * @code
     * CExifManager objExif;
     * objExif.Load("/temp/SNC00003.jpg");
     * printf("%s : %s\n", objExif.GetTagName(EXIF_TAG_MAKE), objExif.GetTagData(EXIF_TAG_MAKE));
     * printf("%s : %s\n", objExif.GetTagName(EXIF_TAG_MODEL), objExif.GetTagData(EXIF_TAG_MODEL));
     * printf("%s\n", objExif.GetExif(EXIF_TAG_DATE_TIME));
     * objExif.Unload();
     * @endcode
     *
     * 
     * EXIF TAG <libexif/exif-tag.h>
     * @code
     * EXIF_TAG_IMAGE_WIDTH 			= 0x0100,
     * EXIF_TAG_IMAGE_LENGTH 			= 0x0101,
     * EXIF_TAG_DOCUMENT_NAME 			= 0x010d,
     * EXIF_TAG_IMAGE_DESCRIPTION 		= 0x010e,
     * EXIF_TAG_MAKE 				= 0x010f,
     * EXIF_TAG_MODEL 				= 0x0110,
     * EXIF_TAG_ORIENTATION 			= 0x0112,
     * EXIF_TAG_X_RESOLUTION 			= 0x011a,
     * EXIF_TAG_Y_RESOLUTION 			= 0x011b,
     * EXIF_TAG_RESOLUTION_UNIT 		= 0x0128,
     * EXIF_TAG_TRANSFER_FUNCTION 		= 0x012d,
     * EXIF_TAG_SOFTWARE 			= 0x0131,
     * EXIF_TAG_DATE_TIME			= 0x0132,
     * EXIF_TAG_ARTIST				= 0x013b,
     * EXIF_TAG_COPYRIGHT			= 0x8298,
     * EXIF_TAG_EXPOSURE_TIME			= 0x829a,
     * EXIF_TAG_FNUMBER			= 0x829d,
     * EXIF_TAG_IMAGE_RESOURCES		= 0x8649,
     * EXIF_TAG_EXIF_IFD_POINTER		= 0x8769,
     * EXIF_TAG_INTER_COLOR_PROFILE		= 0x8773,
     * EXIF_TAG_EXPOSURE_PROGRAM		= 0x8822,
     * EXIF_TAG_SPECTRAL_SENSITIVITY		= 0x8824,
     * EXIF_TAG_GPS_INFO_IFD_POINTER		= 0x8825,
     * EXIF_TAG_ISO_SPEED_RATINGS		= 0x8827,
     * EXIF_TAG_TIME_ZONE_OFFSET		= 0x882a,
     * EXIF_TAG_EXIF_VERSION			= 0x9000,
     * EXIF_TAG_DATE_TIME_ORIGINAL		= 0x9003,
     * EXIF_TAG_DATE_TIME_DIGITIZED		= 0x9004,
     * EXIF_TAG_SHUTTER_SPEED_VALUE		= 0x9201,
     * EXIF_TAG_APERTURE_VALUE			= 0x9202,
     * EXIF_TAG_BRIGHTNESS_VALUE		= 0x9203,
     * EXIF_TAG_EXPOSURE_BIAS_VALUE		= 0x9204,
     * EXIF_TAG_MAX_APERTURE_VALUE		= 0x9205,
     * EXIF_TAG_SUBJECT_DISTANCE		= 0x9206,
     * EXIF_TAG_METERING_MODE			= 0x9207,
     * EXIF_TAG_LIGHT_SOURCE			= 0x9208,
     * EXIF_TAG_FLASH				= 0x9209,
     * EXIF_TAG_FOCAL_LENGTH			= 0x920a,
     * EXIF_TAG_SUBJECT_AREA			= 0x9214,
     * EXIF_TAG_TIFF_EP_STANDARD_ID		= 0x9216,
     * EXIF_TAG_MAKER_NOTE			= 0x927c,
     * EXIF_TAG_USER_COMMENT			= 0x9286,
     * EXIF_TAG_INTEROPERABILITY_IFD_POINTER	= 0xa005,
     * EXIF_TAG_SUBJECT_LOCATION		= 0xa214,
     * EXIF_TAG_EXPOSURE_INDEX			= 0xa215,
     * EXIF_TAG_SENSING_METHOD			= 0xa217,
     * EXIF_TAG_FILE_SOURCE			= 0xa300,
     * EXIF_TAG_SCENE_TYPE			= 0xa301,
     * EXIF_TAG_NEW_CFA_PATTERN		= 0xa302,
     * EXIF_TAG_CUSTOM_RENDERED		= 0xa401,
     * EXIF_TAG_EXPOSURE_MODE			= 0xa402,
     * EXIF_TAG_WHITE_BALANCE			= 0xa403,
     * EXIF_TAG_DIGITAL_ZOOM_RATIO		= 0xa404,
     * EXIF_TAG_FOCAL_LENGTH_IN_35MM_FILM	= 0xa405,
     * EXIF_TAG_SCENE_CAPTURE_TYPE		= 0xa406,
     * EXIF_TAG_GAIN_CONTROL			= 0xa407,
     * EXIF_TAG_CONTRAST			= 0xa408,
     * EXIF_TAG_SATURATION			= 0xa409,
     * EXIF_TAG_SHARPNESS			= 0xa40a,
     * EXIF_TAG_DEVICE_SETTING_DESCRIPTION	= 0xa40b,
     * EXIF_TAG_SUBJECT_DISTANCE_RANGE		= 0xa40c,
     * EXIF_TAG_IMAGE_UNIQUE_ID		= 0xa420,
     * EXIF_TAG_GAMMA				= 0xa500,
     * EXIF_TAG_PRINT_IMAGE_MATCHING		= 0xc4a5
     * @endcode
     *
     * @section CHANGE_LOG
     *
     */
    
    
    
    #ifndef __CD_CEXIFMANAGER_H__
    #define __CD_CEXIFMANAGER_H__
    
    //
    // includes
    //
    #include "libexif/exif-loader.h"
    #include "libexif/exif-tag.h"
    
    #define EXIF_BUFFER_LENGTH 512
    
    
    class CExifManager
    {
    public:
    
    	/*
    	 * Construction
    	 */
    	CExifManager();
    
    	/*
    	 * Destructor
    	 */
    	virtual ~CExifManager();
    
    public:
    
    	/*
    	 * Getter methods
    	 */
    	int Load(const char* szPath);
    
    	int SaveThumb(const char* szPath);
    
    	unsigned char* GetBuffer();
    
    	size_t GetLength();
    
    	void Unload();
    
    	const char* GetExif(int nTag, int nTagType=EXIF_IFD_0);
    
    	const char* GetTagData(int nTag, int nTagType=EXIF_IFD_0);
    
    	const char* GetTagName(int nTag, int nTagType=EXIF_IFD_0);
    
    	int ReadData(unsigned char*& szDest, size_t nSize);
    
    protected:
    
    	/*
    	 * Internal methods
    	 */
    	void Init();
    
    	void TrimSpaces();
    
    private:
    
    	/*
    	 * Attributes
    	 */
    	ExifLoader* _pExif;
    
    	ExifData* _pData;
    
    	char _szBuffer[EXIF_BUFFER_LENGTH];
    
    	size_t _nReaded;
    };
    
    #endif
    
    /**
     * @file CExifManager.cpp
     * @author cheol-dong choi <aucd29@gmail.com>
     * @version 1.0
     * @date 2010-12-13 (12:09:33)
     * @section LICENSE
     *
     * Copyright (c) 2003-2010, cheol-dong choi, (http://www.sarangnamu.net)
     *
     * Permission is hereby granted, free of charge, to any person
     * obtaining a copy of this software and associated documentation
     * files (the "Software"), to deal in the Software without
     * restriction, including without limitation the rights to use,
     * copy, modify, merge, publish, distribute, sublicense, and/or sell
     * copies of the Software, and to permit persons to whom the
     * Software is furnished to do so, subject to the following
     * conditions:
     *
     * The above copyright notice and this permission notice shall be
     * included in all copies or substantial portions of the Software.
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
     * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
     * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
     * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,ARISING
     * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     * OTHER DEALINGS IN THE SOFTWARE.
     * 
     * @section DESCRIPTION
     *
     *
     * @section CHANGE_LOG
     *
     */
    
    
    #include "CExifManager.h"
    #include "RSLOGGER.H"
    #include <string>
    
    
    #if defined(_WIN32_WCE)
    #define snprintf _snprintf
    #endif
    
    
    CExifManager::CExifManager()
    	: _pExif(NULL)
    	, _pData(NULL)
    	, _nReaded(0)
    {
    
    }
    
    
    CExifManager::~CExifManager()
    {
    	if (_pExif != NULL)
    		exif_loader_unref(_pExif);
    
    	if (_pData != NULL)
    		exif_data_unref(_pData);
    }
    
    
    /**
     * Jpeg file open
     *
     * @param szPath jpeg file path
     * @return true or false
     */
    int CExifManager::Load(const char* szPath)
    {
    	LOG1("CExifManager::Load\n");
    
    	Init();
    
    	if (_pExif)
    	{
    		// Load the EXIF data from the image file
    		exif_loader_write_file(_pExif, szPath);
    
    		// Get a pointer to the EXIF data
    		_pData = exif_loader_get_data(_pExif);
    
    		// The loader is no longer needed--free it
    		exif_loader_unref(_pExif);
    
    		_pExif = NULL;
    
    		if (_pData == NULL)
    		{
    			LOG2("Exif Data Error\n");
    
    			return false;
    		}
    	}
    
    	return true;
    }
    
    
    /**
     * Save thumbnail data
     *
     * @param szPath jpeg file path
     * @return true or false
     */
    int CExifManager::SaveThumb(const char* szPath)
    {
    	LOG1("CExifManager::SaveThumb\n");
    
    	int rc = 1;
    
    	if (_pData)
    	{
    		// Make sure the image had a thumbnail before trying to write it
    		if (_pData->data && _pData->size)
    		{
    			FILE *thumb;
    			char thumb_name[1024] = {0};
    			std::string szThumbName;
    			szThumbName = szPath;
    			size_t nPos = szThumbName.rfind(".");
    			if (nPos == std::string::npos)
    			{
    				szThumbName += "_thumb.jpg";
    			}
    			else
    			{
    				szThumbName = szThumbName.replace(nPos, 1, "_thumb.");
    			}
    
    			// Try to create a unique name for the thumbnail file
    			thumb = fopen(szThumbName.c_str(), "wb");
    
    			if (thumb)
    			{
    				// Write the thumbnail image to the file
    				fwrite(_pData->data, 1, _pData->size, thumb);
    				fclose(thumb);
    
    				LOG2("Wrote thumbnail to %s\n", szThumbName.c_str());
    
    				rc = 0;
    			}
    			else
    			{
    				LOG2("Could not create file %s\n", szThumbName.c_str());
    
    				rc = 2;
    			}
    		}
    		else
    		{
    			LOG2("No EXIF thumbnail in file %s\n", szPath);
    
    			rc = 1;
    		}
    
    		// Free the EXIF data
    		exif_data_unref(_pData);
    
    		_pData = NULL;
    	}
    
    	return rc;
    }
    
    
    /**
     * jpeg 이미지에서 thumbnail buffer 를 추출한다.
     *
     * @return thumbnail buffer
     */
    unsigned char* CExifManager::GetBuffer()
    {
    	LOG1("CExifManager::GetData\n");
    
    	if (_pData == NULL)
    		return NULL;
    
    	return _pData->data;
    }
    
    
    /**
     * 버퍼에 사이즈를 반환한다.
     *
     * @return 버퍼 사이즈
     */
    size_t CExifManager::GetLength()
    {
    	LOG1("CExifManager::GetLength\n");
    
    	if (_pData == NULL)
    		return 0;
    
    	return _pData->size;
    }
    
    
    /**
     * exif data free memory
     */
    void CExifManager::Unload()
    {
    	LOG1("CExifManager::Unload\n");
    
    	// Free the EXIF data
    	if (_pData)
    		exif_data_unref(_pData);
    
    	_pData = NULL;
    }
    
    
    /**
     * Getting exif full data
     *
     * @param nTag 테그 정보
     * @param nTagType=EXIF_IFD_0 
     * @return 
     */
    const char* CExifManager::GetExif(int nTag, int nTagType/*=EXIF_IFD_0*/)
    {
    	LOG1("CExifManager::GetExif\n");
    
    	//
    	// Example
    	// -------
    	// Make : SAMSUNG
    	//
    
    	if (_pData == NULL)
    		return NULL;
    
    	ExifEntry* entry = exif_content_get_entry(_pData->ifd[nTagType], (ExifTag)nTag);
    
    	if (entry)
    	{
    		char szBuffer[EXIF_BUFFER_LENGTH];
    
    		memset(szBuffer, 0, sizeof(szBuffer));
    
    		// Get the contents of the tag in human-readable form
    		exif_entry_get_value(entry, szBuffer, sizeof(szBuffer));
    
    		szBuffer[EXIF_BUFFER_LENGTH - 1] = 0;
    
    		snprintf(_szBuffer, EXIF_BUFFER_LENGTH - 1, "%s : %s", GetTagName(nTag, nTagType), szBuffer);
    
    		return _szBuffer;
    	}
    
    	return NULL;
    }
    
    
    /**
     * Getting Exif data
     *
     * @param nTag 
     * @param nTagType=EXIF_IFD_0 
     * @return 
     */
    const char* CExifManager::GetTagData(int nTag, int nTagType/*=EXIF_IFD_0*/)
    {
    	LOG1("CExifManager::GetExifData\n");
    
    	//
    	// Example
    	// -------
    	// SAMSUNG
    	//
    
    	if (_pData == NULL)
    		return NULL;
    
    	// See if this tag exist
    	ExifEntry* entry = exif_content_get_entry(_pData->ifd[nTagType], (ExifTag)nTag);
    
    	if (entry)
    	{
    		memset(_szBuffer, 0, sizeof(_szBuffer));
    
    		// Get the contents of the tag in human-readable form
    		exif_entry_get_value(entry, _szBuffer, sizeof(_szBuffer));
    
    		TrimSpaces();
    
    		return _szBuffer;
    	}
    
    	return NULL;
    }
    
    
    /**
     * Getting tag name
     *
     * @param nTag 
     * @param nTagType=EXIF_IFD_0 
     * @return 
     */
    const char* CExifManager::GetTagName(int nTag, int nTagType/*=EXIF_IFD_0*/)
    {
    	LOG1("CExifManager::GetTagName\n");
    
    	//
    	// Example
    	// -------
    	// Make
    	//
    
    	return exif_tag_get_name_in_ifd((ExifTag)nTag, (ExifIfd)nTagType);
    }
    
    
    /**
     * Init Exif Loader
     */
    void CExifManager::Init()
    {
    	LOG1("CExifManager::Init\n");
    
    	_pExif = exif_loader_new();
    
    	if (_pExif == NULL)
    		LOG2("Exif Init Error\n");
    }
    
    
    /**
     * Don't bother printing it if it's entirely blank
     */
    void CExifManager::TrimSpaces()
    {
    	LOG1("CExifManager::TrimSpace\n");
    
    	_szBuffer[EXIF_BUFFER_LENGTH - 1] = 0;
    }
    
    
    /**
     * exif 에 thumbnail 정보를 nSize 만큼 읽어 dest 에 전달
     *
     * @param szDest 
     * @param nSize 
     * @return 
     */
    int CExifManager::ReadData(unsigned char*& szDest, size_t nSize)
    {
    	LOG1("CExifManager::ReadData\n");
    
    	if ((_pData->size - _nReaded) > nSize)
    	{
    		memcpy(szDest, _pData->data + _nReaded, nSize);
    
    		_nReaded += nSize;
    
    		return nSize;
    	}
    	else if ((_pData->size - _nReaded) < 0)
    	{
    		LOG3("CExifManager::ReadData <READ 0>\n");
    
    		return 0;
    	}
    	else
    	{
    		memcpy(szDest, _pData->data + _nReaded, (_pData->size - _nReaded));
    
    		return (_pData->size - _nReaded);
    	}
    }
    

    Comment


    입력하3 1502976611



    Locations of visitors to this page