海康相机在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

 

发布了169 篇原创文章 · 获赞 204 · 访问量 57万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览