exif

package module
v0.0.0-...-4ee0589 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2020 License: MIT Imports: 9 Imported by: 0

README

Go (golang) bindings for libexif

forked from xiam/exif, add windows support, add raw data export.

Provides basic support for reading EXIF tags on files using libexif and CGO.

How to install

Get libexif for your OS:

# OSX
brew install libexif

# Debian
apt-get install -y libexif-dev

# RedHat
dnf install -y libexif-devel

# windows
cd libexif
make
cd ..
go build

Then grab the exif package with go get:

go get github.com/xiam/exif

Usage

Install the package with go get and use import to include it in your project:

import "github.com/xiam/exif"

This is an example on how to read EXIF data from a file:

package main

import (
  "fmt"
  "github.com/xiam/exif"
)

func main() {
    data, err := exif.Read("_examples/resources/test.jpg")
    ...
    for key, val := range data.Tags {
        fmt.Printf("%s = %s\n", key, val)
    }

    // raw data export.
    for key, val := range exif.Raw {
      fmt.Printf("%s: %d\n", key.String(), len(val.Raw))
    }
}

If you just have the image available as an io.Reader, you can parse the EXIF header like this:

reader := exif.New()

_, err = io.Copy(reader, data)

// exif.FoundExifInData is a signal that the EXIF parser has all it needs,
// it doesn't need to be given the whole image.
if err != nil && err != exif.FoundExifInData {
  t.Fatalf("Error loading bytes: %s", err.Error())
}

err := reader.Parse()

if err != nil {
  t.Fatalf("Error parsing EXIF: %s", err.Error())
}

for key, val := range reader.Tags {
  fmt.Printf("%s: %s\n", key, val)
}

License

This is Open Source released under the terms of the MIT License:

Copyright (c) 2012-2015 José Carlos Nieto, https://menteslibres.net/xiam

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package exif provides bindings for libexif.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLengthNotMatch = errors.New("rational length not match")
	ErrFormatNotMatch = errors.New("cannot match request format")
)
View Source
var (
	ErrNoExifData      = errors.New(`no exif data found`)
	ErrFoundExifInData = errors.New(`found exif header. OK to call Parse`)
)

Error messages.

View Source
var (
	ErrNotFoundEntry = errors.New("cannot found special entry")
	ErrUnknownFormat = errors.New("unknown entry data format")
	ErrValueNotMatch = errors.New("read value format not match")
	ErrValueTooSmall = errors.New("value length too small")
)

Functions

This section is empty.

Types

type Data

type Data struct {
	Raw   map[IfdTag]Entry
	Order binary.ByteOrder
	// contains filtered or unexported fields
}

Data stores the EXIF tags of a file.

func New

func New() *Data

New creates and returns a new exif.Data object.

func Read

func Read(file string) (*Data, error)

Read attempts to read EXIF data from a file.

func (*Data) Open

func (d *Data) Open(file string) error

Open opens a file path and loads its EXIF data.

func (*Data) Parse

func (d *Data) Parse() error

Parse finalizes the data loader and sets the tags

func (*Data) Write

func (d *Data) Write(p []byte) (n int, err error)

Write writes bytes to the exif loader. Sends ErrFoundExifInData error when enough bytes have been sent.

type Entry

type Entry struct {
	Ifd        Ifd
	Tag        Tag
	Format     EntryFormat
	Components int
	Raw        []byte
	// contains filtered or unexported fields
}

func (*Entry) GetDouble64

func (e *Entry) GetDouble64() ([]float64, error)

func (*Entry) GetFloat32

func (e *Entry) GetFloat32() ([]float32, error)

func (*Entry) GetInt32

func (e *Entry) GetInt32() ([]int32, error)

func (*Entry) GetInt8

func (e *Entry) GetInt8() ([]int8, error)

func (*Entry) GetRaw

func (e *Entry) GetRaw() ([]byte, error)

func (*Entry) GetUint32

func (e *Entry) GetUint32() ([]uint32, error)

func (*Entry) GetValue

func (e *Entry) GetValue() (interface{}, error)

FormatAscii => string FormatUnsignedByte => []byte FormatUnsignedShort => []uint16 FormatUnsignedLong => []uint32 FormatUnsignedRational []UnsignedRational FormatSignedByte => []int8 FormatUndefined => []byte FormatSignedShort => []int16 FormatSignedLong => []int32 FormatSignedRational => []SignedRational FormatFloat => []float32 FormatDouble => []float64

