ocr增加

This commit is contained in:
hoteas 2021-12-09 10:58:53 +08:00
parent 1e82a5964d
commit af726fbcfb
2 changed files with 132 additions and 0 deletions

63
dri/tencent/tencent.go Normal file
View File

@ -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()
}

View File

@ -4,16 +4,20 @@ import (
"../../hotime" "../../hotime"
"../../hotime/common" "../../hotime/common"
"../dri/ddsms" "../dri/ddsms"
"../dri/tencent"
"./admin" "./admin"
"./app" "./app"
"encoding/base64"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"strings" "strings"
"time" "time"
) )
func main() { func main() {
date, _ := time.Parse("2006-01-02 15:04", time.Now().Format("2006-01-02")+" 14:00") date, _ := time.Parse("2006-01-02 15:04", time.Now().Format("2006-01-02")+" 14:00")
fmt.Println(date, date.Unix()) fmt.Println(date, date.Unix())
//fmt.Println("0123456"[1:7]) //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" { if len(context.RouterString) == 1 && context.RouterString[0] == "file" && context.Req.Method == "POST" {