#
Junjie
4 天以前 0ecd4a0ec8c4c5585cbd8975d7786c5618814381
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
海康工业相机管理类 - 支持多相机选择和操作
"""
import time
from MvImport.MvCameraControl_class import *
 
 
class HikCameraManager:
    """海康相机管理器"""
    
    def __init__(self):
        self.cam = None
        self.is_initialized = False
        
    def initialize(self):
        """初始化SDK"""
        ret = MvCamera.MV_CC_Initialize()
        if ret != 0:
            raise Exception(f"初始化SDK失败! 错误码: {hex(ret)}")
        self.is_initialized = True
        print("SDK初始化成功")
        
    def list_cameras(self):
        """
        列出所有可用相机
        返回: 相机信息列表
        """
        if not self.is_initialized:
            self.initialize()
            
        deviceList = MV_CC_DEVICE_INFO_LIST()
        ret = MvCamera.MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, deviceList)
        
        if ret != 0:
            raise Exception(f"枚举设备失败! 错误码: {hex(ret)}")
            
        if deviceList.nDeviceNum == 0:
            print("未找到相机设备")
            return []
        
        cameras = []
        for i in range(deviceList.nDeviceNum):
            mvcc_dev_info = cast(deviceList.pDeviceInfo[i], POINTER(MV_CC_DEVICE_INFO)).contents
            
            camera_info = {
                'index': i,
                'device_info': mvcc_dev_info,
                'type': None,
                'model': '',
                'serial': '',
                'ip': '',
                'user_defined_name': ''
            }
            
            if mvcc_dev_info.nTLayerType == MV_GIGE_DEVICE:
                camera_info['type'] = 'GigE'
                
                # 型号
                model_name = ""
                for per in mvcc_dev_info.SpecialInfo.stGigEInfo.chModelName:
                    if per == 0:
                        break
                    model_name += chr(per)
                camera_info['model'] = model_name.strip()
                
                # 序列号
                serial = ""
                for per in mvcc_dev_info.SpecialInfo.stGigEInfo.chSerialNumber:
                    if per == 0:
                        break
                    serial += chr(per)
                camera_info['serial'] = serial.strip()
                
                # IP地址
                nip1 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24)
                nip2 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16)
                nip3 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8)
                nip4 = (mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff)
                camera_info['ip'] = f"{nip1}.{nip2}.{nip3}.{nip4}"
                
                # 用户自定义名称
                user_name = ""
                for per in mvcc_dev_info.SpecialInfo.stGigEInfo.chUserDefinedName:
                    if per == 0:
                        break
                    user_name += chr(per)
                camera_info['user_defined_name'] = user_name.strip()
                
            elif mvcc_dev_info.nTLayerType == MV_USB_DEVICE:
                camera_info['type'] = 'USB'
                
                # 型号
                model_name = ""
                for per in mvcc_dev_info.SpecialInfo.stUsb3VInfo.chModelName:
                    if per == 0:
                        break
                    model_name += chr(per)
                camera_info['model'] = model_name.strip()
                
                # 序列号
                serial = ""
                for per in mvcc_dev_info.SpecialInfo.stUsb3VInfo.chSerialNumber:
                    if per == 0:
                        break
                    serial += chr(per)
                camera_info['serial'] = serial.strip()
                
                # 用户自定义名称
                user_name = ""
                for per in mvcc_dev_info.SpecialInfo.stUsb3VInfo.chUserDefinedName:
                    if per == 0:
                        break
                    user_name += chr(per)
                camera_info['user_defined_name'] = user_name.strip()
            
            cameras.append(camera_info)
        
        return cameras
    
    def print_cameras(self):
        """打印所有相机信息"""
        cameras = self.list_cameras()
        
        if not cameras:
            print("未找到相机")
            return
        
        print(f"\n找到 {len(cameras)} 个相机:")
        print("=" * 80)
        
        for cam in cameras:
            print(f"\n[{cam['index']}] {cam['type']} 相机")
            print(f"  型号: {cam['model']}")
            print(f"  序列号: {cam['serial']}")
            if cam['ip']:
                print(f"  IP地址: {cam['ip']}")
            if cam['user_defined_name']:
                print(f"  自定义名称: {cam['user_defined_name']}")
        
        print("=" * 80)
    
    def open_camera(self, index=None, serial=None, ip=None, model=None):
        """
        打开指定相机
        
        参数:
            index: 相机索引(0, 1, 2...)
            serial: 相机序列号
            ip: 相机IP地址(仅GigE相机)
            model: 相机型号
            
        优先级: serial > ip > model > index
        """
        cameras = self.list_cameras()
        
        if not cameras:
            raise Exception("未找到相机设备")
        
        # 根据条件选择相机
        selected_camera = None
        
        if serial:
            # 按序列号查找
            for cam in cameras:
                if cam['serial'] == serial:
                    selected_camera = cam
                    print(f"通过序列号选择相机: {serial}")
                    break
            if not selected_camera:
                raise Exception(f"未找到序列号为 {serial} 的相机")
                
        elif ip:
            # 按IP地址查找(仅GigE)
            for cam in cameras:
                if cam['ip'] == ip:
                    selected_camera = cam
                    print(f"通过IP地址选择相机: {ip}")
                    break
            if not selected_camera:
                raise Exception(f"未找到IP地址为 {ip} 的相机")
                
        elif model:
            # 按型号查找
            for cam in cameras:
                if model in cam['model']:
                    selected_camera = cam
                    print(f"通过型号选择相机: {model}")
                    break
            if not selected_camera:
                raise Exception(f"未找到型号包含 {model} 的相机")
                
        else:
            # 按索引选择(默认第一个)
            if index is None:
                index = 0
            if index >= len(cameras):
                raise Exception(f"相机索引 {index} 超出范围(共 {len(cameras)} 个相机)")
            selected_camera = cameras[index]
            print(f"通过索引选择相机: {index}")
        
        # 打印选中的相机信息
        print(f"选中相机: [{selected_camera['type']}] {selected_camera['model']} (序列号: {selected_camera['serial']})")
        
        # 创建句柄
        self.cam = MvCamera()
        ret = self.cam.MV_CC_CreateHandle(selected_camera['device_info'])
        if ret != 0:
            raise Exception(f"创建句柄失败! 错误码: {hex(ret)}")
        
        # 打开设备
        ret = self.cam.MV_CC_OpenDevice(MV_ACCESS_Exclusive, 0)
        if ret != 0:
            # 尝试控制模式
            ret = self.cam.MV_CC_OpenDevice(MV_ACCESS_Control, 0)
            if ret != 0:
                self.cam.MV_CC_DestroyHandle()
                raise Exception(f"打开设备失败! 错误码: {hex(ret)}")
        
        print("相机打开成功")
        return selected_camera
    
    def capture_image(self, filename="captured_image", storageAddress = "D:pic/", save_bmp=True, save_jpg=True, timeout=3000):
        """
        采集一帧图像
        
        参数:
            filename: 文件名(不含扩展名)
            save_bmp: 是否保存BMP格式
            save_jpg: 是否保存JPG格式
            timeout: 超时时间(毫秒)
        """
        if not self.cam:
            raise Exception("相机未打开")
        
        # 设置触发模式为连续采集
        ret = self.cam.MV_CC_SetEnumValue("TriggerMode", MV_TRIGGER_MODE_OFF)
        if ret != 0:
            print(f"警告: 设置触发模式失败! 错误码: {hex(ret)}")
        
        # 开始取流
        ret = self.cam.MV_CC_StartGrabbing()
        if ret != 0:
            raise Exception(f"开始取流失败! 错误码: {hex(ret)}")
        
        print("开始采集...")
        time.sleep(1)  # 等待相机稳定
        
        # 获取图像
        stFrameInfo = MV_FRAME_OUT()
        memset(byref(stFrameInfo), 0, sizeof(stFrameInfo))
        
        ret = self.cam.MV_CC_GetImageBuffer(stFrameInfo, timeout)
        if ret != 0:
            self.cam.MV_CC_StopGrabbing()
            raise Exception(f"获取图像失败! 错误码: {hex(ret)}")
        
        print(f"成功获取图像: {stFrameInfo.stFrameInfo.nWidth}x{stFrameInfo.stFrameInfo.nHeight}")
        
        # 转换并保存图像
        saved_files = []
        
        if save_bmp:
            bmp_file = f"{storageAddress}{filename}.bmp"
            if self._save_image(stFrameInfo, bmp_file, MV_Image_Bmp):
                saved_files.append(bmp_file)
        
        if save_jpg:
            jpg_file = f"{storageAddress}{filename}.jpg"
            if self._save_image(stFrameInfo, jpg_file, MV_Image_Jpeg):
                saved_files.append(jpg_file)
        
        # 释放图像缓存
        self.cam.MV_CC_FreeImageBuffer(stFrameInfo)
        
        # 停止取流
        self.cam.MV_CC_StopGrabbing()
        
        return saved_files
    
    def _save_image(self, stFrameInfo, filename, image_type):
        """内部方法:保存图像"""
        stConvertParam = MV_SAVE_IMAGE_PARAM_EX()
        memset(byref(stConvertParam), 0, sizeof(stConvertParam))
        
        nBufSize = stFrameInfo.stFrameInfo.nWidth * stFrameInfo.stFrameInfo.nHeight * 3 + 2048
        pBuf = (c_ubyte * nBufSize)()
        
        stConvertParam.nWidth = stFrameInfo.stFrameInfo.nWidth
        stConvertParam.nHeight = stFrameInfo.stFrameInfo.nHeight
        stConvertParam.pData = stFrameInfo.pBufAddr
        stConvertParam.nDataLen = stFrameInfo.stFrameInfo.nFrameLen
        stConvertParam.enPixelType = stFrameInfo.stFrameInfo.enPixelType
        stConvertParam.pImageBuffer = cast(pBuf, POINTER(c_ubyte))
        stConvertParam.nBufferSize = nBufSize
        stConvertParam.enImageType = image_type
        stConvertParam.nJpgQuality = 90
        
        ret = self.cam.MV_CC_SaveImageEx2(stConvertParam)
        if ret != 0:
            print(f"图像转换失败! 错误码: {hex(ret)}")
            return False
        
        try:
            with open(filename, "wb") as f:
                f.write(bytearray(pBuf[0:stConvertParam.nImageLen]))
            print(f"✓ 图像已保存: {filename}")
            return True
        except Exception as e:
            print(f"保存文件失败: {e}")
            return False
    
    def close_camera(self):
        """关闭相机"""
        if self.cam:
            self.cam.MV_CC_CloseDevice()
            self.cam.MV_CC_DestroyHandle()
            self.cam = None
            print("相机已关闭")
    
    def finalize(self):
        """清理SDK资源"""
        self.close_camera()
        if self.is_initialized:
            MvCamera.MV_CC_Finalize()
            self.is_initialized = False
            print("SDK已清理")
    
    def __enter__(self):
        """支持with语句"""
        self.initialize()
        return self
    
    def __exit__(self, exc_type, exc_val, exc_tb):
        """支持with语句"""
        self.finalize()
 
 
# 使用示例
if __name__ == "__main__":
    try:
        with HikCameraManager() as manager:
            # 1. 列出所有相机
            manager.print_cameras()
            
            # 2. 选择相机的几种方式:
            
            # 方式1: 通过索引选择(默认第一个)
            # manager.open_camera(index=0)
            
            # 方式2: 通过序列号选择
            # manager.open_camera(serial="DA7598570")
            
            # 方式3: 通过IP地址选择(GigE相机)
            manager.open_camera(ip="192.168.4.22")
            
            # 方式4: 通过型号选择
            # manager.open_camera(model="MV-CS200")
            
            # 3. 采集图像
            saved_files = manager.capture_image("test")
            print(f"\n保存的文件: {saved_files}")
            
    except Exception as e:
        print(f"错误: {e}")
        import traceback
        traceback.print_exc()