C# vuforia.image.pixels cover to iOS CIImage or CGImage


Keywords:c# 


Question: 

C#

private Vuforia.Image QCARoutput;
QCARoutput = CameraDevice.Instance.GetCameraImage(Vuforia.Image.PIXEL_FORMAT.RGB888);

_iOS_sendData(QCARoutput.Pixels, QCARoutput.Pixels.length);

iOS

void _iOS_sendData(const char** ptrSrc, const int srcLength) {

        NSData *imageData = [NSData dataWithBytes:(const void *)ptrSrc length:(sizeof(unsigned char) * srcLength)];
        CIImage *ciimg = [CIImage imageWithData:imageData];
        [delegateObject decodeWithQRcode:ciimg complete:^(NSString *result) {
            NSLog(@"%@",result);
        }];
}

So I wannted to get Vuforia.image.pixels which is byte[]. Then send to iOS cover to CIImage, but ciimage is null. How can I cover this byte[] to CIImage object ?

====== Updata C# ======

[DllImport("__Internal")]
private static extern void _iOS_sendData_ResultBlock(System.IntPtr ptrSrc, int srcLength); 

//=================================================
QCARoutput = CameraDevice.Instance.GetCameraImage(Vuforia.Image.PIXEL_FORMAT.RGB888);
IntPtr unmanagedPointer = Marshal.AllocHGlobal(QCARoutput.Pixels.Length);
Marshal.Copy(QCARoutput.Pixels, 0, unmanagedPointer, QCARoutput.Pixels.Length);
// Call unmanaged code
Marshal.FreeHGlobal(unmanagedPointer);
_iOS_sendData_ResultBlock(unmanagedPointer, QCARoutput.Pixels.Length);

====== Updata Objective-C ======

void _iOS_sendData_ResultBlock(Byte *ptrSrc, const int srcLength, iOSUnityCallBack callback) {

        NSData *imageData = [NSData dataWithBytes:ptrSrc length:(sizeof(unsigned char) * srcLength)];
        CIImage *ciimg = [CIImage imageWithData:imageData];
        if (ciimg == nil) {
            NSLog(@"the ciimage is nil ,stop");
            return;
        }
}

I make some change. It does not work.


1 Answer: 

I think you need to change the _iOS_sendData's declaration on both sides, C# and obj-c. I'm guessing you have something like this:

//c# side
[DllImport ("__Internal")]
private static extern void _iOS_sendData(byte[] bytes, int length);

not sure why, but bytes argument has to be an array pointer, instead of an actual array. I think it's because of the way Objective-C is handling things.

Try the following:

//c# side
[DllImport ("__Internal")]
private static extern void _iOS_sendData(System.IntPtr bytes, int length);

//obj-c side
void _iOS_sendData(Byte * bytes, int length)

source