Work in progress: add base capability to geotag.cpp (read XML, directory and images).

This commit is contained in:
Robin Mills 2012-07-12 03:47:30 +00:00
parent 6788fe8ff6
commit fd02823600
6 changed files with 211 additions and 846 deletions

View File

@ -149,6 +149,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tiff-test", "tiff-test\tiff
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "geotag", "geotag\geotag.vcproj", "{7E0025E8-CFBE-4941-BEB7-825F14A87EE1}"
ProjectSection(ProjectDependencies) = postProject
{831EF580-92C8-4CA8-B0CE-3D906280A54D} = {831EF580-92C8-4CA8-B0CE-3D906280A54D}
EndProjectSection
EndProject
Global

File diff suppressed because one or more lines are too long

View File

@ -2,9 +2,9 @@ Microsoft Visual Studio Solution File, Format Version 9.00
# Visual Studio 2005
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "exiv2lib", "exiv2lib\exiv2lib.vcproj", "{831EF580-92C8-4CA8-B0CE-3D906280A54D}"
ProjectSection(ProjectDependencies) = postProject
{8308C68D-E12B-4C71-96F4-7137F6BEB654} = {8308C68D-E12B-4C71-96F4-7137F6BEB654}
{6C4C06A3-6F8F-4067-AA4C-D5F41E1FFF9A} = {6C4C06A3-6F8F-4067-AA4C-D5F41E1FFF9A}
{09877CF4-83B6-44FE-A2E2-629AA5C8093E} = {09877CF4-83B6-44FE-A2E2-629AA5C8093E}
{6C4C06A3-6F8F-4067-AA4C-D5F41E1FFF9A} = {6C4C06A3-6F8F-4067-AA4C-D5F41E1FFF9A}
{8308C68D-E12B-4C71-96F4-7137F6BEB654} = {8308C68D-E12B-4C71-96F4-7137F6BEB654}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "exiv2", "exiv2\exiv2.vcproj", "{07293CAC-00DA-493E-90C9-5D010C2B1B53}"
@ -14,8 +14,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "exiv2", "exiv2\exiv2.vcproj
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "xmpsdk", "xmpsdk\xmpsdk.vcproj", "{09877CF4-83B6-44FE-A2E2-629AA5C8093E}"
ProjectSection(ProjectDependencies) = postProject
{8308C68D-E12B-4C71-96F4-7137F6BEB654} = {8308C68D-E12B-4C71-96F4-7137F6BEB654}
{6C4C06A3-6F8F-4067-AA4C-D5F41E1FFF9A} = {6C4C06A3-6F8F-4067-AA4C-D5F41E1FFF9A}
{8308C68D-E12B-4C71-96F4-7137F6BEB654} = {8308C68D-E12B-4C71-96F4-7137F6BEB654}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "expat", "expat\expat.vcproj", "{6C4C06A3-6F8F-4067-AA4C-D5F41E1FFF9A}"
@ -81,6 +81,9 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "utiltest", "utiltest\utilte
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "geotag", "geotag\geotag.vcproj", "{E3073076-4837-4DDB-89E5-5AC297C7481D}"
ProjectSection(ProjectDependencies) = postProject
{831EF580-92C8-4CA8-B0CE-3D906280A54D} = {831EF580-92C8-4CA8-B0CE-3D906280A54D}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

File diff suppressed because one or more lines are too long

202
samples/geotag.cpp Normal file
View File

