-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
78 lines (71 loc) · 1.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"syscall"
"unsafe"
)
var (
avicap32 = syscall.NewLazyDLL("avicap32.dll")
proccapCreateCaptureWindowA = avicap32.NewProc("capCreateCaptureWindowA")
user32 = syscall.NewLazyDLL("user32.dll")
procSendMessageA = user32.NewProc("SendMessageA")
)
func CaptureWebcam() {
var name = "WebcamCapture"
handle, _, _ := proccapCreateCaptureWindowA.Call(uintptr(unsafe.Pointer(&name)), 0, 0, 0, 320, 240, 0, 0)
procSendMessageA.Call(handle, 0x40A, 0, 0) //WM_CAP_DRIVER_CONNECT
procSendMessageA.Call(handle, 0x432, 30, 0) //WM_CAP_SET_PREVIEW
procSendMessageA.Call(handle, 0x43C, 0, 0) //WM_CAP_GRAB_FRAME
procSendMessageA.Call(handle, 0x41E, 0, 0) //WM_CAP_EDIT_COPY
procSendMessageA.Call(handle, 0x40B, 0, 0) //WM_CAP_DRIVER_DISCONNECT
camera, err := os.Create("Image.png")
if err != nil {
fmt.Println(err)
return
}
clip, err := readClipboard()
if err != nil {
fmt.Println(err)
return
}
_, err = io.Copy(camera, clip)
if err != nil {
fmt.Println(err)
return
}
camera.Close()
}
func readClipboard() (io.Reader, error) {
f, err := ioutil.TempFile("", "")
if err != nil {
fmt.Println(err)
return nil, err
}
f.Close()
_, err = exec.Command("PowerShell", "-Command", "Add-Type", "-AssemblyName", fmt.Sprintf("System.Windows.Forms;$clip=[Windows.Forms.Clipboard]::GetImage();if ($clip -ne $null) { $clip.Save('%s') };", f.Name())).CombinedOutput()
if err != nil {
fmt.Println(err)
return nil, err
}
r := new(bytes.Buffer)
file, err := os.Open(f.Name())
if err != nil {
fmt.Println(err)
return nil, err
}
if _, err := io.Copy(r, file); err != nil {
fmt.Println(err)
return nil, err
}
file.Close()
os.Remove(f.Name())
return r, nil
}
func main() {
CaptureWebcam()
}