海康相機在python環境下保存圖像

def work_thread(cam=0, pData=0, nDataSize=0):
    stFrameInfo = MV_FRAME_OUT_INFO_EX()
    memset(byref(stFrameInfo), 0, sizeof(stFrameInfo))
    data_buf = (c_ubyte * nDataSize)()
    while True:
        ret = cam.MV_CC_GetOneFrameTimeout(byref(data_buf), nDataSize, stFrameInfo, 1000)

        if ret == 0:
            print("get one frame: Width[%d], Height[%d], nFrameNum[%d]" % (
            stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nFrameNum))

            nRGBSize = stFrameInfo.nWidth * stFrameInfo.nHeight * 3 + 54
            stParam = MV_SAVE_IMAGE_PARAM()
            stParam.enImageType = MV_Image_Bmp
            stParam.enPixelType = stFrameInfo.enPixelType
            stParam.nBufferSize = nRGBSize
            stParam.nWidth = stFrameInfo.nWidth
            stParam.nHeight = stFrameInfo.nHeight
            stParam.nDataLen = stFrameInfo.nFrameLen
            stParam.pData = data_buf
            stParam.pImageBuffer = (c_ubyte * nRGBSize)()

            ret = cam.MV_CC_SaveImageEx2(stParam)
            if ret != 0:
                print("convert pixel fail! ret[0x%x]" % ret)
                del data_buf
                sys.exit()

            file_path = "AfterConvert_RGB.bmp"
            file_open = open(file_path.encode('ascii'), 'wb+')
            try:
                img_buff = (c_ubyte * stParam.nImageLen)()
                cdll.msvcrt.memcpy(byref(img_buff), stParam.pImageBuffer, stParam.nImageLen)
                file_open.write(img_buff)
            except:
                raise Exception("save file executed failed:%s" % e.message)
            finally:
                file_open.close()
        else:
            print("get one frame fail, ret[0x%x]" % ret)

        if ret == 0:
            print ("get one frame: Width[%d], Height[%d], nFrameNum[%d]"  % (stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nFrameNum))
        else:
            print ("no data[0x%x]" % ret)
        if g_bExit == True:
                break

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章