本文整理汇总了Golang中github.com/rwcarlsen/goexif/exif.Decode函数的典型用法代码示例。如果您正苦于以下问题:Golang Decode函数的具体用法?Golang Decode怎么用?Golang Decode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Decode函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: readOrientation
func readOrientation(r io.ReadSeeker) (int, error) {
_, err := r.Seek(0, os.SEEK_SET)
if err != nil {
fmt.Println("Failed to seek to beginning of stream")
return 0, err
}
x, err := exif.Decode(r)
if err != nil {
fmt.Println("Failed to decode EXIF", err)
return 0, err
}
orientationData, err := x.Get(exif.Orientation)
if err != nil {
fmt.Println("Failed to read orientation property")
return 0, err
}
orientation, err := orientationData.Int(0)
if err != nil {
fmt.Println("Failed to decode orientation")
return 0, err
}
return orientation, nil
}
示例2: main
func main() {
fi, err := ioutil.ReadDir("../../../images/")
if err != nil {
log.Fatal(err)
}
for _, v := range fi {
f, err := os.Open(v.Name())
if err != nil {
log.Fatal(err)
}
x, err := exif.Decode(f)
if err != nil {
log.Fatal(err)
}
tm, _ := x.DateTime()
fmt.Println("Taken: ", tm)
lat, long, _ := x.LatLong()
fmt.Println("lat, long: ", lat, ", ", long)
}
}
示例3: Load
// Load initializes a Photograph from a file.
func (p *Photograph) Load(fileName string) error {
p.OriginalFileName = fileName
fd, err := os.Open(fileName)
if err != nil {
logging.Log.Error("%v", err)
return err
}
defer fd.Close()
exif.RegisterParsers(mknote.All...)
exifData, err := exif.Decode(fd)
if err != nil {
logging.Log.Error("%v", err)
return err
}
err = exifData.Walk(p)
if err != nil {
logging.Log.Error("%v", err)
return err
}
offset, err := fd.Seek(0, 0)
if err != nil {
logging.Log.Error("%v", err)
return err
}
logging.Log.Debug("offset=%v", offset)
p.Data, err = ioutil.ReadAll(fd)
if err != nil {
logging.Log.Error("%v", err)
return err
}
return nil
}
示例4: ReadRAF
// ReadRAF makes a new RAF from a file
func ReadRAF(fname string) *RAF {
raf := new(RAF)
f, err := os.Open(fname)
defer f.Close()
if err == nil {
err = binary.Read(f, binary.BigEndian, &raf.Header)
}
jbuf := make([]byte, raf.Header.Dir.Jpeg.Len)
f.ReadAt(jbuf, int64(raf.Header.Dir.Jpeg.IDX))
raf.Jpeg = jbuf
// Soon.
// exif.RegisterParsers(mknote.Fuji)
buf := bytes.NewBuffer(jbuf)
raf.Exif, err = exif.Decode(buf)
if err != nil {
panic(err)
}
return raf
}
示例5: getDateFromExif
func getDateFromExif(file string) (error, time.Time) {
f, err := os.Open(file)
defer f.Close()
if err != nil {
return errors.New("pclassify: warning: read exif info failed"), time.Now()
}
x, err := exif.Decode(f)
if err != nil {
return errors.New("pclassify: warning: read exif info failed"), time.Now()
}
ts, err := x.Get(exif.DateTimeOriginal)
if err != nil {
return errors.New("pclassify: warning: read exif info failed"), time.Now()
}
const layout = "2006:01:02 15:04:05"
t, err := time.ParseInLocation(layout, ts.StringVal(), time.Local)
if err != nil {
return errors.New("pclassify: warning: read exif info failed"), time.Now()
}
return nil, t
}
示例6: orient
func orient(r io.Reader, i image.Image) (image.Image, error) {
t := log.Start()
defer log.End(t)
e, err := exif.Decode(r)
if err != nil {
log.Printf("fail to decode exif: %v", err)
return nil, err
}
tag, err := e.Get(exif.Orientation)
// Orientationタグが存在しない場合、処理を完了する
if err != nil {
log.Println("oritentation tag doesn't exist")
return nil, err
}
o, err := tag.Int(0)
if err != nil {
log.Println("oritentation tag is't int")
return nil, err
}
rect := i.Bounds()
// orientation=5~8 なら画像サイズの縦横を入れ替える
if o >= 5 && o <= 8 {
rect = RotateRect(rect)
}
d := image.NewRGBA64(rect)
a := affines[o]
a.TransformCenter(d, i, interp.Bilinear)
return d, nil
}
示例7: getDateTime
// getDateTime reads the exif data from fname and returns a string representation
// of the DateTimeOriginal tag.
// TODO move to its own package?
func getDateTime(fname string) (time.Time, error) {
f, err := os.Open(fname)
if err != nil {
return time.Now(), err
}
defer f.Close()
x, err := exif.Decode(f)
if err != nil {
return time.Now(), err
}
date, err := x.Get(exif.DateTimeOriginal)
if err != nil {
return time.Now(), err
}
dateStr, err := date.StringVal()
if err != nil {
return time.Now(), nil
}
log.Println("Setting DateTimeOriginal to ", dateStr, " on ", fname)
t, err := time.Parse("2006:01:02 15:04:05", dateStr)
if err != nil {
return time.Now(), err
}
return t, nil
}
示例8: main
func main() {
ImageFileName := flag.String("image", "", "Dont use pngs please.")
flag.Parse()
var R io.Reader
if len(*ImageFileName) > 0 {
Rrr, err := os.Open(*ImageFileName)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", "Cant open file.")
}
R = Rrr
}
if len(*ImageFileName) == 0 {
R = os.Stdin
}
ExifData, err := exif.Decode(R)
if err == io.EOF {
fmt.Fprintf(os.Stderr, "%s\n", "Error decoding file.")
}
if err != io.EOF {
Location, err := goexifgps.DecodeGPS(ExifData)
if err != io.EOF {
fmt.Println(Location)
}
if err == io.EOF {
fmt.Fprintf(os.Stderr, "%s\n", "Contains no GPS exif data.")
}
}
}
示例9: main
func main() {
fname := "01.jpg"
f, err := os.Open(fname)
if err != nil {
log.Fatal(err)
}
// Optionally register camera makenote data parsing - currently Nikon and
// Canon are supported.
exif.RegisterParsers(mknote.All...)
x, err := exif.Decode(f)
if err != nil {
log.Fatal(err)
}
camModel, _ := x.Get(exif.Model) // normally, don't ignore errors!
fmt.Println(camModel.StringVal())
focal, _ := x.Get(exif.FocalLength)
numer, denom, _ := focal.Rat2(0) // retrieve first (only) rat. value
fmt.Printf("%v/%v", numer, denom)
// Two convenience functions exist for date/time taken and GPS coords:
tm, _ := x.DateTime()
fmt.Println("Taken: ", tm)
lat, long, _ := x.LatLong()
fmt.Println("lat, long: ", lat, ", ", long)
}
示例10: createPhoto
func createPhoto(sourceFilename string) (util.Photo, error) {
photo := util.Photo{}
f, err := os.Open(imageSourceFolderPath + sourceFilename)
if err != nil {
log.Println(err)
return photo, err
}
x, err := exif.Decode(f)
if err != nil {
log.Println(sourceFilename, err)
return photo, err
}
tm, _ := x.DateTime()
if false {
log.Println("Taken: ", tm)
}
albumTime := time.Date(tm.Year(), tm.Month(), tm.Day(), 0, 0, 0, 0, time.UTC)
photo.AlbumDateTime = int(albumTime.Unix())
photo.DateTime = int(tm.Unix())
photo.Filename = sourceFilename
return photo, nil
}
示例11: main
// Our programs starts executing here
func main() {
// We need exactly 2 parameters: the first one is the program name,
// the second one should be the photo we want to operate on
if len(os.Args) != 2 {
fmt.Println("Please give a single file name as an argument!")
os.Exit(1)
}
// Retrieve the photo file name from the arguments array
fileName := os.Args[1]
// Try to opern the given file, error out on failure
file, err := os.Open(fileName)
exitOnError(err, "Couldn't open file")
// Try to extract the EXIF data, error out on failure
exifData, err := exif.Decode(file)
exitOnError(err, "Couldn't find EXIF data")
// Try to find a GPS coordinates entry in the EXIF data structure.
// Error out on failure
lat, long, err := exifData.LatLong()
exitOnError(err, "Couldn't read GPS coordinates")
// Create the final URL by using the Google Maps URL template
url := fmt.Sprintf(googleMapsURLTemplate, lat, long)
// Try to start the default browser for the current OS.
// Show the computer URL on error, so that the user can still
// access it manually
err = open.Start(url)
exitOnError(err, fmt.Sprintf(
"Couldn't start the default browser, please visit %v manually", url))
}
示例12: ParseExif
func (f *File) ParseExif() error {
fpath := filepath.Join(f.TagDir, f.Filename)
if f.Tempfile != "" {
fpath = f.Tempfile
}
fp, err := os.Open(fpath)
defer fp.Close()
if err != nil {
return err
}
f.Exif, err = exif.Decode(fp)
if err != nil {
return err
}
f.DateTime, err = f.Exif.DateTime()
if err != nil {
/// XXX: Log
} else {
f.DateTimeReadable = humanize.Time(f.DateTime)
}
f.Latitude, f.Longitude, err = f.Exif.LatLong()
if err != nil {
/// XXX: Log
}
return nil
}
示例13: imageMeta
func imageMeta(r io.ReadSeeker) (*Image, error) {
conf, err := jpeg.DecodeConfig(r)
if err != nil {
return nil, fmt.Errorf("cannot decode JPEG: %s", err)
}
// compute image hash from image content
oid := sha256.New()
if _, err := io.Copy(oid, r); err != nil {
return nil, fmt.Errorf("cannot compute SHA: %s", err)
}
img := Image{
ImageID: encode(oid),
Width: conf.Width,
Height: conf.Height,
}
if _, err := r.Seek(0, os.SEEK_SET); err != nil {
return nil, fmt.Errorf("cannot seek: %s", err)
}
if meta, err := exif.Decode(r); err != nil {
log.Error("cannot extract EXIF metadata", "error", err.Error())
} else {
if orientation, err := meta.Get(exif.Orientation); err != nil {
log.Debug("cannot extract image orientation",
"decoder", "EXIF",
"error", err.Error())
} else {
if o, err := orientation.Int(0); err != nil {
log.Debug("cannot format orientation",
"decoder", "EXIF",
"error", err.Error())
} else {
img.Orientation = o
}
}
if dt, err := meta.Get(exif.DateTimeOriginal); err != nil {
log.Debug("cannot extract image datetime original",
"decoder", "EXIF",
"error", err.Error())
} else {
if raw, err := dt.StringVal(); err != nil {
log.Debug("cannot format datetime original",
"decoder", "EXIF",
"error", err.Error())
} else {
img.Created, err = time.Parse("2006:01:02 15:04:05", raw)
if err != nil {
log.Debug("cannot parse datetime original",
"decoder", "EXIF",
"value", raw,
"error", err.Error())
}
}
}
}
return &img, nil
}
示例14: processExif
// processExif attempts to rotate a JPEG based on the exif data. If the exif data
// cannot be decoded or the orientation tag not read, we return nil so that the image
// may continue to be uploaded. If there is an error encoding the image after
// modification, this is returned to the caller.
func (f *FileMetadataType) processExif() error {
// Decode exif.
ex, err := exif.Decode(bytes.NewReader(f.Content))
if err != nil {
return nil
}
// Get orientation tag.
tag, err := ex.Get(exif.Orientation)
if err != nil {
return nil
}
orientation, err := tag.Int(0)
if err != nil {
return nil
}
var (
angle int
flipMode exifutil.FlipDirection
switchDimensions bool
)
angle, flipMode, switchDimensions = exifutil.ProcessOrientation(int64(orientation))
im, _, err := image.Decode(bytes.NewReader(f.Content))
if err != nil {
return err
}
if angle != 0 {
im = exifutil.Rotate(im, angle)
}
if flipMode != 0 {
im = exifutil.Flip(im, flipMode)
}
if switchDimensions {
f.Width, f.Height = f.Height, f.Width
}
// Encode JPEG and replace f.Content.
buf := new(bytes.Buffer)
err = jpeg.Encode(buf, im, nil)
if err != nil {
return err
}
f.Content = buf.Bytes()
// Update the hash and filesize based on changed content.
sha1, err := h.SHA1(f.Content)
if err != nil {
return err
}
f.FileHash = sha1
f.FileSize = int32(len(f.Content))
return nil
}
示例15: rename
func rename(filename string, prefix string, suffix string, datetimeFormat string, counterLength int, dryrun bool) (result int, err error) {
if !exists(filename) {
result = 1
err = os.ErrNotExist
return
}
f, err := os.Open(filename)
if err != nil {
result = 1
return
}
e, err := exif.Decode(f)
if err != nil {
result = 1
return
}
t, err := e.DateTime()
if err != nil {
if !exif.IsTagNotPresentError(err) {
result = 1
return
}
/* Exifから撮影時刻が取得できない場合 => ファイルの更新時刻を使用 */
info, _ := os.Stat(filename)
t = info.ModTime()
err = nil
}
datetime := t.Format(datetimeFormat)
ext := filepath.Ext(filename)
for i := 0; ; i++ {
counter := fmt.Sprintf(fmt.Sprintf("_%%0%dd", counterLength), i)
n := datetime + counter + ext
if !exists(n) {
newname := prefix + datetime + counter + suffix + ext
log.Print("[RENAME] ", filename, " => ", newname)
if !dryrun {
err := os.Rename(filename, newname)
if err != nil {
log.Fatal(err)
}
}
break
}
}
result = 0
return
}