I have a shell working class for Dallmeier camera devices. It contains a callback method to retrieve the current YUV image.
See Details C # Wrap for an Array of Three Pointers .
I have a button in my form that receives a YUV image. The callback returns "yuvData", which is an array of three pointers to the Y, U, and V parts of the image. Then I copy the three pointers to my own pointer, and then copy them to an array of bytes. YuvCallback continues to work until I turn off the camera.
Am I using Marshal.Copy correctly?
public class DLMSDK
{
public delegate int YUVDataCallback(dlm_yuvdataParametersStructure pParameters);
DllImport(@"DallmeiersDLL\davidapileolive.dll")]
public extern static int dlm_setYUVDataCallback(int SessionHandle, YUVDataCallback dataCallback);
[StructLayout(LayoutKind.Explicit, Size = 32)]
public struct dlm_yuvdataParametersStructure
{
[FieldOffset(0)]
public int IPlayerID;
[FieldOffset(4), MarshalAs(UnmanagedType.ByValArray, SizeConst=3)]
public IntPtr[] yuvData;
[FieldOffset(8), MarshalAs(UnmanagedType.ByValArray, SizeConst=1)]
public IntPtr[] pitch;
[FieldOffset(12)]
public int width;
[FieldOffset(16)]
public int height;
[FieldOffset(18)]
public long ts;
[FieldOffset(28), MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public IntPtr[] extData;
}
}
public partical class Form1 : Form
{
int width; int height;
int yArraySize; int uvArraySize;
byte[] yBytes; byte[] uBytes; byte[] vBytes;
int sizeY; int sizeU; int sizeV;
IntPtr ptrY; IntPtr ptrU; IntPtr ptrV;
DLMSDK.YuvDataCallback yuvdataCallback;
private void Form1_Load(object send, EventArgs e)
{
error = DLMSDK.dlm_initSDK();
if (error == 0)
registerEvents();
}
private void registerEVents()
{
yuvdataCallback = yuvdataHandler;
}
private void btnGetYUV_Click(object sender, EventArgs e)
{
try
{
error = DLMSDK.dlm_setYUVDataCallback(SessionHandle, yuvdataCallback);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public int yuvdataHandler(DLMSDK.dlm_yuvdataParametersStructure pParameters)
{
width = pParameters.width;
height = pParameters.height;
yArraySize = width * height;
uvArraySize = yArraySize/4;
yBytes = new byte[yArraySize];
uBytes = new byte[uvArraySize];
vBytes = new byte[uvArraySize];
sizeY = Marshal.SizeOf(yBytes[0]) * yBytes.Length;
ptrY = Marshal.AllocHGlobal(sizeY);
sizeU = Marshal.SizeOf(uBytes[0]) * uBytes.Length;
ptrU = Marshal.AllocHGlobal(sizeU);
sizeV = Marshal.SizeOf(vBytes[0]) * vBytes.Length;
ptrV = Marshal.AllocHGlobal(sizeV);
try
{
Marshal.Copy(pParameters.yuvData, 0, ptrY, 1);
Marshal.Copy(pParameters.yuvData, 1, ptrU, 1);
Marshal.Copy(pParameters.yuvData, 2, ptrV, 1);
Marshal.Copy(ptrY, yBytes, 0, sizeY);
Marshal.Copy(ptrU, uBytes, 0, sizeU);
Marshal.Copy(ptrV, vBytes, 0, sizeV);
Bitmap bmp = ImgConvert.ToGreyscale(yBytes, width, height);
DisplayImage(bmp);
}
finally
{
if (ptrY != IntPtr.Zero)
{
Marshal.FreeHGlobal(ptrY);
ptrY = IntPtr.Zero;
}
if (ptrU != IntPtr.Zero)
{
Marshal.FreeHGlobal(ptrU);
ptrU = IntPtr.Zero;
}
if (ptrV != IntPtr.Zero)
{
Marshal.FreeHGlobal(ptrV);
ptrV = IntPtr.Zero;
}
}
return 0;
}
}