-
Notifications
You must be signed in to change notification settings - Fork 2
/
grabCallback.go
149 lines (133 loc) · 3.99 KB
/
grabCallback.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package sdk_camera
/*
#cgo CFLAGS: -I./include/
#cgo LDFLAGS: -L./lib/64/ -lMvCameraControl -ldl
#include "CameraParams.h"
#include "MvCameraControl.h"
#include "callback.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
*/
import "C"
import (
"fmt"
gopointer "github.com/mattn/go-pointer"
"os"
"time"
"unsafe"
)
// 回调函数取图
func grabImageWithCallback() {
sdkVersion := C.MV_CC_GetSDKVersion()
fmt.Println("SDKVersion:", sdkVersion)
deviceList := &C.MV_CC_DEVICE_INFO_LIST{}
var tlayerType C.uint = C.MV_GIGE_DEVICE | C.MV_USB_DEVICE
//ch:枚举设备 | en:Enum device
var ret C.int
ret = C.MV_CC_EnumDevices(tlayerType, deviceList)
if ret != 0 {
fmt.Println("return signal is not zero:", fmt.Sprintf("0x%x", C.uint(ret)))
return
}
fmt.Println("device num:", deviceList.nDeviceNum)
handle := unsafe.Pointer(nil)
stDeviceList := deviceList.pDeviceInfo[0]
// 指定第一个设备并创建句柄
ret = C.MV_CC_CreateHandle(&handle, stDeviceList)
if ret != 0 {
fmt.Println("MV_CC_CreateHandle failure:", fmt.Sprintf("0x%x", C.uint(ret)))
return
}
defer func() {
ret = C.MV_CC_DestroyHandle(handle)
if ret != 0 {
fmt.Println("MV_CC_DestroyHandle failure:", fmt.Sprintf("0x%x", C.uint(ret)))
} else {
fmt.Println("MV_CC_DestroyHandle success")
}
}()
fmt.Println("handle:", handle)
// 打开设备
ret = C.MV_CC_OpenDevice(handle, C.MV_ACCESS_Exclusive, 0)
if ret != 0 {
fmt.Println("MV_CC_OpenDevice failure:", fmt.Sprintf("0x%x", C.uint(ret)))
return
}
defer func() {
ret = C.MV_CC_CloseDevice(handle)
if ret != 0 {
fmt.Println("MV_CC_CloseDevice failure:", fmt.Sprintf("0x%x", C.uint(ret)))
return
} else {
fmt.Println("MV_CC_CloseDevice success")
}
}()
// 探测网络最佳包大小(只对GigE相机有效)
if stDeviceList.nTLayerType == C.MV_GIGE_DEVICE {
var nPacketSize C.int = C.MV_CC_GetOptimalPacketSize(handle)
if nPacketSize > 0 {
ret = C.MV_CC_SetIntValue(handle, C.CString("GevSCPSPacketSize"), C.uint(nPacketSize))
if ret != 0 {
fmt.Println("MV_CC_SetIntValue failure:", fmt.Sprintf("0x%x", C.uint(ret)))
return
}
} else {
fmt.Println("MV_CC_GetOptimalPacketSize failure:", nPacketSize)
}
}
// ch:设置触发模式为off | en:Set trigger mode as off
ret = C.MV_CC_SetEnumValue(handle, C.CString("TriggerMode"), C.MV_TRIGGER_MODE_OFF)
if ret != 0 {
fmt.Println("MV_CC_SetEnumValue failure:", fmt.Sprintf("0x%x", ret))
return
}
// ch:获取数据包大小 | en:Get payload size
stParam := &C.MVCC_INTVALUE{}
ret = C.MV_CC_GetIntValue(handle, C.CString("PayloadSize"), stParam)
if ret != 0 {
fmt.Println("MV_CC_GetIntValue failure:", fmt.Sprintf("0x%x", C.uint(ret)))
return
}
// 先注册回调函数
cb := &CallbackStruct{
PortID: "测试摄像头1",
FamilyName: "测试摄像头family",
Callback: func(frameInfo FrameOutInfo) {
// 朋友,回调函数可以写这里
fmt.Printf("get one frame:[PortID:%v], [DataBuf len:%d], Width[%v], Height[%v], nFrameNum[%v]\n", frameInfo.PortID, len(frameInfo.DataBuf), frameInfo.Width, frameInfo.Height, frameInfo.FrameNum)
},
}
// 因为C中不能长期持有GO的指针对象,只能通过全局变量来映射
p := gopointer.Save(cb)
defer gopointer.Unref(p)
// C的指向函数的指针在Go中被视为*[0]byte,所以要转换一下
ret = C.MV_CC_RegisterImageCallBackEx(handle, (*[0]byte)(unsafe.Pointer(C.Callback)), p)
if ret != 0 {
fmt.Println("MV_CC_RegisterImageCallBackEx failure:", fmt.Sprintf("0x%x", C.uint(ret)))
return
}
// 开始取流
ret = C.MV_CC_StartGrabbing(handle)
if ret != 0 {
fmt.Println("MV_CC_StartGrabbing failure:", fmt.Sprintf("0x%x", C.uint(ret)))
return
}
defer func() {
ret = C.MV_CC_StopGrabbing(handle)
if ret != 0 {
fmt.Println("MV_CC_StopGrabbing failure:", fmt.Sprintf("0x%x", C.uint(ret)))
return
} else {
fmt.Println("MV_CC_StopGrabbing success")
}
}()
exitSignal := make(chan os.Signal, 1)
i := 0
for {
time.Sleep(time.Second)
fmt.Println(i)
i++
}
<-exitSignal
}