[英]Binding to 3rd-party library in Monotouch - mapping uint8_t * and uint8_t **
我試圖使用monotouch綁定到第三方條形碼掃描庫 - 除了以下方法之外,一切都運行良好,如庫頭文件中所定義:
/**
* 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_image
和uint8_t **pp_data
。
第一個參數處理來自像素緩沖區的灰度圖像。 我從CMSampleBuffer
獲取圖像緩沖區。 是期望亮度轉換的字節數組,字節數組的內存地址,還是將pixelBuffer.GetBaseAddress(0)
作為IntPtr
傳遞就足夠了?
最后一個參數在一個指針中傳遞,該指針被初始化為unsigned char *pResult=NULL;
在objective-C演示中,然后在找到有效掃描時填充數據。 同樣,我不知道如何初始化並傳遞此信息,因為您無法在C#中傳入未初始化的/ null字節數組。
我的綁定庫代碼目前如下(雖然我也嘗試使用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,映射到“掃描失敗”。 任何幫助搞清楚這一點將不勝感激。
第一個參數是保存亮度值的字節數組的內存地址,但是將數組作為參數傳遞應該這樣做。
最后一個參數必須引用一個(有效)內存位置,其中將存儲對輸出字符串的引用。
你嘗試過類似下面的東西嗎?
[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;
}
我建議將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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.