robotgo_ocr.go 928 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // https://github.com/go-vgo/robotgo/blob/master/LICENSE
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. //go:build ocr
  11. // +build ocr
  12. package robotgo
  13. import (
  14. "github.com/otiai10/gosseract"
  15. )
  16. // GetText get the image text by tesseract ocr
  17. func GetText(imgPath string, args ...string) (string, error) {
  18. var lang = "eng"
  19. if len(args) > 0 {
  20. lang = args[0]
  21. if lang == "zh" {
  22. lang = "chi_sim"
  23. }
  24. }
  25. client := gosseract.NewClient()
  26. defer client.Close()
  27. client.SetImage(imgPath)
  28. client.SetLanguage(lang)
  29. return client.Text()
  30. }