使用Exiv2讀取圖像屬性的詳細信息

一、圖像詳細信息

1.在windows下,點擊圖像,右鍵屬性,詳細信息裏面,就可以查看該圖像的具體信息,如水平分辨率,分辨率,寬度,高度等,如下圖:
在這裏插入圖片描述
2.這此信息對於做圖像處理是很有用處的,我一直在用OpenCV做相關的圖像處理,但查了文檔,好像OpenCV沒有獲取這些信息相關的函數,OpenCV可以獲取圖像的位深度,但對位深度是40的圖像,OpenCV獲取的並不準確。那麼只能藉助於別的工具庫,比如GDI+ 或者是EXIV2,我這裏用的是EXIV2。
3.我的編程環境是VS2015,WIN10 64,可以從我上傳的資源下載編譯好的EXIV2庫。

二、EXIV2

1.下載地址https://www.exiv2.org/,進去後下載已編譯好的msvc64庫,或者下載源碼自己Cmake也可以。
2.把lib和include加到工程的相關目錄下,把bin加到系統環境變量裏面,然後在附加依賴項裏面加上lib的文件名。
在這裏插入圖片描述

三、測試源碼

#include <exiv2/exiv2.hpp>   
#include <iostream> 
#include <string>

std::string FindExifKey(Exiv2::ExifData &ed, std::string key)
{
	Exiv2::ExifKey tmp = Exiv2::ExifKey(key);
	Exiv2::ExifData::iterator pos = ed.findKey(tmp);
	if (pos == ed.end())
	{
		return "Unknow";
	}
	return pos->value().toString();
}

int main(void)
{
	//輸入的圖像路徑
	std::string image_path = "5.JPG";
	
	static std::unique_ptr<Exiv2::Image> image = Exiv2::ImageFactory::open(image_path);//儘量別這麼寫
	
	if (image.get() == 0)
	{
		std::cout << "Read Exif Error." << std::endl;
		return -1;
	}

	//讀取照片的exif信息  
	image->readMetadata();
	Exiv2::ExifData ed = image->exifData();//得到exif信息  

	if (ed.empty())
	{
		std::cout << "Not Find ExifInfo" << std::endl;
		return -1;
	}

	std::cout << "版權	:" << FindExifKey(ed, "Exif.Image.Copyright") << std::endl;
	std::cout << "創作人	:" << FindExifKey(ed, "Exif.Image.Artist") << std::endl;
	std::cout << "相機品牌	:" << FindExifKey(ed, "Exif.Image.Make") << std::endl;
	std::cout << "相機型號	:" << FindExifKey(ed, "Exif.Image.Model") << std::endl;
	std::cout << "鏡頭型號	:" << FindExifKey(ed, "Exif.Photo.LensModel") << std::endl;
	std::cout << "鏡頭序列號	:" << FindExifKey(ed, "Exif.Photo.LensSerialNumber") << std::endl;
	std::cout << "ISO	:" << FindExifKey(ed, "Exif.Photo.ISOSpeedRatings") << std::endl;
	std::cout << "光圈	:" << FindExifKey(ed, "Exif.Photo.FNumber") << std::endl;
	std::cout << "焦距	:" << FindExifKey(ed, "Exif.Photo.FocalLength") << std::endl;
	std::cout << "快門	:" << FindExifKey(ed, "Exif.Photo.ExposureTime") << std::endl;
	std::cout << "拍攝時間	:" << FindExifKey(ed, "Exif.Image.DateTime") << std::endl;
	std::cout << "閃光燈	:" << FindExifKey(ed, "Exif.CanonCs.FlashMode") << std::endl;
	std::cout << "水平分辨率	:" << FindExifKey(ed, "Exif.Image.XResolution") << std::endl;
	std::cout << "垂直分辨率	:" << FindExifKey(ed, "Exif.Image.YResolution") << std::endl;
	std::cout << "照片尺寸	:" << FindExifKey(ed, "Exif.Photo.PixelYDimension") << " x " << FindExifKey(ed, "Exif.Photo.PixelXDimension") << std::endl;

	return 0;
}

運行結果:
在這裏插入圖片描述

發佈了69 篇原創文章 · 獲贊 38 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章