c# - 为什么 C# 数组在 Android 上运行时通过 ref 从 C# 传递到 C++ 库后变为长度 1,但在 Windows 上正常工作?
问题描述
我作为 ref 从 C# 传递给 C++ 库函数的数组长度返回长度为 1,而不是在 Android 上运行时的实际长度。
该代码在为 Windows 编写时工作正常,但不适用于 Android。
仅供参考,这是一个 Unity 项目,我正在使用 OpenCV。
我在库中有以下功能。
extern "C" void ApplyCannyEffect(Color32 **rawImage, int width, int height)
{
Mat image(height, width, CV_8UC4, *rawImage);
flip(image, image, -1);
Mat edges;
Canny(image, edges, 50, 200);
dilate(edges, edges, (5, 5));
cvtColor(edges, edges, COLOR_GRAY2RGBA);
normalize(edges, edges, 0, 1, NORM_MINMAX);
multiply(image, edges, image);
flip(image, image, 0);
}
Color32 定义为
struct Color32
{
uchar red;
uchar green;
uchar blue;
uchar alpha;
};
在我的 C# 代码中,我通过以下方式声明此函数:
[DllImport("test")]
internal static extern void ApplyCannyEffect(ref Color32[] rawImage, int width, int height);
在 C# 中,我从纹理中获取图像数据。Unity 的 GetPixels32 函数为每个像素返回一个结构数组,从左下角的像素开始(Unity 的约定,而不是 OpenCV 的)。这些结构是四个字节,每个字节用于每个像素的红色、绿色、蓝色和 alpha 值。然后我检查数组的长度,它是预期的大小(以像素为单位的宽度 * 以像素为单位的高度)。我调用 C++ 函数。然后我检查数组的长度,它是 1。不用说,我没有使用大小为 1 像素的图像。
Color32[] rawImage = texture.GetPixels32();
Debug.Log(rawImage.Length.ToString()); //output is as expected on both Windows and Android
ApplyCannyEffect(ref rawImage, texture.width, texture.height);
Debug.Log(rawImage.Length.ToString()); //output is same as above on Windows but "1" on Android
它可以按预期在 Windows 上运行,但不能在 Android 上运行。我的 Windows 代码和我的 Android 代码之间的相关代码没有变化。
这是什么原因造成的?
我对 C++ 不太熟悉。但我没想到需要在 C++ 中将 Color32 参数作为指向指针的指针传递(我从教程中获得了这个示例代码)。这是因为我正在传递一个结构数组吗?通过在结构数组上使用两个引用运算符,它最终会成为指向数组第一个结构元素的第一个 uchar 的指针吗?这是函数调用后长度变为 1 的原因吗?
为了测试这一点,我尝试将 Color32 Structs 的数组转换为一个字节数组,该字节数组的长度是 C# 中 Color32 数组的 4 倍(r、g、b 和一个按顺序填充字节数组的数据)
private byte[] Color32toByteArray(Color32[] rawImage)
{
int outputIndex = 0;
byte[] output = new byte[rawImage.Length * 4];
for (int c = 0; c < rawImage.Length; c++)
{
output[outputIndex] = rawImage[c].r;
outputIndex++;
output[outputIndex] = rawImage[c].g;
outputIndex++;
output[outputIndex] = rawImage[c].b;
outputIndex++;
output[outputIndex] = rawImage[c].a;
outputIndex++;
}
Debug.Log("rawImage byte array length " + output.Length.ToString());
return output;
}
然后将输出传递给我修改为的 Windows C++ dll 函数:
extern "C" __declspec(dllexport) void ApplyCannyEffect_Byte(unsigned char* rawImage, int width, int height)
{
using namespace cv;
// create an opencv object sharing the same data space
Mat image(height, width, CV_8UC4, rawImage);
// start with flip (in both directions) if your image looks inverted
flip(image, image, -1);
Mat edges;
Canny(image, edges, 50, 200);
dilate(edges, edges, (5, 5));
cvtColor(edges, edges, COLOR_GRAY2RGBA);
normalize(edges, edges, 0, 1, NORM_MINMAX);
multiply(image, edges, image);
// flip again (just vertically) to get the right orientation
flip(image, image, 0);
}
通过以下代码
Color32[] rawImage = texture.GetPixels32();
int length = rawImage.Length;
byte[] rawByteImage = Color32toByteArray(rawImage);
ApplyCannyEffect_Byte(ref rawByteImage, texture.width, texture.height);
rawImage = BytetoColor32Array(rawByteImage, texture.width, texture.height);
texture.SetPixels32(rawImage);
texture.Apply();
Debug.Log((rawByteImage.Length / 4).ToString() + " " + rawImage.Length.ToString());
但这也(在 Windows 上)只是在 ApplyCannyEffect_Byte 调用时使程序崩溃(注释该行按预期工作,数组长度的输出是正确的)。
为什么第一组代码可以在 Windows 上运行,而不能在 Android 上运行?
解决方案
这可能是包装问题。考虑使用 Unity 的 Color32 结构,该结构完美对齐以用于本机代码。
此外,您不能将托管数组作为 ref 传递(因为 ref 还可能添加内部信息,例如实际数据之前的数组长度 ,这些信息会被 DLL 代码覆盖),对于此调用,您应该使用
Color32[] rawImage = texture.GetPixels32();
var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(rawImage, 0);
ApplyCannyEffect_Byte(ptr, texture.width, texture.height);
texture.SetPixels32(rawImage);
texture.Apply();
...
// Modified function import:
[DllImport("test")]
internal static extern void ApplyCannyEffect(IntPtr rawImage, int width, int height);
注意:没有转换,因为它实际上并不需要并且会大大降低性能,还会产生无用的 GC 分配。使用本机功能,您可以直接写入 Color32 数组。
注意 2:对于完全无 GC 的实现,请考虑使用 NativeArray Texture2D.GetPixelData() 方法,您可以从 NativeArray 获取 IntPtr,并在 SetPixels32 或预分配 Color32[] 数组后将其处理并使用 GCHandle 固定它(不是很方便的解决方案)。
无需更改 DLL 函数签名。
推荐阅读
- javascript - 正确(React/MaterialUI)在表格中将排序图标放在彼此下方的方法
- python - 在尾部python插入一个节点
- coq - Coq 前向推理:适用于多个假设
- java - 如何访问注释中的属性值?
- php - 复制Mysql表
- java - 我需要一个 TreeMap,其中每个 id 数据的 Key 将是 id 和 value(将是一个 hasMap 在该名称中将是 id & values 将是其余详细信息)?
- php - 尝试在全新安装的 Laravel 5.7 的刀片模板中使用 @foreach 时出现“未定义的变量:发布”
- c++ - C++ 正则表达式示例输出与官方不同
- c# - 在此上下文中启动了第二个操作
- php - 将 CSV 转换为 JSON 数组的正确方法