Skip to content

Commit 7dc869a

Browse files
committed
feat: add GetText - ocr
1 parent c78ebe5 commit 7dc869a

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

main.go

+11
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,15 @@ func KeyTap(key *C.char, vals *C.char) *C.char {
190190
return ch("")
191191
}
192192

193+
//export GetText
194+
func GetText(imgPath *C.char) *C.char {
195+
result, err := robotgo.GetText(str(imgPath))
196+
197+
resultAndError, _ := json.Marshal(&ResultAndError{
198+
Result: result,
199+
Error: sf(err),
200+
})
201+
return ch(string(resultAndError))
202+
}
203+
193204
func main() {} // Required but ignored

play.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ const bb = new Bunbot()
55
setTimeout(() => {
66
console.log(123)
77

8-
const mousePos = bb.getMousePosition()
9-
console.log(mousePos)
10-
bb.type(`Mouse coords: ${JSON.stringify(mousePos)}`)
11-
bb.tap('enter')
12-
const screenSize = bb.getScreenSize()
13-
console.log(screenSize)
14-
bb.type(`Screen size: ${JSON.stringify(screenSize)}`)
8+
// const mousePos = bb.getMousePosition()
9+
// console.log(mousePos)
10+
// bb.type(`Mouse coords: ${JSON.stringify(mousePos)}`)
11+
// bb.tap('enter')
12+
// const screenSize = bb.getScreenSize()
13+
// console.log(screenSize)
14+
// bb.type(`Screen size: ${JSON.stringify(screenSize)}`)
15+
// bb.type('Hello world')
16+
bb.getText('./sample.webp')
1517
}, 2000)

sample.webp

16 KB
Binary file not shown.

src/ffi.ts

+4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ export const { symbols } = dlopen(location, {
6060
args: [FFIType.ptr, FFIType.ptr],
6161
returns: FFIType.ptr
6262
},
63+
GetText: {
64+
args: [FFIType.ptr],
65+
returns: FFIType.ptr
66+
},
6367
FreeString: {
6468
args: [FFIType.ptr],
6569
returns: FFIType.void

src/index.ts

+14
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ export class Bunbot {
7979
const modifiersPtr = toPtr(encode(modifiers))
8080
symbols.KeyTap(keyPtr, modifiersPtr)
8181
}
82+
83+
getText(imagePath: string) {
84+
const ptr = symbols.GetText(toPtr(encode(imagePath)))
85+
const { result, error } = JSON.parse(toString(ptr)) as {
86+
result: string,
87+
error: string
88+
}
89+
90+
if (error !== '') {
91+
throw new Error(error)
92+
}
93+
94+
return result
95+
}
8296
}
8397

8498
export default Bunbot

0 commit comments

Comments
 (0)