From 30c088b90e7b6a25d93cbdad7564ff063e62afd3 Mon Sep 17 00:00:00 2001 From: Nick White Date: Fri, 25 Jan 2019 09:55:55 +0000 Subject: Reorganisation and cleanup --- lib/line/line.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 lib/line/line.go (limited to 'lib/line') diff --git a/lib/line/line.go b/lib/line/line.go new file mode 100644 index 0000000..3adac0a --- /dev/null +++ b/lib/line/line.go @@ -0,0 +1,64 @@ +package line + +import ( + "image" + "image/png" + "io" + "os" +) + +type Detail struct { + Name string + Avgconf float64 + Img CopyableImg + Text string + OcrName string +} + +type CopyableImg interface { + CopyLineTo(io.Writer) (error) +} + +type Details []Detail + +// Used by sort.Sort. +func (l Details) Len() int { return len(l) } + +// Used by sort.Sort. +func (l Details) Less(i, j int) bool { + return l[i].Avgconf < l[j].Avgconf +} + +// Used by sort.Sort. +func (l Details) Swap(i, j int) { l[i], l[j] = l[j], l[i] } + +// This is an implementation of the CopyableImg interface that +// stores the image directly as an image.Image +type ImgDirect struct { + Img image.Image +} + +func (i ImgDirect) CopyLineTo(w io.Writer) (error) { + err := png.Encode(w, i.Img) + if err != nil { + return err + } + return nil +} + +// This is an implementation of the CopyableImg interface that +// stores the path of an image +type ImgPath struct { + Path string +} + +func (i ImgPath) CopyLineTo(w io.Writer) (error) { + f, err := os.Open(i.Path) + if err != nil { + return err + } + defer f.Close() + + _, err = io.Copy(w, f) + return err +} -- cgit v1.2.1-24-ge1ad