-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera_windows.go
More file actions
80 lines (66 loc) · 1.41 KB
/
Copy pathcamera_windows.go
File metadata and controls
80 lines (66 loc) · 1.41 KB
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
79
80
package camera
/*
#cgo LDFLAGS: -lole32 -lmf -lmfplat -lmfuuid -lmfreadwrite
#include "camera_windows.c"
static void copyImage(uint8_t *dstBuf, void* srcBuf, size_t frame_size) {
memcpy(dstBuf, srcBuf, frame_size);
}
*/
import "C"
import (
"fmt"
"unsafe"
)
var (
running = false
)
func startCapture() {
if running {
return
}
running = true
go func() {
for running {
C.capture_frame()
}
running = false
}()
}
//export onFrameAvailableGo
func onFrameAvailableGo(data unsafe.Pointer, width, height, bytesPerPixel C.int) {
frameSize := int(width) * int(height) * int(bytesPerPixel)
go func() {
buf := make([]byte, frameSize)
C.copyImage((*C.uint8_t)(unsafe.Pointer(&buf[0])), data, C.size_t(frameSize))
// Convert the buffer to an image.RGBA
rgba := convertAndMirrorRGB24ToRGBA(buf, int(width), int(height))
select {
case frameBufferChan <- rgba:
default:
// Drop the frame if the channel is full
}
}()
}
func openCamera(id, width, height int) error {
if C.webcam_open(C.int(id), C.int(width), C.int(height)) != 0 {
return fmt.Errorf("failed to initialize camera")
}
return nil
}
func startCamera() error {
if C.webcam_start() != 0 {
return fmt.Errorf("failed to start camera")
}
startCapture()
return nil
}
func stopCamera() error {
running = false
if C.webcam_stop() != 0 {
return fmt.Errorf("failed to stop camera")
}
return nil
}
func closeCamera() {
C.webcam_delete()
}