python: c_char_p指向的bitmap图像数据,通过c_char_Array最终赋值给PIL的Image对象

    def GetCurrentImage(self):
        ok, bitmap, buff_len = self.GetCurrentFrameBitmap()  #调用C函数,返回位图数据的指针. bitmap是c_char_p类型
        if not ok:
            return False,None,'GetCurrentFrameBitmap fail:code=%d, msg=%s'% 
                (reader.LastErrorCode(), reader.LastErrorMessage())
        ret,width,height = self.GetVideoRect()
        if not ret:
            return False,None,'GetVideoRect fail:code=%d, msg=%s'%(reader.LastErrorCode(), reader.LastErrorMessage())
        BitmapType = c_char*buff_len  #创建一个数组对象
        arr = BitmapType()   #数组的实例
        memmove(arr, bitmap, buff_len)  #拷贝内容
        #img = Image.frombytes('RGB', (width, height), arr)
        img = Image.frombuffer('RGB', (width, height), arr, 'raw', 'RGB', 0, 1)   #数组直接映射为图片对象
        return True,img,'success'

这个方法试了三小时才试出来。

求大神指导更好的方法!

原文地址:https://www.cnblogs.com/ahfuzhang/p/9975168.html