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'

這個方法試了三小時才試出來。

求大神指導更好的方法!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM