数码相机拍照都有EXIF信息,GPS信息就是加在EXIF里的;
Exiv2是一个开源的项目用来读写照片的EXIF信息,附上官网https://www.exiv2.org/
关于Exiv2和EXIF的介绍网上的资料很多,不在多介绍
结合官方的例子(https://www.exiv2.org/doc/examples.html),用Qt对Exiv2进行简单的封装实现添加GPS添加和EXIF信息查询
.h文件
#include <QObject>
#include<QString>
#include"exiv2/exiv2.hpp"
using namespace std;
//经度 纬度 高度
const QString GPS_Longitude="Exif.GPSInfo.GPSLongitude";
const QString GPS_Latitude="Exif.GPSInfo.GPSLatitude";
const QString GPS_Altitude="Exif.GPSInfo.GPSAltitude";
class ImageAnalysis : public QObject
{
Q_OBJECT
public:
explicit ImageAnalysis(QObject *parent = nullptr);
public:
//重新设置分析的照片
void Reset(const QString &imagePath);
//添加GPS信息
bool AddExifGPSInfo(const QString &keyStr, const QString &value);
//设置完后,把数据写入
bool WriteExifData();
//查找一个ExifKey避免没有的Key产生段错误 目前没有用
QString FindExifKey(const QString &key);
private:
//照片的路径
QString m_imagePath;
std::auto_ptr<Exiv2::Image> m_imagePtr;
Exiv2::ExifData m_ed;
private:
//度转度分秒
QStringList DegreeToDDMMSS(const QString& degree);
QString GetDDMMSS(const QString& degree,int n,bool bEnd=false);
QString DDMMSSToExivGps(QStringList &strList);//把度分秒转换为exiv 保存的gps字符串格式
QString AltitudeToExiivGps(const QString &altitude);//把高度转换为exiv 保存的gps字符串格式
};
.cpp文件
#include "imageanalysis.h"
#include<QDebug>
#include<iostream>
#include<QMessageBox>
#include<qmath.h>
using namespace std;
ImageAnalysis::ImageAnalysis(QObject *parent) : QObject(parent),m_imagePath("")
{
}