gpt4 book ai didi

c# - 绑定(bind)到 Monotouch 中的第 3 方库 - 映射 uint8_t * 和 uint8_t **

转载 作者:行者123 更新时间:2023-11-30 17:08:52 26 4
gpt4 key购买 nike

我正在尝试使用 monotouch 绑定(bind)到第 3 方条形码扫描库 - 目前一切正常,但库头文件中定义的以下方法除外:

/**
* Main scan function. Invokes all activated decoders by priority.
* For successful scan, allocates pp_data buffer and pass it to user.
* User should deallocate *pp_data pointer when no more needed.
*
* @param[in] pp_image Byte array representing grayscale value of image pixels.
* Array shold be stored in row after row fashion, starting with top row.
* @param[in] lenX X axis size (width) of image.
* @param[in] lenY Y axis size (length) of image.
* @param[out] pp_data On successful decode, library allocates new byte array where it stores decoded
* string result. Pointer to string is passed here. User application is responsible
* for deallocating this buffer after use.
*
* @retval >0 Result string length for successful decode
* @retval MWB_RT_BAD_PARAM Null pointer or out of range parameters
* @retval MWB_RT_NOT_SUPPORTED Unsupported decoder found in execution list - library error
*/
extern int MWB_scanGrayscaleImage(uint8_t * pp_image, int lenX, int lenY, uint8_t **pp_data);

我已经有一段时间没有直接处理 C 数据结构了,我不清楚如何映射 uint8_t * pp_imageuint8_t **pp_data

第一个参数处理来自像素缓冲区的灰度图像。我正在从 CMSampleBuffer 获取图像缓冲区。它需要一个亮度转换的字节数组,字节数组的内存地址,还是将 pixelBuffer.GetBaseAddress(0) 作为 IntPtr 传递就足够了?

最后一个参数在一个指针中传递,该指针在 Objective-C 演示中初始化为 unsigned char *pResult=NULL;,然后在找到有效扫描时填充数据。同样,我不确定如何初始化和传入它,因为您不能在 C# 中传入未初始化/空字节数组。

目前我的绑定(bind)库代码如下(虽然我也尝试过使用IntPtr,通过ref传递,在不安全模式下传递直接地址):

[DllImport ("__Internal")]
extern static int MWB_scanGrayscaleImage (byte[] pp_image, int lenX, int lenY, out byte[] pp_data);
public static int ScanGrayscaleImage (byte[] pp_image, int lenX, int lenY, out byte[] pp_data)
{
int result = MWB_scanGrayscaleImage(pp_image, lenX, lenY, out pp_data);
return result;
}

到目前为止,对于我尝试过的所有操作,不断返回的结果值是 -1,映射到“扫描失败”。任何解决这个问题的帮助将不胜感激。

最佳答案

我建议将 uint8_t** 绑定(bind)为 IntPtr,因为该库将无法分配托管字节 []

也许是这样的:

[DllImport ("__Internal")]
extern static int MWB_scanGrayscaleImage (byte[] pp_image, int lenX, int lenY, out IntPtr pp_data);
public static int ScanGrayscaleImage (byte[] image, int lenX, int lenY, out byte[] data)
{
IntPtr pp_data;

int result = MWB_scanGrayscaleImage (image, lenX, lenY, out pp_data);
if (result > 0) {
data = new byte[result];
Marshal.Copy (pp_data, data, 0, result);
Marshal.FreeHGlobal (pp_data); // I think this is what you want...
} else {
data = null;
}

return result;
}

关于c# - 绑定(bind)到 Monotouch 中的第 3 方库 - 映射 uint8_t * 和 uint8_t **,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13431356/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com