| | |
| | | import ( |
| | | "encoding/base64" |
| | | "encoding/json" |
| | | "gat1400Exchange/client" |
| | | "time" |
| | | |
| | | "gat1400Exchange/client" |
| | | "gat1400Exchange/config" |
| | | "gat1400Exchange/models" |
| | | "gat1400Exchange/pkg/logger" |
| | |
| | | |
| | | // 获取大图, 目前海康的小图分辨率太低 |
| | | for _, image := range face.SubImageList.SubImageInfoObject { |
| | | if len(image.Data) > len(bgImageStr) { |
| | | bgImageStr = image.Data |
| | | imageType = image.Type |
| | | if imageType != "14" { |
| | | continue |
| | | } |
| | | |
| | | if len(image.Data) > 0 { |
| | | if len(image.Data) > len(bgImageStr) { |
| | | bgImageStr = image.Data |
| | | } |
| | | } else if image.StoragePath != "" { |
| | | imgData, err := util.ImageDownload(image.StoragePath, nil) |
| | | if err != nil { |
| | | logger.Warn("Image download failure, %s", err.Error()) |
| | | } else { |
| | | bgImageBytes = imgData |
| | | bgImageStr = "///" |
| | | } |
| | | } |
| | | } |
| | | |
| | | bgImageBytes, err = base64.StdEncoding.DecodeString(bgImageStr) |
| | | if err != nil { |
| | | logger.Warn("Decode Image Base64 String failure, %s", err.Error()) |
| | | continue |
| | | if bgImageBytes == nil { |
| | | bgImageBytes, err = base64.StdEncoding.DecodeString(bgImageStr) |
| | | if err != nil { |
| | | logger.Warn("Decode Image Base64 String failure, %s", err.Error()) |
| | | continue |
| | | } |
| | | } |
| | | |
| | | // 判断图片类型是否为场景图, 根据人脸坐标切小图. |
| | |
| | | } |
| | | |
| | | // 转发图像 |
| | | logger.Debug("Prepare forward image, deviceId:%s, image len:%d, server:%s", deviceId, len(bgImageStr), config.ForwardConf.SyncServer) |
| | | logger.Debug("Prepare forward image, deviceId:%s, image len:%d, server:%s", deviceId, len(bgImageBytes), config.ForwardConf.SyncServer) |
| | | if deviceId != "" && bgImageStr != "" && config.ForwardConf.SyncServer != "" { |
| | | pd := c.PackPushDataV2(deviceId, faceId, face.FaceAppearTime, bgImageBytes, faceImageBytes) |
| | | if pd == nil { |
| | | return |
| | | } |
| | | |
| | | // 部署在服务端的1400代理, 仅接收1400信息, 楼层信息暂存在oherFeature字段 |
| | | if config.ServeConf.Role == "proxy" { |
| | | if face.OtherFeature != "" { |
| | | pd.CameraFloor = face.OtherFeature |
| | | } |
| | | // 处理梯控填写的楼层信息 暂时使用oherFeature字段 |
| | | if face.OtherFeature != "" { |
| | | pd.CameraFloor = face.OtherFeature |
| | | } |
| | | |
| | | payload, err := json.Marshal(pd) |
| | |
| | | import ( |
| | | "bytes" |
| | | "encoding/json" |
| | | "errors" |
| | | "fmt" |
| | | "io" |
| | | "net/http" |
| | | "strconv" |
| | | "time" |
| | | |
| | | "gat1400Exchange/pkg/logger" |
| | | "gat1400Exchange/vo" |
| | |
| | | |
| | | return body, nil |
| | | } |
| | | |
| | | func ImageDownload(downloadUrl string, fb func(length, downLen int64)) ([]byte, error) { |
| | | var ( |
| | | fSize int64 |
| | | buf = make([]byte, 10*1024) |
| | | buffer bytes.Buffer |
| | | written int64 |
| | | ) |
| | | |
| | | request, err := http.NewRequest("GET", downloadUrl, nil) |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | | |
| | | //创建一个http client |
| | | client := new(http.Client) |
| | | client.Timeout = time.Second * 600 //设置超时时间 |
| | | |
| | | // get方法获取资源 |
| | | resp, err := client.Do(request) |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | | |
| | | if resp.StatusCode != 200 { |
| | | return nil, errors.New(fmt.Sprintf("请求失败, 状态码:%d", resp.StatusCode)) |
| | | } |
| | | |
| | | //读取服务器返回的文件大小 |
| | | fSize, err = strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 32) |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | | |
| | | if resp.Body == nil { |
| | | return nil, errors.New("body is null") |
| | | } |
| | | |
| | | defer resp.Body.Close() |
| | | |
| | | //下面是 io.copyBuffer() 的简化版本 |
| | | for { |
| | | //读取bytes |
| | | nr, er := resp.Body.Read(buf) |
| | | if nr > 0 { |
| | | //写入bytes |
| | | nw, ew := buffer.Write(buf[0:nr]) |
| | | //数据长度大于0 |
| | | if nw > 0 { |
| | | written += int64(nw) |
| | | } |
| | | |
| | | //写入出错 |
| | | if ew != nil { |
| | | err = ew |
| | | break |
| | | } |
| | | |
| | | //读取是数据长度不等于写入的数据长度 |
| | | if nr != nw { |
| | | err = io.ErrShortWrite |
| | | break |
| | | } |
| | | } |
| | | |
| | | if er != nil { |
| | | if er != io.EOF { |
| | | err = er |
| | | } |
| | | break |
| | | } |
| | | |
| | | //没有错误了快使用 callback |
| | | if fb != nil { |
| | | fb(fSize, written) |
| | | } |
| | | } |
| | | |
| | | return buffer.Bytes(), err |
| | | } |