-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_exif.go
More file actions
168 lines (151 loc) · 5.03 KB
/
parse_exif.go
File metadata and controls
168 lines (151 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package phantomtype
import (
"fmt"
"github.com/dsoprea/go-exif"
"io"
"io/ioutil"
"github.com/dsoprea/go-logging"
"github.com/mitchellh/mapstructure"
)
type IfdEntry struct {
IfdPath string `json:"ifd_path"`
FqIfdPath string `json:"fq_ifd_path"`
IfdIndex int `json:"ifd_index"`
TagId uint16 `json:"tag_id"`
TagName string `json:"tag_name"`
TagTypeId uint16 `json:"tag_type_id"`
TagTypeName string `json:"tag_type_name"`
UnitCount uint32 `json:"unit_count"`
Value interface{} `json:"value"`
ValueString string `json:"value_string"`
}
// need to be "int" for GAE datastore
type Rational struct {
Numerator int
Denominator int
}
// need to be "int" for GAE datastore
type SignedRational struct {
Numerator int
Denominator int
}
type Exif struct {
Make string `json:"Make"`
Model string `json:"Model"`
XResolution string `json:"XResolution"`
YResolution string `json:"YResolution"`
ResolutionUnit string `json:"ResolutionUnit"`
Software string `json:"Software""`
DateTime string `json:"DateTime"`
ExifTag string `json:"ExifTag"`
ExposureTime Rational `json:"ExposureTime"`
FNumber Rational `json:"FNumber"`
ExposureProgram string `json:"ExposureProgram"`
ISOSpeedRatings string `json:"ISOSpeedRatings"`
SensitivityType string `json:"SensitivityType"`
ExifVersion string `json:"ExifVersion"`
DateTimeOriginal string `json:"DateTimeOriginal"`
DateTimeDigitized string `json:"DateTimeDigitized"`
ShutterSpeedValue SignedRational `json:"ShutterSpeedValue"`
ApertureValue Rational `json:"ApertureValue"`
BrightnessValue SignedRational `json:"BrightnessValue"`
ExposureBiasValue SignedRational `json:"ExposureBiasValue"`
MaxApertureValue Rational `json:"MaxApertureValue"`
MeteringMode string `json:"MeteringMode"`
LightSource string `json:"LightSource"`
Flash string `json:"Flash"`
FocalLength Rational `json:"FocalLength"`
ColorSpace string `json:"ColorSpace"`
FocalPlaneXResolution Rational `json:"FocalPlaneXResolution"`
FocalPlaneYResolution Rational `json:"FocalPlaneYResolution"`
FocalPlaneResolutionUnit string `json:"FocalPlaneResolutionUnit"`
SensingMethod string `json:"SensingMethod"`
FileSource string `json:"FileSource"`
SceneType string `json:"SceneType"`
CustomRendered string `json:"CustomRendered"`
ExposureMode string `json:"ExposureMode"`
WhiteBalance string `json:"WhiteBalance"`
FocalLengthIn35mmFilm string `json:"FocalLengthIn35mmFilm"`
SceneCaptureType string `json:"SceneCaptureType"`
Sharpness string `json:"Sharpness"`
SubjectDistanceRange string `json:"SubjectDistanceRange"`
LensSpecification Rational `json:"LensSpecification"`
LensMake string `json:"LensMake"`
LensModel string `json:"LensModel"`
LensSerialNumber string `json:"LensSerialNumber"`
}
func ExtractExif(reader io.Reader) Exif {
bs, _ := ioutil.ReadAll(reader)
rawExif, err := exif.SearchAndExtractExif(bs)
log.PanicIf(err)
im := exif.NewIfdMappingWithStandard()
ti := exif.NewTagIndex()
im.StripPathPhraseIndices("")
result := Exif{}
resultData := make(map[string]interface{})
visitor := func(fqIfdPath string, ifdIndex int, tagId uint16, tagType exif.TagType, valueContext exif.ValueContext) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
log.PanicIf(err)
}
}()
ifdPath, err := im.StripPathPhraseIndices(fqIfdPath)
log.PanicIf(err)
it, err := ti.Get(ifdPath, tagId)
if err != nil {
if log.Is(err, exif.ErrTagNotFound) {
fmt.Printf("Warning: Unknown tag: [%s] (%04x)\n", ifdPath, tagId)
return nil
} else {
log.PanicIf(err)
}
}
valueString := ""
var value interface{}
if tagType.Type() == exif.TypeUndefined {
var err error
value, err = exif.UndefinedValue(ifdPath, tagId, valueContext, tagType.ByteOrder())
if log.Is(err, exif.ErrUnhandledUnknownTypedTag) {
value = nil
} else if err != nil {
log.Panic(err)
} else {
valueString = fmt.Sprintf("%v", value)
}
} else if tagType.Type() == exif.TypeRational {
valueA, err := tagType.ReadRationalValues(valueContext)
value = valueA[0]
log.PanicIf(err)
valueString = fmt.Sprintf("%v/%v", valueA[0].Numerator, valueA[0].Denominator)
} else if tagType.Type() == exif.TypeSignedRational {
valueA, err := tagType.ReadSignedRationalValues(valueContext)
value = valueA[0]
log.PanicIf(err)
valueString = fmt.Sprintf("%v/%v", valueA[0].Numerator, valueA[0].Denominator)
} else {
valueString, err = tagType.ResolveAsString(valueContext, true)
log.PanicIf(err)
value = valueString
}
entry := IfdEntry{
IfdPath: ifdPath,
FqIfdPath: fqIfdPath,
IfdIndex: ifdIndex,
TagId:tagId,
TagName: it.Name,
TagTypeId:tagType.Type(),
TagTypeName:tagType.Name(),
UnitCount:valueContext.UnitCount,
Value:value,
ValueString:valueString,
}
resultData[entry.TagName] = entry.Value
fmt.Printf("%+v\n", entry)
return nil
}
_, err = exif.Visit(exif.IfdStandard, im, ti, rawExif, visitor)
log.PanicIf(err)
mapstructure.Decode(resultData, &result)
return result
}