diff --git a/dri/tencent/tencent.go b/dri/tencent/tencent.go new file mode 100644 index 0000000..b8bd138 --- /dev/null +++ b/dri/tencent/tencent.go @@ -0,0 +1,63 @@ +package tencent + +import ( + "fmt" + "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" + "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" + "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile" + ocr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ocr/v20181119" +) + +var credential = common.NewCredential( + "AKIDOgT8cKCQksnY7yKATaYO7j9ORJzSYohP", + "GNXgjdN4czA9ya0FNMApVJzTmsmU0KSN", +) + +func OCR(base64Str string) string { + + cpf := profile.NewClientProfile() + cpf.HttpProfile.Endpoint = "ocr.tencentcloudapi.com" + client, _ := ocr.NewClient(credential, "ap-guangzhou", cpf) + + request := ocr.NewGeneralAccurateOCRRequest() + + //request.ImageUrl = common.StringPtr("https://img0.baidu.com/it/u=2041013181,3227632688&fm=26&fmt=auto") + request.ImageBase64 = common.StringPtr(base64Str) + + response, err := client.GeneralAccurateOCR(request) + if _, ok := err.(*errors.TencentCloudSDKError); ok { + fmt.Println("An API error has returned: %s", err) + return "" + } + if err != nil { + fmt.Println("An API error has returned: %s", err) + return "" + } + //fmt.Printf("%s", response.ToJsonString()) + + return response.ToJsonString() +} + +func Qrcode(base64Str string) string { + + cpf := profile.NewClientProfile() + cpf.HttpProfile.Endpoint = "ocr.tencentcloudapi.com" + client, _ := ocr.NewClient(credential, "ap-guangzhou", cpf) + + request := ocr.NewQrcodeOCRRequest() + + request.ImageBase64 = common.StringPtr(base64Str) + + response, err := client.QrcodeOCR(request) + if _, ok := err.(*errors.TencentCloudSDKError); ok { + fmt.Println("An API error has returned: %s", err) + return "" + } + if err != nil { + fmt.Println("An API error has returned: %s", err) + return "" + } + //fmt.Printf("%s", response.ToJsonString()) + + return response.ToJsonString() +} diff --git a/example/main.go b/example/main.go index 5c2bd86..c202e5a 100644 --- a/example/main.go +++ b/example/main.go @@ -4,16 +4,20 @@ import ( "../../hotime" "../../hotime/common" "../dri/ddsms" + "../dri/tencent" "./admin" "./app" + "encoding/base64" "fmt" "io" + "io/ioutil" "os" "strings" "time" ) func main() { + date, _ := time.Parse("2006-01-02 15:04", time.Now().Format("2006-01-02")+" 14:00") fmt.Println(date, date.Unix()) //fmt.Println("0123456"[1:7]) @@ -35,6 +39,71 @@ func main() { } } + //文件上传接口 + if len(context.RouterString) == 1 && context.RouterString[0] == "ocr" && context.Req.Method == "POST" { + + //读取网络文件 + ocr := context.Req.FormValue("ocr") + if len(ocr) < 100 { + + context.Display(3, "没有上传文件") + return false + + } + + go func() { + filePath := context.Config.GetString("filePath") + if filePath == "" { + filePath = "file/2006/01/02/" + } + path := time.Now().Format(filePath) + e := os.MkdirAll(context.Config.GetString("tpt")+"/"+path, os.ModeDir) + if e != nil { + return + } + + fi, _ := base64.StdEncoding.DecodeString(ocr) + filePath = path + common.Md5(common.ObjToStr(common.RandX(100000, 9999999))) + ".jpg" + ioutil.WriteFile(context.Config.GetString("tpt")+"/"+filePath, fi, 0666) + }() + + re := tencent.OCR(ocr) + + re1 := tencent.Qrcode(ocr) + data := common.Map{} + //fmt.Println(re1) + data["text"] = common.ObjToMap(re) + + if data["text"] != nil { + lis := data.GetMap("text").GetMap("Response").GetSlice("TextDetections") + textList := common.Slice{} + for k, _ := range lis { + li := lis.GetMap(k).GetString("DetectedText") + if li != "" { + textList = append(textList, li) + } + } + data["text"] = textList + } + + data["qrcode"] = common.ObjToMap(re1) + if data["qrcode"] != nil { + lis := data.GetMap("qrcode").GetMap("Response").GetSlice("CodeResults") + textList := common.Slice{} + for k, _ := range lis { + li := lis.GetMap(k).GetString("Url") + if li != "" { + textList = append(textList, li) + } + } + data["qrcode"] = textList + } + + fmt.Println(data.ToJsonString()) + context.Display(0, data) + return false + } + //文件上传接口 if len(context.RouterString) == 1 && context.RouterString[0] == "file" && context.Req.Method == "POST" {