|
| 1 | +package SuScreen |
| 2 | + |
| 3 | +import ( |
| 4 | + KeyTool "GoRobotScript/GokeyLog/Tool" |
| 5 | + "fmt" |
| 6 | + "github.com/go-vgo/robotgo" // 导入robotgo库 |
| 7 | + lua "github.com/yuin/gopher-lua" // 导入gopher-lua库 |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strconv" // 导入strconv库 |
| 11 | + "strings" // 导入strings库 |
| 12 | +) |
| 13 | + |
| 14 | +var SuBitmap = make(map[string]robotgo.CBitmap) /*创建集合 */ |
| 15 | + |
| 16 | +func Loader(L *lua.LState) int { |
| 17 | + // register functions to the table |
| 18 | + mod := L.SetFuncs(L.NewTable(), exports) // 将函数注册到table中 |
| 19 | + // register other stuff |
| 20 | + L.SetField(mod, "name", lua.LString("value")) // 将name字段设置为value |
| 21 | + |
| 22 | + // returns the module |
| 23 | + L.Push(mod) // 将mod压入栈中 |
| 24 | + return 1 // 返回1 |
| 25 | +} |
| 26 | + |
| 27 | +// 定义一个map类型的变量exports,其中key为string类型,value为lua.LGFunction类型 |
| 28 | +var exports = map[string]lua.LGFunction{ |
| 29 | + "CaptureScreen": captureScreen, // 将captureScreen函数注册到exports中 |
| 30 | + "SaveBitmap": saveBitmap, // 将saveBitmap函数注册到exports中 |
| 31 | + "ReadBitmap": readBitmap, |
| 32 | + "ReadAllBitmap": readAllBitmap, |
| 33 | +} |
| 34 | + |
| 35 | +func readAllBitmap(L *lua.LState) int { |
| 36 | + path := L.ToString(1) |
| 37 | + var files []string |
| 38 | + err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + if !info.IsDir() { |
| 43 | + switch filepath.Ext(path) { |
| 44 | + case ".png", ".jpg", "": |
| 45 | + files = append(files, strings.TrimSuffix(path, filepath.Ext(path))) |
| 46 | + } |
| 47 | + } |
| 48 | + return nil |
| 49 | + }) |
| 50 | + if err != nil { |
| 51 | + panic(err) |
| 52 | + } |
| 53 | + for _, file := range files { |
| 54 | + GreadBitmap(strings.ReplaceAll(file, "\\", "/")) // 把添加到files的反斜杠路径改为正斜杠路径 |
| 55 | + } |
| 56 | + return 0 |
| 57 | +} |
| 58 | + |
| 59 | +// captureScreen函数接收一个LState类型的参数L,返回一个int类型的值 |
| 60 | +// 从L中获取第一个参数,转换为table类型 |
| 61 | +func captureScreen(L *lua.LState) int { |
| 62 | + name := L.ToString(1) // 获取第一个参数并转换为string类型 |
| 63 | + tbl := L.ToTable(2) // 获取第二个参数并转换为table类型 |
| 64 | + var arr []int // 定义一个int类型的数组 |
| 65 | + tbl.ForEach(func(i lua.LValue, j lua.LValue) { |
| 66 | + // 将table中的值转换为int类型 |
| 67 | + if num, err := strconv.Atoi(j.String()); err == nil { |
| 68 | + arr = append(arr, num) // 将转换后的值添加到数组中 |
| 69 | + } |
| 70 | + }) |
| 71 | + //bit := robotgo.GoCaptureScreen(arr[0], arr[1], arr[2], arr[3]) |
| 72 | + SuBitmap[name] = robotgo.CaptureScreen(arr[0], arr[1], arr[2], arr[3]) // 将截图保存到SuBitmap中 |
| 73 | + L.SetGlobal("SuBitmap", L.NewUserData()) // 将SuBitmap设置为全局变量 |
| 74 | + return 0 // 返回0 |
| 75 | +} |
| 76 | + |
| 77 | +// saveBitmap函数接收一个LState类型的参数L,返回一个int类型的值 |
| 78 | +func saveBitmap(L *lua.LState) int { |
| 79 | + haven := false |
| 80 | + name := L.ToString(1) // 获取第一个参数并转换为string类型 |
| 81 | + _, ok := SuBitmap[name] |
| 82 | + if ok { |
| 83 | + haven = true |
| 84 | + //fmt.Println(GetDir(name)) |
| 85 | + path, _ := GetDir(name) |
| 86 | + filename, _ := GetFileName(name) |
| 87 | + if L.GetTop() == 1 { |
| 88 | + |
| 89 | + robotgo.Save(robotgo.ToImage(SuBitmap[name]), filepath.Join(path, filename)) // 保存截图 |
| 90 | + return 0 // 返回0 |
| 91 | + } |
| 92 | + end := strings.ToLower(L.ToString(2)) |
| 93 | + switch end { |
| 94 | + case ".png": |
| 95 | + robotgo.SavePng(robotgo.ToImage(SuBitmap[name]), filepath.Join(path, filename)+".png") // 保存为png格式 |
| 96 | + case ".jpg": |
| 97 | + robotgo.SaveJpeg(robotgo.ToImage(SuBitmap[name]), filepath.Join(path, filename)+".jpg") // 保存为jpg格式 |
| 98 | + default: |
| 99 | + robotgo.Save(robotgo.ToImage(SuBitmap[name]), filepath.Join(path, filename)+end) // 保存为默认格式 |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + L.Push(lua.LBool(haven)) |
| 104 | + return 1 // 返回1 |
| 105 | +} |
| 106 | +func readBitmap(L *lua.LState) int { |
| 107 | + name := L.ToString(1) // 获取第一个参数并转换为string |
| 108 | + L.Push(lua.LBool(GreadBitmap(name))) |
| 109 | + return 1 // 返回1 |
| 110 | +} |
| 111 | +func GreadBitmap(name string) bool { |
| 112 | + haven := false |
| 113 | + dir, _ := GetDir("") |
| 114 | + end := []string{".png", ".jpg", ""} |
| 115 | + var file1 = "" |
| 116 | + for _, s := range end { |
| 117 | + file1 = filepath.Join(dir, name+s) |
| 118 | + if _, err := os.Stat(file1); err == nil { |
| 119 | + file1 = filepath.Join(dir, name+s) |
| 120 | + break |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + println(file1) |
| 125 | + img, err := robotgo.Read(file1) |
| 126 | + if err != nil { |
| 127 | + fmt.Println("Error: ", err.Error()+" "+name) // 提示错误信息 |
| 128 | + } else { |
| 129 | + SuBitmap[name] = robotgo.ImgToCBitmap(img) |
| 130 | + haven = true |
| 131 | + } |
| 132 | + return haven // 返回1 |
| 133 | +} |
| 134 | + |
| 135 | +func GetDir(name string) (path string, err error) { |
| 136 | + scriptpath, err := KeyTool.GetAppPath() // 获取当前应用程序的路径 Get the path of the current application |
| 137 | + if err != nil { |
| 138 | + return "", err // 如果获取失败,返回错误 Return error if failed to get |
| 139 | + } |
| 140 | + // 获取文件的父路径 |
| 141 | + dir := filepath.Dir(name) |
| 142 | + path = filepath.Join(scriptpath, dir) // 将应用程序路径与相对路径拼接 Join the application path with the relative path |
| 143 | + _, err = os.Stat(path) // 获取脚本文件夹的信息 Get information about the script folder |
| 144 | + if os.IsNotExist(err) { // 如果脚本文件夹不存在 If the script folder does not exist |
| 145 | + err = os.MkdirAll(path, os.ModePerm) // 创建脚本文件夹 Create the script folder |
| 146 | + if err != nil { |
| 147 | + return "", err // 如果创建失败,返回错误 Return error if failed to create |
| 148 | + } |
| 149 | + } |
| 150 | + return path, nil // 返回脚本文件夹路径 Return the path of the script folder |
| 151 | +} |
| 152 | +func GetFileName(name string) (path string, err error) { |
| 153 | + _, fileName := filepath.Split(name) // 获取文件名 |
| 154 | + return fileName, nil // 返回文件名 |
| 155 | +} |
| 156 | + |
| 157 | +// GetFileEnd GetFileEnd函数接收一个string类型的参数name,返回一个string类型的值 |
| 158 | +// 仅获取文件拓展名 |
| 159 | +func GetFileEnd(name string) (path string, err error) { |
| 160 | + _, fileName := filepath.Split(name) // 获取文件名 |
| 161 | + return filepath.Ext(fileName), nil // 返回文件拓展名 |
| 162 | +} |
0 commit comments