使用cpp的exiv2库编写将经纬度信息写入图片并可以在windows图片属性中查看经纬度脚本
时间: 2023-05-17 13:02:44 浏览: 152
非常感谢您的提问。以下是使用cpp的exiv2库编写将经纬度信息写入图片并可以在windows图片属性中查看经纬度的示例代码:
#include <iostream>
#include <exiv2/exiv2.hpp>
int main(int argc, char** argv) {
try {
// Open the image file
std::string filename = "example.jpg";
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(filename);
if (!image.get()) {
std::cerr << "Failed to open image file: " << filename << std::endl;
return 1;
}
// Read the image metadata
image->readMetadata();
// Set the GPS coordinates
Exiv2::ExifData& exifData = image->exifData();
exifData["Exif.GPSInfo.GPSLatitude"] = Exiv2::Rational(37, 1);
exifData["Exif.GPSInfo.GPSLatitudeRef"] = "N";
exifData["Exif.GPSInfo.GPSLongitude"] = Exiv2::Rational(122, 1);
exifData["Exif.GPSInfo.GPSLongitudeRef"] = "W";
// Write the updated metadata to the image file
image->writeMetadata();
// Print the GPS coordinates
Exiv2::gps_degrees latitude = exifData["Exif.GPSInfo.GPSLatitude"].toGpsDegrees();
Exiv2::gps_degrees longitude = exifData["Exif.GPSInfo.GPSLongitude"].toGpsDegrees();
std::cout << "Latitude: " << latitude << std::endl;
std::cout << "Longitude: " << longitude << std::endl;
return 0;
} catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
}
希望这个示例代码能够帮助到您。如果您有任何问题,请随时问我。
阅读全文