简体   繁体   中英

how to convert MediaFrameReader frame from USB camera to a C# byte[] array to send to a remote socket

Can't figure out how to cast the 1.2MB frame into a byte[] to send a camera frame from my Windows 10 UWP C# app to socket at Xamarin iPad app.

EVENT FOR ARRIVAL OF FRAME FROM CAMERA...

private void Reader_FrameArrived( MediaFrameReader sender, MediaFrameArrivedEventArgs args )
{
    using( MediaFrameReference frame = sender.TryAcquireLatestFrame() )
    {
        if( frame != null )
        {
            // frame = latest frame or null if no frame
            var frame_size_bytes = frame.Buffer​Media​Frame.Buffer.Length;

            if( SDKTemplate.SOURCE.Camera_Service_Socket.connection_established && frame != null )
            {

                ////////////////////////////////    S E N D   F R A M E   T O   I P A D      /////////////////////////////


                // Init packet header:
                    this.packet_header.CFV = 1;     
                    var header_size_bytes = System.Runtime.InteropServices.Marshal.SizeOf(typeof( Packet_Header_struct));
                    this.packet_header.frame_size_bytes = Convert.ToInt32( frame_size_bytes );
                    this.packet_header.packet_sequence_index = packet_sequence_index++;
                    this.packet_header.ORC = 0;     // TBD
                    this.packet_header.recognition_probability_factor = 0;  // tbd
                    this.packet_header.logical_CRC = 0;     // TBD

                // Send packet header to iPad:
                    // Copy structure to a Byte array:
                    Byte[] header_buffer = getBytes( this.packet_header );

                    SDKTemplate.SOURCE.Camera_Service_Socket.send_data( header_buffer );     /////////  SEND 32 BYTE HEADER

                // Send packet payload (ie. frame) to iPad:
                // Copy 



                SDKTemplate.SOURCE.Camera_Service_Socket.send_data( frame.???????????????????? );    ////////////  SEND 1.2MByte frame
            }

            // Display locally:
            _frameRenderer.ProcessFrame( frame );
        }
    }
}

how to cast the 1.2MB frame into a byte[]

You could get the Buffer from the BufferMediaFrame of frame. Then you could call the .ToArray() method to get the byte array.

using (var frame = sender.TryAcquireLatestFrame())
{
    if (frame != null)
    {
        IBuffer buffrer = frame.BufferMediaFrame.Buffer;
        byte[] bytes = buffrer.ToArray();
    }
    _frameRenderer.ProcessFrame(frame);
}

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