|
@@ -1,6 +1,15 @@
|
|
|
package weixin
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
+ "encoding/base64"
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
+ "github.com/disintegration/imaging"
|
|
|
+ "io/ioutil"
|
|
|
+ "math"
|
|
|
+ "net/http"
|
|
|
+
|
|
|
"log"
|
|
|
"os"
|
|
|
"qfw/util"
|
|
@@ -9,6 +18,14 @@ import (
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
+const (
|
|
|
+ OCR_SERVERKEY = "1fb48799-5379-485c-9047-f886b535759b"
|
|
|
+ OCR_SERVICECODE = "cf22e3bb-d41c-47e0-aa44-a92984f5829d"
|
|
|
+)
|
|
|
+
|
|
|
+var upload_lock chan bool = make(chan bool, 500) //支持500并发
|
|
|
+var max_size float64 = 1200 //最大1200像素
|
|
|
+
|
|
|
//微信图像处理
|
|
|
func ImageMsgHandler(w ResponseWriter, r *Request) {
|
|
|
//TODO 先验证是否存在用户,
|
|
@@ -19,6 +36,11 @@ func ImageMsgHandler(w ResponseWriter, r *Request) {
|
|
|
return
|
|
|
}
|
|
|
processIndenfyUpload(us, openid, w, r)
|
|
|
+ } else { //走的是名片识别
|
|
|
+ //TODO 需要加上调用次数限制
|
|
|
+ upload_lock <- true
|
|
|
+ go cardRecognition(r.MediaId, r.FromUserName)
|
|
|
+ w.ReplyOK()
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -56,3 +78,82 @@ func getSavePath() string {
|
|
|
|
|
|
return path + name
|
|
|
}
|
|
|
+
|
|
|
+//名片识别
|
|
|
+func cardRecognition(mediaid, openid string) {
|
|
|
+ defer func() {
|
|
|
+ <-upload_lock
|
|
|
+ }()
|
|
|
+ //下载
|
|
|
+ picpath := getSavePath()
|
|
|
+ err := Mux.DownloadMediaToFile(mediaid, wf.SysConfig.Imgpath+picpath)
|
|
|
+ if err != nil {
|
|
|
+ log.Println(err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //图像处理
|
|
|
+ fi, _ := os.Open(wf.SysConfig.Imgpath + picpath)
|
|
|
+ srcimg, err := imaging.Decode(fi)
|
|
|
+ fi.Close()
|
|
|
+ if err != nil {
|
|
|
+ log.Println("read image file:::", err.Error())
|
|
|
+ }
|
|
|
+ bound := srcimg.Bounds().Size()
|
|
|
+ log.Println(bound.X, bound.Y)
|
|
|
+ w, h := bound.X, bound.Y
|
|
|
+ if max := math.Max(float64(w), float64(h)); max > max_size {
|
|
|
+ bl := max / max_size
|
|
|
+ w = int(float64(w) / bl)
|
|
|
+ h = int(float64(h) / bl)
|
|
|
+ }
|
|
|
+ small_file := wf.SysConfig.Imgpath + getSavePath()
|
|
|
+ destimg := imaging.Resize(srcimg, w, h, imaging.Linear)
|
|
|
+ imaging.Save(destimg, small_file)
|
|
|
+ //识别
|
|
|
+ fi, _ = os.Open(small_file)
|
|
|
+ bs, _ := ioutil.ReadAll(fi)
|
|
|
+ fi.Close()
|
|
|
+ hurl := fmt.Sprintf("http://api.hanvon.com/rt/ws/v1/ocr/bcard/recg?key=%s&code=%s",
|
|
|
+ OCR_SERVERKEY, OCR_SERVICECODE)
|
|
|
+ body := map[string]interface{}{
|
|
|
+ "uid": "123.160.231.13",
|
|
|
+ "color": "original",
|
|
|
+ "lang": "auto",
|
|
|
+ "image": base64.StdEncoding.EncodeToString(bs),
|
|
|
+ }
|
|
|
+ bs, _ = json.Marshal(body)
|
|
|
+ //
|
|
|
+ client := &http.Client{}
|
|
|
+ req, _ := http.NewRequest("POST", hurl, bytes.NewReader(bs))
|
|
|
+ req.Header.Set("Content-Type", "application/octet-stream")
|
|
|
+ resp, _ := client.Do(req) //发送
|
|
|
+ ret, _ := ioutil.ReadAll(resp.Body)
|
|
|
+ resp.Body.Close()
|
|
|
+ log.Println(string(ret))
|
|
|
+ ocr_ret := map[string]interface{}{}
|
|
|
+ json.Unmarshal(ret, &ocr_ret)
|
|
|
+ if v, ok := ocr_ret["comp"]; ok {
|
|
|
+ comps, _ := v.([]interface{})
|
|
|
+ if len(comps) > 0 {
|
|
|
+ searchcomp := comps[0].(string)
|
|
|
+ //发送客服消息
|
|
|
+ var msg struct {
|
|
|
+ ToUser string `json:"touser"`
|
|
|
+ MsgType string `json:"msgtype"`
|
|
|
+ Text struct {
|
|
|
+ Content string `json:"content"`
|
|
|
+ } `json:"text"`
|
|
|
+ }
|
|
|
+ msg.ToUser = openid
|
|
|
+ msg.MsgType = "text"
|
|
|
+ msg.Text.Content = searchcomp
|
|
|
+ bs, err := Mux.PostCustomMsg("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=", msg)
|
|
|
+ if err != nil {
|
|
|
+ log.Println(err.Error())
|
|
|
+ } else {
|
|
|
+ log.Println(string(bs))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|