liuxiaolong
2019-09-28 4c7890a506f95bfc8d06a75dbf6150bb8fcad1bb
extend/util/util.go
@@ -7,9 +7,12 @@
   "errors"
   "github.com/gin-gonic/gin"
   "github.com/golang/glog"
   "github.com/pierrec/lz4"
   "gocv.io/x/gocv"
   "image"
   "io/ioutil"
   "net"
   "net/http"
   "os/exec"
   "strconv"
   "webserver/extend/code"
@@ -74,6 +77,19 @@
   //   data[t.Field(i).Name] = v.Field(i).Interface()
   //}
   //return data
}
func ReadImgData(url string) ([]byte,error) {
   resp,err := http.Get(url)
   if err !=nil {
      return nil,err
   }
   defer resp.Body.Close()
   pix,err := ioutil.ReadAll(resp.Body)
   if err !=nil {
      return nil,err
   }
   return pix,nil
}
// 按尺寸去切图
@@ -192,6 +208,37 @@
}
func ParseScore(compareScore float32) float32 {
   f, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", compareScore), 32)
   if compareScore < 1 {
      compareScore = compareScore * 100
   }
   f, _ := strconv.ParseFloat(fmt.Sprintf("%2.2f", compareScore), 32)
   return float32(f)
}
}
// UnCompress uncompress
func UnCompress(in []byte) ([]byte, error) {
   out := make([]byte, 10*len(in))
   n, err := lz4.UncompressBlock(in, out)
   if err != nil {
      fmt.Println(err)
      return nil, err
   }
   out = out[:n] // uncompressed data
   return out, nil
}
// Compress compress
func Compress(in []byte) ([]byte, error) {
   out := make([]byte, len(in))
   ht := make([]int, 64<<10) // buffer for the compression table
   n, err := lz4.CompressBlock(in, out, ht)
   if err != nil {
      fmt.Println(err)
      return nil, err
   }
   if n >= len(in) {
      fmt.Println("image is not compressible")
   }
   out = out[:n] // compressed data
   return out, nil
}