海康工业相机: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

你可能感兴趣的:(TX2)