简体   繁体   中英

How to efficiently convert IList<byte> to byte[]?

I have problem with efficiently converting an IList<byte> to byte[] . I'm working with Xamarin Studio with OpenCV to process images and Fotoapparat to take pictures. I want to convert the list to an array because OpenCV expects an array not a list. I know the 'easy' way to convert with ToArray() , but it takes too much time (more than 8-9 sec to convert). For reference, my IList<byte> has a length of 5564448.

Does anybody know how to efficiently convert IList<byte> to byte[] ?

I found solution to my own problem. Maybe someone will have the same problem in future. Below solution:

internal static IntPtr javaClassHandle;
internal static IntPtr classRef
{
    get
    {
        return JNIEnv.FindClass("io/fotoapparat/preview/Frame", ref javaClassHandle);
    }
}
static IntPtr imageJFieldId;

public void ProcessFrame(Frame frame)
{            
    int bufferSize = frame.Image.Count;
    var buffer = new byte[bufferSize];
    if (imageJFieldId == IntPtr.Zero)
        imageJFieldId = JNIEnv.GetFieldID(classRef, "image", "[B");
    var fieldObject = JNIEnv.GetObjectField(frame.Handle, imageJFieldId);
    var fastByteArray = new FastJavaByteArray(fieldObject);
    fastByteArray.CopyTo(buffer, 0);
}

Link to FastJavaByteArray

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM