mirror of
https://github.com/go-vgo/robotgo.git
synced 2025-06-01 06:33:56 +00:00
add simple ocr support
This commit is contained in:
parent
530b8d6960
commit
2fbd770917
20
robotgo.go
20
robotgo.go
@ -61,6 +61,7 @@ import (
|
||||
"time"
|
||||
"unsafe"
|
||||
// "syscall"
|
||||
"os/exec"
|
||||
|
||||
"github.com/go-vgo/robotgo/clipboard"
|
||||
"github.com/shirou/gopsutil/process"
|
||||
@ -786,6 +787,25 @@ func TocharBitmap(bit C.MMBitmapRef) *C.char {
|
||||
return strBit
|
||||
}
|
||||
|
||||
// GetText get the image text by tesseract ocr
|
||||
func GetText(imgPath string, args ...string) (string, error) {
|
||||
var lang = "eng"
|
||||
|
||||
if len(args) > 0 {
|
||||
lang = args[0]
|
||||
if lang == "zh" {
|
||||
lang = "chi_sim"
|
||||
}
|
||||
}
|
||||
|
||||
body, err := exec.Command("tesseract", imgPath,
|
||||
"stdout", "-l", lang).Output()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(body), nil
|
||||
}
|
||||
|
||||
func internalFindBitmap(bit, sbit C.MMBitmapRef, tolerance float64) (int, int) {
|
||||
pos := C.find_bitmap(bit, sbit, C.float(tolerance))
|
||||
// fmt.Println("pos----", pos)
|
||||
|
26
robotgo_ocr.go
Normal file
26
robotgo_ocr.go
Normal file
@ -0,0 +1,26 @@
|
||||
// +build ocr
|
||||
|
||||
package robotgo
|
||||
|
||||
import (
|
||||
"github.com/otiai10/gosseract"
|
||||
)
|
||||
|
||||
// GetText get the image text by tesseract ocr
|
||||
func GetText(imgPath string, args ...string) (string, error) {
|
||||
var lang = "eng"
|
||||
|
||||
if len(args) > 0 {
|
||||
lang = args[0]
|
||||
if lang == "zh" {
|
||||
lang = "chi_sim"
|
||||
}
|
||||
}
|
||||
|
||||
client := gosseract.NewClient()
|
||||
defer client.Close()
|
||||
|
||||
client.SetImage(imgPath)
|
||||
client.SetLanguage(lang)
|
||||
return client.Text()
|
||||
}
|
Loading…
Reference in New Issue
Block a user