func (*Entry) ReadAsSignedRational

func (e *Entry) ReadAsSignedRational() ([]SignedRational, error)

func (*Entry) ReadAsSignedShort

func (e *Entry) ReadAsSignedShort() ([]int16, error)

func (*Entry) ReadAsString

func (e *Entry) ReadAsString() (string, error)

func (*Entry) ReadAsUnsignedRational

func (e *Entry) ReadAsUnsignedRational() ([]UnsignedRational, error)

func (*Entry) ReadAsUnsignedShort

func (e *Entry) ReadAsUnsignedShort() ([]uint16, error)

func (*Entry) String

func (e *Entry) String() string

type EntryFormat

type EntryFormat int
const (
	FormatUnsignedByte     EntryFormat = 1
	FormatAscii            EntryFormat = 2
	FormatUnsignedShort    EntryFormat = 3
	FormatUnsignedLong     EntryFormat = 4
	FormatUnsignedRational EntryFormat = 5
	FormatSignedByte       EntryFormat = 6
	FormatUndefined        EntryFormat = 7
	FormatSignedShort      EntryFormat = 8
	FormatSignedLong       EntryFormat = 9
	FormatSignedRational   EntryFormat = 10
	FormatFloat            EntryFormat = 11
	FormatDouble           EntryFormat = 12
)

type Helper

type Helper struct {
	Raw   map[IfdTag]Entry
	Order binary.ByteOrder
}

func NewHelper

func NewHelper(data *Data) *Helper

func (*Helper) GetAltitude

func (h *Helper) GetAltitude() (float64, error)

func (*Helper) GetEntry

func (h *Helper) GetEntry(ifd, tag uint16) *Entry

func (*Helper) GetLatitude

func (h *Helper) GetLatitude() (float64, error)

func (*Helper) GetLocation

func (h *Helper) GetLocation() (*Location, error)

func (*Helper) GetLongitude

func (h *Helper) GetLongitude() (float64, error)

func (*Helper) GetTimestamp

func (h *Helper) GetTimestamp() (*time.Time, error)

func (*Helper) GetValue

func (h *Helper) GetValue(ifd Ifd, tag Tag) (interface{}, error)

FormatAscii => string FormatUnsignedByte => []byte FormatUnsignedShort => []uint16 FormatUnsignedLong => []uint32 FormatUnsignedRational []UnsignedRational FormatSignedByte => []int8 FormatUndefined => []byte FormatSignedShort => []int16 FormatSignedLong => []int32 FormatSignedRational => []SignedRational FormatFloat => []float32 FormatDouble => []float64

type Ifd

type Ifd uint16
const (
	Ifd0 Ifd = 0 + iota
	Ifd1
	IfdExif
	IfdGps
	IfdInterOperability
	IfdMaxCount
)

type IfdTag

type IfdTag [4]byte

ifd: [0: 2], tag: [2: 4], littleEndian

func NewIfdTag

func NewIfdTag(ifd, tag uint16) IfdTag

func (*IfdTag) Ifd

func (m *IfdTag) Ifd() uint16

func (*IfdTag) String

func (m *IfdTag) String() string

func (*IfdTag) Tag

func (m *IfdTag) Tag() uint16

type Location

type Location struct {
	Longitude float64
	Latitude  float64
	Altitude  float64
}

func (*Location) String

func (l *Location) String() string

type SignedRational

type SignedRational struct {
	Numerator   int32
	Denominator int32
}

type Tag

