1 Star 1 Fork 0

gdxumeng/百度ocr-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
main.go 1.69 KB
一键复制 编辑 原始数据 按行查看 历史
gdxumeng 提交于 2022-05-30 00:57 . first
package main
import (
"baidu_ocr_api/ocr"
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
r.Use(CheckCors()) //开启中间件 允许使用跨域请求
//获取post过来的图片(base64编码)
r.POST("/get_account_ocr", func(c *gin.Context) {
image := c.PostForm("image")
text, err := ocr.OcrImg(image)
if err == nil {
c.JSON(200, gin.H{
"result": text,
})
}
})
r.POST("/get_i_ocr", func(c *gin.Context) {
image := c.PostForm("image")
templateId := c.PostForm("templateId")
text, err := ocr.GetIOcrImg(templateId, image)
if err == nil {
c.JSON(200, gin.H{
"result": text,
})
}
})
r.Run(":5002") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
func CheckCors() gin.HandlerFunc {
//这里可以处理一些别的逻辑
return func(c *gin.Context) {
// 定义一个origin的map,只有在字典中的key才允许跨域请求
var allowOrigins = map[string]struct{}{
"http://192.168.1.4": struct{}{},
}
origin := c.Request.Header.Get("Origin") //请求头部
method := c.Request.Method
if method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
}
if origin != "" {
if _, ok := allowOrigins[origin]; ok {
c.Header("Access-Control-Allow-Origin", origin)
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
c.Header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
c.Header("Access-Control-Allow-Credentials", "true")
}
}
c.Next()
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/gdxumeng/baidu-ocr-go.git
git@gitee.com:gdxumeng/baidu-ocr-go.git
gdxumeng
baidu-ocr-go
百度ocr-go
master

搜索帮助