图片浏览器开发日志-04(读取相机参数)

相机产生的文件中有(jpg)有个EXIF 段用来记录拍摄时的一些参数,MFC的gdiplus,已经支持这些参数的读取,不用自己费劲琢磨原始文件了。对于图像显示所用的主要参数是相机的旋转角度,知道这个旋转角度就可以自动将照片显示为实际的方向。
下面是笔者根据需要整理的一个简易的exif读取的类:

// An highlighted block
#pragma once
#include "stdafx.h"
#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
#include <map>

using namespace Gdiplus;
class CExif
{
private:
	std::map<PROPID, PropertyItem> PrptItemMap;
	PropertyItem* pAllItems;
public:

	CExif()
	{
		pAllItems = nullptr;
	}

	~CExif()
	{
		if (pAllItems != nullptr) {
			free(pAllItems);
		}
	}
	void setImage(CString imgfile) {
	
		GdiplusStartupInput gdiplusStartupInput;
		ULONG_PTR gdiplusToken;
		GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

		// Create an Image object based on a JPEG file.
		//Image* image = new Image(L"I:\\DC_PIC\\2017\\104ND600\\DSC_6648.jpg");//0x004e0300
		//G:\CPP\MyImageKit\MyImageKit\res
		Image* image = new Image(imgfile);//0x004e0300

																					 // Find out how many property items are in the image, and find out the
																					 // required size of the buffer that will receive those property items.
		UINT totalBufferSize;
		UINT numProperties;
		image->GetPropertySize(&totalBufferSize, &numProperties);

		// Allocate the buffer that will receive the property items.
		pAllItems = (PropertyItem*)malloc(totalBufferSize);

		// Fill the buffer.
		image->GetAllPropertyItems(totalBufferSize, numProperties, pAllItems);

		// Print the id data member of each property item.
		for (UINT j = 0; j < numProperties; ++j)
		{
			std::pair<PROPID, PropertyItem> exifPair(pAllItems[j].id, pAllItems[j]);
			
			PrptItemMap.insert(exifPair);
		}

	
		delete image;
		GdiplusShutdown(gdiplusToken);

	}
	//1#PropertyTagOrientation
	int getRotateAngle() {
		short *p;
		std::map<PROPID, PropertyItem>::iterator iter;
		iter=PrptItemMap.find(PropertyTagOrientation);
		p = (short*)(iter->second.value);
		// 1:0, 8: 90,3,180,6:-90, counterclock is +
		int val;
		int degree = 0;
		val = p[0];
			switch (val)
			{
			case 8:
				degree = 90;
				break;
			case 3:
				degree = 180;
				break;
			case 6:
				degree = 270;
				break;
			default:
				break;
			}

			return degree;
	}
	//2#PropertyTagEquipMake

	int getEquipMake(char * retVal,int len ) {
		char *p;
		std::map<PROPID, PropertyItem>::iterator iter;
		iter = PrptItemMap.find(PropertyTagEquipMake);
		p = (char*)(iter->second.value);
		strcpy_s(retVal, strlen(p) + 1,p);
		return strlen(p)+1;
	}

	//3#PropertyTagEquipModel
	int getEquipModel(char * retVal, int len) {
		char *p;
		std::map<PROPID, PropertyItem>::iterator iter;
		iter = PrptItemMap.find(PropertyTagEquipModel);
		p = (char*)(iter->second.value);
		strcpy_s(retVal, strlen(p) + 1, p);
		return strlen(p)+1;
	}
	//4#PropertyTagArtist
	int getArtist(char * retVal, int len) {
		char *p;
		std::map<PROPID, PropertyItem>::iterator iter;
		iter = PrptItemMap.find(PropertyTagArtist);
		p = (char*)(iter->second.value);
		strcpy_s(retVal, strlen(p)+1, p);
		return strlen(p)+1;
	}
	//5#PropertyTagHostComputer
	int getHostComputer(char * retVal, int len) {
		char *p;
		std::map<PROPID, PropertyItem>::iterator iter;
		iter = PrptItemMap.find(PropertyTagHostComputer);
		if (iter == PrptItemMap.end()) {
			return 0;
		}
		p = (char*)(iter->second.value);
		strcpy_s(retVal, strlen(p) + 1, p);
		return strlen(p) + 1;
	}
	//5#PropertyTagExifExposureTime
	int getExposureTime(long * retVal) {
		long *p;
		std::map<PROPID, PropertyItem>::iterator iter;
		iter = PrptItemMap.find(PropertyTagExifExposureTime);
		if (iter == PrptItemMap.end()) {
			return 0;
		}
		p = (long*)(iter->second.value);
		retVal[0] = p[0];
		retVal[1] = p[1];
		return iter->second.length;
	}	
	//6#PropertyTagExifISOSpeed
	int getISOSpeed() {
		short *p;
		std::map<PROPID, PropertyItem>::iterator iter;
		iter = PrptItemMap.find(PropertyTagExifISOSpeed);
		if (iter == PrptItemMap.end()) {
			return 0;
		}
		p = (short*)(iter->second.value);
		
		return *p;
	}

		
	//7#PropertyTagExifShutterSpeed
	int getShutterSpeed() {
		short *p;
		std::map<PROPID, PropertyItem>::iterator iter;
		iter = PrptItemMap.find(PropertyTagExifShutterSpeed);
		if (iter == PrptItemMap.end()) {
			return 0;
		}
		p = (short*)(iter->second.value);
		
		return *p;
	}	
	//8#PropertyTagExifAperture
	int getAperture(char * retVal, int len) {
		char *p;
		std::map<PROPID, PropertyItem>::iterator iter;
		iter = PrptItemMap.find(PropertyTagExifAperture);
		if (iter == PrptItemMap.end()) {
			return 0;
		}
		p = (char*)(iter->second.value);
		strcpy_s(retVal, strlen(p) + 1, p);
		return strlen(p) + 1;
	}
//9#PropertyTagExifFocalLength
	int getFocalLength(long * retVal) {
		long *p;
		std::map<PROPID, PropertyItem>::iterator iter;
		iter = PrptItemMap.find(PropertyTagExifFocalLength);
		if (iter == PrptItemMap.end()) {
			return 0;
		}
		p = (long*)(iter->second.value);
		retVal[0] = p[0];
		retVal[1] = p[1];
		return iter->second.length;
	}

};


都已经经过测试,各位如果要求不高,可以拿去使用。

说明:获得的参数只是非常基本的参数,GPS数据没有读取。如果哪位有这方面的需求,可以留言。

2020/03/24 于泛五道口地区

发布了7 篇原创文章 · 获赞 0 · 访问量 236

猜你喜欢

转载自blog.csdn.net/Uman/article/details/105073677
今日推荐