ApiForCode
Golang 基于Gin框架的 Ocr验证码本地识别Api
- 实现Golang调用Ocr.dll(仅Windows 32位使用)通过设置日志级别可忽略dll调用的报错
//设置google日志级别
os.Setenv("GLOG_minloglevel", "4")
- 64位的Golang环境须使用32位运行 在Goland中可在运行配置-环境 添加如下:
GOARCH=386
- 实现图片验证码识别 提交图片的Base64编码即可
POST /api/ocr HTTP/1.1
Host: 127.0.0.1:9797
Content-Type: text/plain
Content-Length: 7410
{
"img":"iVBORw0KGgoAAAANSUhEUgAAAHsAAAAoCAIAAABRvxJxAAAVbUlEQVRogdWba6xtV3Xff2M+1mO/zt7ncc992PgN5mFAQHgVaEMMaYCEOAlpCUS4tE0Uqa2qfkg/Var6oV8qqkotRC1CVVsF0pgEsBRkMCSGGmiCKFBCSWwwNr7X93We+7Ve89EPa+9zzr22Uw45FnToaunctfaaa8z/HHPMMcf4T4kx8kNLBA+AgAZCIAaiIioilJ5MowDQILjQaGVDDOKjEkEUESKEgFKLRgWA5f8WH2jvy/IpRKKI0CrrAjESQSLKIwEMAkpHCKi4bEBFJEKAsLxlQNzT+xyeqbMCCiNPe/TXEXOsXwfwS1UArVr4lo9TjUBc4BlicDEIIQrKaAhEkEBUi/clgDq4RpRwBHqBJcIBIsQYFGhRGAUQgYCAqCW8qv1lKxGCoNs2Zam3PB1b2p9c90A4qs2JiRzLxh3MwYMGDYZgCBoOzZxD24wEh/cQEI0SQggh+MaI0VqUGAigAiEurwJq2ckFysu/A0CIPiiRRGkFBBoCSvkj80SWKrC8c01vQRGebrN/BQRy0qAfG/FqibiAJmhCC9NsXmSdTu28MSYQPARCJDQ0BmXQGpFFnw/lwCTbqaIQwXAE6OtELQf0wGrrpe3qpVbXte+XAxZBE2zrD59J4pGZEA+H7ceK+KFqtNMzsHTuDtXAhHlAgZ1TK6zFD1ApeLxeWDoR37gmNSnPgLiKERUJohY3jlytiEIrQgwIQcQcKhOWrRzMM0CBDh4aaAgRNGSoZ0P8WeTHi3gEH4hhoYdaGFtN2A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件上传转Base64
POST /api/img2base64 HTTP/1.1
Host: 127.0.0.1:9797
Content-Length: 193
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="img"; filename="/C:/Users/Test/Desktop/img.png"
Content-Type: image/png
(data)
----WebKitFormBoundary7MA4YWxkTrZu0gW
- 64位编译程序
C:\Users\Test\Desktop>cd /d D:
D:\>cd "Source Demo\go\ApiForCode
D:\Source Demo\go\ApiForCode>go env GOARCH
amd64
D:\Source Demo\go\ApiForCode>set GOARCH=386
D:\Source Demo\go\ApiForCode>go env GOARCH
386
D:\Source Demo\go\ApiForCode>go build
调用dll代码参考
package utils
import (
"io/ioutil"
"log"
"os"
"syscall"
"unsafe"
)
var dll syscall.Handle
var initFun uintptr
var ocrFunc uintptr
//InitDll 初始化orc
func InitDll() error {
//设置google日志级别
os.Setenv("GLOG_minloglevel", "4")
var err error
dll, err = syscall.LoadLibrary("ocr.dll")
if err != nil {
return err
}
initFun, err = syscall.GetProcAddress(dll, "init")
if err != nil {
return err
}
syscall.Syscall(initFun, 0, 0, 0, 0)
ocrFunc, err = syscall.GetProcAddress(dll, "ocr")
if err != nil {
return err
}
return nil
}
//GetCode 传入图片字节返回识别后的文本
func GetCode(img *[]byte) string {
p := *((*int32)(unsafe.Pointer(img)))
ret, _, _ := syscall.Syscall(ocrFunc, 2, uintptr(p), uintptr(len(*img)), 0)
return prt2Str(ret)
}
//prt2Str 转为字符
func prt2Str(vCode uintptr) string {
if vCode <= 0 {
return ""
}
var vBytes []byte
for {
sByte := *((*byte)(unsafe.Pointer(vCode)))
if sByte == 0 {
break
}
vBytes = append(vBytes, sByte)
vCode += 1
}
return string(vBytes)
}
//获取图片数据
func readImgData(path string) string {
file, err := os.Open(path)
if err != nil {
log.Println("打开验证码图片文件异常", err)
}
defer file.Close()
pix, err := ioutil.ReadAll(file)
if err != nil {
log.Println("读取验证码图片异常", err)
}
tmpCode := GetCode(&pix)
return tmpCode
}
func Free() {
syscall.FreeLibrary(dll)
}