@ -0,0 +1,202 @@
// ***************************************************************** -*- C++ -*-
// geotag.cpp, $Rev: 2286 $
// Sample program to read gpx files and update the images with GPS tags
// Work in progress - doesn't do anything yet!
#include <exiv2/exiv2.hpp>
#include <iostream>
#include <iomanip>
#include <cassert>
#include <sys/types.h>
#include <stdio.h>
#include "expat.h"
#include <vector>
#include <string>
typedef std::vector<std::string> strings_t ;
#ifndef lengthof
#define lengthof(x) (sizeof(*x)/sizeof(x))
#endif
#ifdef _MSC_VER
#include <windows.h>
#if _MSC_VER < 1400
#define strcpy_s(d,l,s) strcpy(d,s)
#define strcat_s(d,l,s) strcat(d,s)
#endif
#else
#include <dirent.h>
#endif
enum
{ typeUnknown = 0
, typeDirectory = 1
, typeImage = 2
, typeXML = 3
};
static const char* types[] = { "unknown" , "directory" , "image" , "xml" };
class UserData
{
public:
UserData() : indent(0),count(0) {};
virtual ~UserData() {} ;
// public data members
int indent;
size_t count ;
};
static void startElement(void* userData, const char* name, const char** /*atts*/ )
{
UserData* me = (UserData*) userData;
for ( int i = 0 ; i < me->indent ; i++ ) printf(" ");
printf("begin %s\n",name);
me->count++ ;
me->indent++ ;
}
static void endElement(void* userData, const char* name)
{
UserData* me = (UserData*) userData;
me->indent-- ;
for ( int i = 0 ; i < me->indent ; i++ ) printf(" ");
printf("end %s\n",name);
}
bool readDir(char* path,strings_t& paths)
{
#ifndef _MSC_VER
bool bResult = false;
DIR* dir = opendir (path);
if (dir != NULL)
{
bResult = true;
struct dirent* ent;
// print all the files and directories within directory
while ((ent = readdir (dir)) != NULL)
{
printf ("%s\n", ent->d_name);
paths.push_back(std::string(ent->d_name)) ;
}
closedir (dir);
}
#else
bool bResult = (GetFileAttributesA(path) & FILE_ATTRIBUTE_DIRECTORY)!= 0L ;
if ( bResult ) {
char search[_MAX_PATH+10];
strcpy_s(search,_MAX_PATH,path);
strcat_s(search,_MAX_PATH,"\\*");
WIN32_FIND_DATA ffd;
HANDLE hFind = FindFirstFile(search, &ffd);
BOOL bGo = hFind != INVALID_HANDLE_VALUE;
while ( bGo ) {
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// _tprintf(TEXT(" %s <DIR>\n"), ffd.cFileName);
// count++ ;
}
else // if ( ffd.dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
{
// filesize.LowPart = ffd.nFileSizeLow;
// filesize.HighPart = ffd.nFileSizeHigh;
// _tprintf(TEXT(" %s %ld bytes\n"), ffd.cFileName, filesize.QuadPart);
paths.push_back( std::string(ffd.cFileName));
printf("-> %s\n",ffd.cFileName);
}
bGo = FindNextFile(hFind, &ffd) != 0;
}
CloseHandle(hFind);
}
#endif
return bResult ;
}
bool readXML(char* path,size_t& count)
{
FILE* f = fopen(path,"r");
XML_Parser parser = XML_ParserCreate(NULL);
bool bResult = f && parser ;
if ( bResult ) {
char buffer[8*1024];
UserData me ;
XML_SetUserData(parser, &me);
XML_SetElementHandler(parser, startElement, endElement);
// a little sip at the data
size_t len = fread(buffer,1,sizeof(buffer),f);
const char* lead = "<?xml" ;
bResult = strncmp(lead,buffer,strlen(lead))==0;
// swallow it
if ( bResult ) {
bResult = XML_Parse(parser, buffer,(int)len, len == 0 ) == XML_STATUS_OK;
}
// drink the rest of the file
while ( bResult && !feof(f) ) {
len = fread(buffer,1,sizeof(buffer),f);
bResult = XML_Parse(parser, buffer,(int)len, len == 0 ) == XML_STATUS_OK;
};
count = me.count ;
}
if ( f ) fclose(f);
if ( parser ) XML_ParserFree(parser);
return bResult ;
}
int main(int argc, char* const argv[])
{
if ( argc < 2 ) {
std::cout << "Usage: " << argv[0] << " arg+\n";
return 1;
}
for ( int i = 1 ; i < argc ; i++ ) {
char* arg = argv[i];
size_t count = 0 ;
int fileType = typeUnknown ;
if ( fileType == typeUnknown ) {
if ( readXML(arg,count) ) {
if ( count ) fileType = typeXML ;
}
}
if ( fileType == typeUnknown ) {
strings_t files ;
if ( readDir(arg,files) ) {
fileType = typeDirectory ;
count = files.size();
}
}
if ( fileType == typeUnknown ) try {
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(arg);
if ( image.get() ) {
Exiv2::ExifData &exifData = image->exifData();
image->readMetadata();
if ( !exifData.empty() ) {
fileType = typeImage ;
count = exifData.count();
}
}
} catch (Exiv2::Error& ) {};
printf("arg:%s type:%s count:%d\n",arg,types[fileType],count); ;
}
return 0;
}

View File

@ -1,53 +0,0 @@
// ***************************************************************** -*- C++ -*-
// geotag.cpp, $Rev: 2286 $
// Sample program to read gpx files and update the images with GPS tags
// This is a placeholder for now (a copy of exifprint.cpp)
#include <exiv2/exiv2.hpp>
#include <iostream>
#include <iomanip>
#include <cassert>
int main(int argc, char* const argv[])
try {
if (argc != 2) {
std::cout << "Usage: " << argv[0] << " file\n";
return 1;
}
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(argv[1]);
assert(image.get() != 0);
image->readMetadata();
Exiv2::ExifData &exifData = image->exifData();
if (exifData.empty()) {
std::string error(argv[1]);
error += ": No Exif data found in the file";
throw Exiv2::Error(1, error);
}
Exiv2::ExifData::const_iterator end = exifData.end();
for (Exiv2::ExifData::const_iterator i = exifData.begin(); i != end; ++i) {
const char* tn = i->typeName();
std::cout << std::setw(44) << std::setfill(' ') << std::left
<< i->key() << " "
<< "0x" << std::setw(4) << std::setfill('0') << std::right
<< std::hex << i->tag() << " "
<< std::setw(9) << std::setfill(' ') << std::left
<< (tn ? tn : "Unknown") << " "
<< std::dec << std::setw(3)
<< std::setfill(' ') << std::right
<< i->count() << " "
<< std::dec << i->value()
<< "\n";
}
return 0;
}
//catch (std::exception& e) {
//catch (Exiv2::AnyError& e) {
catch (Exiv2::Error& e) {
std::cout << "Caught Exiv2 exception '" << e.what() << "'\n";
return -1;
}