type Tag uint16
const (
	EXIF_TAG_INTEROPERABILITY_INDEX                   Tag = 0x0001
	EXIF_TAG_INTEROPERABILITY_VERSION                 Tag = 0x0002
	EXIF_TAG_NEW_SUBFILE_TYPE                         Tag = 0x00fe
	EXIF_TAG_IMAGE_WIDTH                              Tag = 0x0100
	EXIF_TAG_IMAGE_LENGTH                             Tag = 0x0101
	EXIF_TAG_BITS_PER_SAMPLE                          Tag = 0x0102
	EXIF_TAG_COMPRESSION                              Tag = 0x0103
	EXIF_TAG_PHOTOMETRIC_INTERPRETATION               Tag = 0x0106
	EXIF_TAG_FILL_ORDER                               Tag = 0x010a
	EXIF_TAG_DOCUMENT_NAME                            Tag = 0x010d
	EXIF_TAG_IMAGE_DESCRIPTION                        Tag = 0x010e
	EXIF_TAG_MAKE                                     Tag = 0x010f
	EXIF_TAG_MODEL                                    Tag = 0x0110
	EXIF_TAG_STRIP_OFFSETS                            Tag = 0x0111
	EXIF_TAG_ORIENTATION                              Tag = 0x0112
	EXIF_TAG_SAMPLES_PER_PIXEL                        Tag = 0x0115
	EXIF_TAG_ROWS_PER_STRIP                           Tag = 0x0116
	EXIF_TAG_STRIP_BYTE_COUNTS                        Tag = 0x0117
	EXIF_TAG_X_RESOLUTION                             Tag = 0x011a
	EXIF_TAG_Y_RESOLUTION                             Tag = 0x011b
	EXIF_TAG_PLANAR_CONFIGURATION                     Tag = 0x011c
	EXIF_TAG_RESOLUTION_UNIT                          Tag = 0x0128
	EXIF_TAG_TRANSFER_FUNCTION                        Tag = 0x012d
	EXIF_TAG_SOFTWARE                                 Tag = 0x0131
	EXIF_TAG_DATE_TIME                                Tag = 0x0132
	EXIF_TAG_ARTIST                                   Tag = 0x013b
	EXIF_TAG_WHITE_POINT                              Tag = 0x013e
	EXIF_TAG_PRIMARY_CHROMATICITIES                   Tag = 0x013f
	EXIF_TAG_SUB_IFDS                                 Tag = 0x014a
	EXIF_TAG_TRANSFER_RANGE                           Tag = 0x0156
	EXIF_TAG_JPEG_PROC                                Tag = 0x0200
	EXIF_TAG_JPEG_INTERCHANGE_FORMAT                  Tag = 0x0201
	EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH           Tag = 0x0202
	EXIF_TAG_YCBCR_COEFFICIENTS                       Tag = 0x0211
	EXIF_TAG_YCBCR_SUB_SAMPLING                       Tag = 0x0212
	EXIF_TAG_YCBCR_POSITIONING                        Tag = 0x0213
	EXIF_TAG_REFERENCE_BLACK_WHITE                    Tag = 0x0214
	EXIF_TAG_XML_PACKET                               Tag = 0x02bc
	EXIF_TAG_RELATED_IMAGE_FILE_FORMAT                Tag = 0x1000
	EXIF_TAG_RELATED_IMAGE_WIDTH                      Tag = 0x1001
	EXIF_TAG_RELATED_IMAGE_LENGTH                     Tag = 0x1002
	EXIF_TAG_CFA_REPEAT_PATTERN_DIM                   Tag = 0x828d
	EXIF_TAG_CFA_PATTERN                              Tag = 0x828e
	EXIF_TAG_BATTERY_LEVEL                            Tag = 0x828f
	EXIF_TAG_COPYRIGHT                                Tag = 0x8298
	EXIF_TAG_EXPOSURE_TIME                            Tag = 0x829a
	EXIF_TAG_FNUMBER                                  Tag = 0x829d
	EXIF_TAG_IPTC_NAA                                 Tag = 0x83bb
	EXIF_TAG_IMAGE_RESOURCES                          Tag = 0x8649
	EXIF_TAG_EXIF_IFD_POINTER                         Tag = 0x8769
	EXIF_TAG_INTER_COLOR_PROFILE                      Tag = 0x8773
	EXIF_TAG_EXPOSURE_PROGRAM                         Tag = 0x8822
	EXIF_TAG_SPECTRAL_SENSITIVITY                     Tag = 0x8824
	EXIF_TAG_GPS_INFO_IFD_POINTER                     Tag = 0x8825
	EXIF_TAG_ISO_SPEED_RATINGS                        Tag = 0x8827
	EXIF_TAG_OECF                                     Tag = 0x8828
	EXIF_TAG_TIME_ZONE_OFFSET                         Tag = 0x882a
	EXIF_TAG_EXIF_VERSION                             Tag = 0x9000
	EXIF_TAG_DATE_TIME_ORIGINAL                       Tag = 0x9003
	EXIF_TAG_DATE_TIME_DIGITIZED                      Tag = 0x9004
	EXIF_TAG_COMPONENTS_CONFIGURATION                 Tag = 0x9101
	EXIF_TAG_COMPRESSED_BITS_PER_PIXEL                Tag = 0x9102
	EXIF_TAG_SHUTTER_SPEED_VALUE                      Tag = 0x9201
	EXIF_TAG_APERTURE_VALUE                           Tag = 0x9202
	EXIF_TAG_BRIGHTNESS_VALUE                         Tag = 0x9203
	EXIF_TAG_EXPOSURE_BIAS_VALUE                      Tag = 0x9204
	EXIF_TAG_MAX_APERTURE_VALUE                       Tag = 0x9205
	EXIF_TAG_SUBJECT_DISTANCE                         Tag = 0x9206
	EXIF_TAG_METERING_MODE                            Tag = 0x9207
	EXIF_TAG_LIGHT_SOURCE                             Tag = 0x9208
	EXIF_TAG_FLASH                                    Tag = 0x9209
	EXIF_TAG_FOCAL_LENGTH                             Tag = 0x920a
	EXIF_TAG_SUBJECT_AREA                             Tag = 0x9214
	EXIF_TAG_TIFF_EP_STANDARD_ID                      Tag = 0x9216
	EXIF_TAG_MAKER_NOTE                               Tag = 0x927c
	EXIF_TAG_USER_COMMENT                             Tag = 0x9286
	EXIF_TAG_SUB_SEC_TIME                             Tag = 0x9290
	EXIF_TAG_SUB_SEC_TIME_ORIGINAL                    Tag = 0x9291
	EXIF_TAG_SUB_SEC_TIME_DIGITIZED                   Tag = 0x9292
	EXIF_TAG_XP_TITLE                                 Tag = 0x9c9b
	EXIF_TAG_XP_COMMENT                               Tag = 0x9c9c
	EXIF_TAG_XP_AUTHOR                                Tag = 0x9c9d
	EXIF_TAG_XP_KEYWORDS                              Tag = 0x9c9e
	EXIF_TAG_XP_SUBJECT                               Tag = 0x9c9f
	EXIF_TAG_FLASH_PIX_VERSION                        Tag = 0xa000
	EXIF_TAG_COLOR_SPACE                              Tag = 0xa001
	EXIF_TAG_PIXEL_X_DIMENSION                        Tag = 0xa002
	EXIF_TAG_PIXEL_Y_DIMENSION                        Tag = 0xa003
	EXIF_TAG_RELATED_SOUND_FILE                       Tag = 0xa004
	EXIF_TAG_INTEROPERABILITY_IFD_POINTER             Tag = 0xa005
	EXIF_TAG_FLASH_ENERGY                             Tag = 0xa20b
	EXIF_TAG_SPATIAL_FREQUENCY_RESPONSE               Tag = 0xa20c
	EXIF_TAG_FOCAL_PLANE_X_RESOLUTION                 Tag = 0xa20e
	EXIF_TAG_FOCAL_PLANE_Y_RESOLUTION                 Tag = 0xa20f
	EXIF_TAG_FOCAL_PLANE_RESOLUTION_UNIT              Tag = 0xa210
	EXIF_TAG_SUBJECT_LOCATION                         Tag = 0xa214
	EXIF_TAG_EXPOSURE_INDEX                           Tag = 0xa215
	EXIF_TAG_SENSING_METHOD                           Tag = 0xa217
	EXIF_TAG_FILE_SOURCE                              Tag = 0xa300
	EXIF_TAG_SCENE_TYPE                               Tag = 0xa301
	EXIF_TAG_NEW_CFA_PATTERN                          Tag = 0xa302
	EXIF_TAG_CUSTOM_RENDERED                          Tag = 0xa401
	EXIF_TAG_EXPOSURE_MODE                            Tag = 0xa402
	EXIF_TAG_WHITE_BALANCE                            Tag = 0xa403
	EXIF_TAG_DIGITAL_ZOOM_RATIO                       Tag = 0xa404
	EXIF_TAG_FOCAL_LENGTH_IN_35MM_FILM                Tag = 0xa405
	EXIF_TAG_SCENE_CAPTURE_TYPE                       Tag = 0xa406
	EXIF_TAG_GAIN_CONTROL                             Tag = 0xa407
	EXIF_TAG_CONTRAST                                 Tag = 0xa408
	EXIF_TAG_SATURATION                               Tag = 0xa409
	EXIF_TAG_SHARPNESS                                Tag = 0xa40a
	EXIF_TAG_DEVICE_SETTING_DESCRIPTION               Tag = 0xa40b
	EXIF_TAG_SUBJECT_DISTANCE_RANGE                   Tag = 0xa40c
	EXIF_TAG_IMAGE_UNIQUE_ID                          Tag = 0xa420
	EXIF_TAG_CAMERA_OWNER_NAME                        Tag = 0xa430
	EXIF_TAG_BODY_SERIAL_NUMBER                       Tag = 0xa431
	EXIF_TAG_LENS_SPECIFICATION                       Tag = 0xa432
	EXIF_TAG_LENS_MAKE                                Tag = 0xa433
	EXIF_TAG_LENS_MODEL                               Tag = 0xa434
	EXIF_TAG_LENS_SERIAL_NUMBER                       Tag = 0xa435
	EXIF_TAG_COMPOSITE_IMAGE                          Tag = 0xa460
	EXIF_TAG_SOURCE_IMAGE_NUMBER_OF_COMPOSITE_IMAGE   Tag = 0xa461
	EXIF_TAG_SOURCE_EXPOSURE_TIMES_OF_COMPOSITE_IMAGE Tag = 0xa462
	EXIF_TAG_GAMMA                                    Tag = 0xa500
	EXIF_TAG_PRINT_IMAGE_MATCHING                     Tag = 0xc4a5
	EXIF_TAG_PADDING                                  Tag = 0xea1c

	EXIF_TAG_GPS_VERSION_ID          Tag = 0x0000
	EXIF_TAG_GPS_LATITUDE_REF        Tag = 0x0001
	EXIF_TAG_GPS_LATITUDE            Tag = 0x0002
	EXIF_TAG_GPS_LONGITUDE_REF       Tag = 0x0003
	EXIF_TAG_GPS_LONGITUDE           Tag = 0x0004
	EXIF_TAG_GPS_ALTITUDE_REF        Tag = 0x0005
	EXIF_TAG_GPS_ALTITUDE            Tag = 0x0006
	EXIF_TAG_GPS_TIME_STAMP          Tag = 0x0007
	EXIF_TAG_GPS_SATELLITES          Tag = 0x0008
	EXIF_TAG_GPS_STATUS              Tag = 0x0009
	EXIF_TAG_GPS_MEASURE_MODE        Tag = 0x000a
	EXIF_TAG_GPS_DOP                 Tag = 0x000b
	EXIF_TAG_GPS_SPEED_REF           Tag = 0x000c
	EXIF_TAG_GPS_SPEED               Tag = 0x000d
	EXIF_TAG_GPS_TRACK_REF           Tag = 0x000e
	EXIF_TAG_GPS_TRACK               Tag = 0x000f
	EXIF_TAG_GPS_IMG_DIRECTION_REF   Tag = 0x0010
	EXIF_TAG_GPS_IMG_DIRECTION       Tag = 0x0011
	EXIF_TAG_GPS_MAP_DATUM           Tag = 0x0012
	EXIF_TAG_GPS_DEST_LATITUDE_REF   Tag = 0x0013
	EXIF_TAG_GPS_DEST_LATITUDE       Tag = 0x0014
	EXIF_TAG_GPS_DEST_LONGITUDE_REF  Tag = 0x0015
	EXIF_TAG_GPS_DEST_LONGITUDE      Tag = 0x0016
	EXIF_TAG_GPS_DEST_BEARING_REF    Tag = 0x0017
	EXIF_TAG_GPS_DEST_BEARING        Tag = 0x0018
	EXIF_TAG_GPS_DEST_DISTANCE_REF   Tag = 0x0019
	EXIF_TAG_GPS_DEST_DISTANCE       Tag = 0x001a
	EXIF_TAG_GPS_PROCESSING_METHOD   Tag = 0x001b
	EXIF_TAG_GPS_AREA_INFORMATION    Tag = 0x001c
	EXIF_TAG_GPS_DATE_STAMP          Tag = 0x001d
	EXIF_TAG_GPS_DIFFERENTIAL        Tag = 0x001e
	EXIF_TAG_GPS_H_POSITIONING_ERROR Tag = 0x001f
)

type UnsignedRational

type UnsignedRational struct {
	Numerator   uint32
	Denominator uint32
}

func (*UnsignedRational) String

func (u *UnsignedRational) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL