海康工业相机:MV-CE100-30GC摄像头的调用及图片保存 - python实现

一、官方接口中找到Samples文件夹中的Python目录

1. 找到GrabImage文件夹下的GrabImage.py
2.修改work_thread()函数:

Linux下:

def work_thread(cam=0, data_buf=0, nDataSize=0):
    stFrameInfo = MV_FRAME_OUT_INFO_EX()
    memset(byref(stFrameInfo), 0, sizeof(stFrameInfo))
    while True:
        ret = cam.MV_CC_GetOneFrameTimeout(byref(data_buf), nDataSize, stFrameInfo, 1000)
        if ret == 0:
            print("get one frame: Width[%d], Height[%d], PixelType[0x%x], nFrameNum[%d]" % (
            stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.enPixelType, stFrameInfo.nFrameNum))
            stConvertParam = MV_SAVE_IMAGE_PARAM_EX()
            stConvertParam.nWidth = stFrameInfo.nWidth
            stConvertParam.nHeight = stFrameInfo.nHeight
            stConvertParam.pData = data_buf
            stConvertParam.nDataLen = stFrameInfo.nFrameLen
            stConvertParam.enPixelType = stFrameInfo.enPixelType

            file_path = "save.bmp"
            stConvertParam.enImageType = MV_Image_Bmp

            bmpsize = stFrameInfo.nWidth * stFrameInfo.nHeight * 3 + 54

            stConvertParam.nBufferSize = bmpsize
            bmp_buf = (c_ubyte * bmpsize)()
            stConvertParam.pImageBuffer = bmp_buf

            ret = cam.MV_CC_SaveImageEx2(stConvertParam)
            if ret != 0:
                print("save file executed failed0:! ret[0x%x]" % ret)
                del data_buf
                sys.exit()
            # print(stop - start)
            file_open = open(file_path.encode('ascii'), 'wb+')
            try:
                img_buff = (c_ubyte * stConvertParam.nDataLen)()
                memmove(byref(img_buff), stConvertParam.pImageBuffer, stConvertParam.nDataLen)
                file_open.write(img_buff)
            except Exception as e:
                raise Exception("save file executed failed1::%s" % e)
            finally:
                file_open.close()
        else:
            print("no data[0x%x]" % ret)
        if g_bExit == True:
            break

windows下:

def work_thread(cam=0, data_buf=0, nDataSize=0):
    stFrameInfo = MV_FRAME_OUT_INFO_EX()
    memset(byref(stFrameInfo), 0, sizeof(stFrameInfo))
    while True:
        ret = cam.MV_CC_GetOneFrameTimeout(byref(data_buf), nDataSize, stFrameInfo, 1000)
        if ret == 0:
            print("get one frame: Width[%d], Height[%d], PixelType[0x%x], nFrameNum[%d]" % (
            stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.enPixelType, stFrameInfo.nFrameNum))
            stConvertParam = MV_SAVE_IMAGE_PARAM_EX()
            stConvertParam.nWidth = stFrameInfo.nWidth
            stConvertParam.nHeight = stFrameInfo.nHeight
            stConvertParam.pData = data_buf
            stConvertParam.nDataLen = stFrameInfo.nFrameLen
            stConvertParam.enPixelType = stFrameInfo.enPixelType

            file_path = "save.bmp"
            stConvertParam.enImageType = MV_Image_Bmp

            bmpsize = stFrameInfo.nWidth * stFrameInfo.nHeight * 3 + 54

            stConvertParam.nBufferSize = bmpsize
            bmp_buf = (c_ubyte * bmpsize)()
            stConvertParam.pImageBuffer = bmp_buf

            ret = cam.MV_CC_SaveImageEx2(stConvertParam)
            if ret != 0:
                print("save file executed failed0:! ret[0x%x]" % ret)
                del data_buf
                sys.exit()
            # print(stop - start)
            file_open = open(file_path.encode('ascii'), 'wb+')
            try:
                img_buff = (c_ubyte * stConvertParam.nDataLen)()
                cdll.msvcrt.memcpy(byref(img_buff), stConvertParam.pImageBuffer, stConvertParam.nDataLen)
                file_open.write(img_buff, )
            except Exception as e:
                raise Exception("save file executed failed1::%s" % e)
            finally:
                file_open.close()
        else:
            print("no data[0x%x]" % ret)
        if g_bExit == True:
            break
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值