3

我正在尝试使用 monotouch 绑定到第 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# 中传递未初始化/空字节数组。

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

[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,映射为“扫描失败”。任何解决此问题的帮助将不胜感激。

4

2 回答 2

1

第一个参数是保存亮度值的字节数组的内存地址,但是将数组作为参数传递应该可以做到这一点。

最后一个参数必须引用一个(有效的)内存位置,其中将存储对输出字符串的引用。

您是否尝试过类似以下的方法?

[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;

    fixed (byte** pp_data_ptr = &pp_data) {
        result = MWB_scanGrayscaleImage(pp_image, lenX, lenY, pp_data_ptr);
    }

    return result;
}
于 2012-11-17T16:15:47.753 回答
1

我建议将 uint8_t** 绑定为 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;
}
于 2012-11-20T21:33:52.760 回答