Exiv2 —— 读取图片Exif信息(附源码)


附:Exiv2 —— exiv2介绍及下载搭建环境(Vs2017)

源码运行效果

Exiv2 —— 读取图片Exif信息(附源码)_第1张图片

源码
#include 
#include 
#include 

#include "exiv2/exiv2.hpp"

// 寻找对应标签的值
std::string FindExifKey(Exiv2::ExifData &ed, std::string key)
{
	Exiv2::ExifKey tmp = Exiv2::ExifKey(key);			// key,必须为存在的标签,否则会抛出异常。具体标签名称集合可查看csdn
	Exiv2::ExifData::iterator pos = ed.findKey(tmp);
	if (pos == ed.end())
	{
		return "Unknow";
	}
	return pos->value().toString();
}

int main()
{
	const char *imagePath = "./clouds.jpeg";

	Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(imagePath);
	assert(image.get() != 0);
	image->readMetadata();

	Exiv2::ExifData &ed = image->exifData();
	if (ed.empty()) { return -1; }

#if 1
	std::cout << "照片尺寸	:" << FindExifKey(ed, "Exif.Photo.PixelYDimension") << " x " << FindExifKey(ed, "Exif.Photo.PixelXDimension") << std::endl;
#else
	// 遍历存在的数据(并非完全)
	Exiv2::ExifData::const_iterator end = ed.end();
	for (Exiv2::ExifData::const_iterator i = ed.begin(); i != end; ++i)
	{
		const char* tn = i->typeName();
		std::cout
			<< i->key() << "\t"
			<< "0x" << std::hex << i->tag() << " "
			<< (tn ? tn : "Unknown") << " "
			<< i->count() << "  " << i->value()
			<< "\n";
	}
#endif

	system("pause");
	return 0;
}

关注

笔者 - jxd

你可能感兴趣的:(Exiv2,c++,exif,exiv2)