海康工業相機: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
發佈了70 篇原創文章 · 獲贊 84 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章