|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "syscall" |
| 6 | + "unsafe" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + WM_CAP_START = 0x400 |
| 11 | + WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10 |
| 12 | + WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11 |
| 13 | + WM_CAP_SINGLE_FRAME_OPEN = WM_CAP_START + 70 |
| 14 | + WM_CAP_SINGLE_FRAME_CLOSE = WM_CAP_START + 71 |
| 15 | + WM_CAP_SINGLE_FRAME = WM_CAP_START + 72 |
| 16 | + WM_CAP_FILE_SAVEDIB = WM_CAP_START + 25 |
| 17 | + WS_VISIBLE = 0x10000000 |
| 18 | + WS_CHILD = 0x40000000 |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + avicap32 = syscall.NewLazyDLL("avicap32.dll") |
| 23 | + |
| 24 | + // AVICap32 API functions |
| 25 | + capCreateCaptureWindowW = avicap32.NewProc("capCreateCaptureWindowW") |
| 26 | + SendMessage = avicap32.NewProc("SendMessageW") |
| 27 | + DestroyWindow = avicap32.NewProc("DestroyWindow") |
| 28 | + capFileSaveDIB = avicap32.NewProc("capFileSaveDIB") |
| 29 | +) |
| 30 | + |
| 31 | +func main() { |
| 32 | + // Initialize the capture window |
| 33 | + captureWindow := initCaptureWindow() |
| 34 | + |
| 35 | + fmt.Println(captureWindow) |
| 36 | + if captureWindow == 0 { |
| 37 | + fmt.Println("Failed to initialize capture window") |
| 38 | + return |
| 39 | + } |
| 40 | + defer DestroyWindow.Call(captureWindow) |
| 41 | + |
| 42 | + // Start capturing |
| 43 | + _, _, _ = SendMessage.Call(captureWindow, WM_CAP_DRIVER_CONNECT, 0, 0) |
| 44 | + |
| 45 | + // Capture a single frame |
| 46 | + _, _, _ = SendMessage.Call(captureWindow, WM_CAP_SINGLE_FRAME_OPEN, 0, 0) |
| 47 | + _, _, _ = SendMessage.Call(captureWindow, WM_CAP_SINGLE_FRAME_CLOSE, 0, 0) |
| 48 | + |
| 49 | + // Save the captured image |
| 50 | + imageFile := "captured_image.bmp" |
| 51 | + _, _, _ = SendMessage.Call(captureWindow, WM_CAP_FILE_SAVEDIB, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(imageFile)))) |
| 52 | + |
| 53 | + // Stop capturing |
| 54 | + _, _, _ = SendMessage.Call(captureWindow, WM_CAP_DRIVER_DISCONNECT, 0, 0) |
| 55 | + |
| 56 | + fmt.Println("Image saved as", imageFile) |
| 57 | +} |
| 58 | + |
| 59 | +func initCaptureWindow() uintptr { |
| 60 | + // Create a capture window |
| 61 | + title := "Live! Cam Sync 1080p" |
| 62 | + titlePtr, err := syscall.UTF16PtrFromString(title) |
| 63 | + if err != nil { |
| 64 | + fmt.Println("Error converting title to UTF16:", err) |
| 65 | + return 0 |
| 66 | + } |
| 67 | + |
| 68 | + captureWindow, _, _ := capCreateCaptureWindowW.Call( |
| 69 | + uintptr(unsafe.Pointer(titlePtr)), |
| 70 | + uintptr(WS_VISIBLE|WS_CHILD), |
| 71 | + 0, 0, 320, 240, |
| 72 | + 0, 0, |
| 73 | + 0, 0, |
| 74 | + ) |
| 75 | + |
| 76 | + return captureWindow |
| 77 | +} |
0 commit comments