将指针传递给指向缓冲区的ctypes指针

时间:2014-06-10 09:55:27

标签: python c ctypes

我试图使用ctypes和python从相机中获取图像数据。

非常基本上,文档说我应该连接到设备,设置分辨率,输出窗口,帧速率和触发模式(所有,显然是成功的)。然后,我应该打开一个图像序列,然后开始抓取帧,或者在我想要获得帧的时刻触发相机。这一切都相当清楚。事情开始出错的地方在于访问帧数据。在C中,代码看起来应该是这样的:

// Snap two images into buffers
// Buffer size is decided by image resolution
// Here assuming a image is 800*600
BeginHVDevice(1,&hDevice1); //Initialize camera 1
HVSetOutputWindow(hDevice1, 0, 0, 800, 600);
//bunch of other settings as well


BYTE Buffer1[800 * 600]; // Buffer for one image
BYTE Buffer2[800 * 600]; // Buffer for a second image 
BYTE *ppBuffer[2]; //This array keeps the pointer to each buffer 
ppBuffer[0] = Buffer1;
ppBuffer[1] = Buffer2;
void *nParam
long *callBack

HVOpenSnap(hDevice1, callBack, nParam); //note that I don't use call backs so the last two params are not used 
HVSnapShot(hhv, ppBuffer, 2); //last argument is just the number of frames to collect
HVCloseSnap(hhv);

问题是用于存储数据的缓冲区。我尝试创建一个正确大小的字符串缓冲区并传递该参数。我尝试过创建整数缓冲区,长缓冲区。我得到三个结果:缓冲区永远不会被触及(所有空值),程序硬崩溃并且必须重新启动(我猜想python会在此时尝试写入无效地址),或者存在类型错误。 / p>

当前的python代码如下所示

ailt_lib = WinDLL("HVDAILT")
util_lib = WinDLL("HVUtil")
raw_2_rgb_lib = WinDLL("Raw2Rgb")


open_snap = ailt_lib.HVOpenSnap
open_snap.restype = c_int
open_snap.argtypes = [HANDLE, POINTER(c_long), c_void_p]

close_snap = ailt_lib.HVCloseSnap
close_snap.restype = c_int
close_snap.argtypes = [HANDLE]


stop_snap = ailt_lib.HVStopSnap
stop_snap.restype = c_int
stop_snap.argtypes = [HANDLE,]

start_snap = ailt_lib.HVStartSnap
start_snap.restype = c_int
start_snap.argtypes = [HANDLE, POINTER(c_char_p*buf_size), c_int]

click = ailt_lib.HVTriggerShot
click.restype = c_int
click.argtype = [POINTER(HANDLE),]

#Bunch of code to initialise the camera. All apparently succeeds

print open_snap(cam_handle, byref(c_long(0)), c_void_p(None))
storage = c_char_p*buf_size
buf = storage()
storage_ptr = pointer(buf)
print start_snap(cam_handle, storage_ptr, 1)
print click(cam_handle)

我认为,在这种情况下,问题是我根本不知道如何生成应该传递给start_snap的正确形式的缓冲区。

0 个答案:

没有答案