admin管理员组

文章数量:1123793

I need to collect video streams from a microscope and used SDL's library to obtain the data frameData. However, I didn't know how to parse the video stream data in frameData to create videos that Unity could use。

public class CameraStreamManager : MonoBehaviour
{


    private Thread cameraThread;
    private bool isRunning = false; 
    private SLcamNet.NetCamera _camera; 
    private Texture2D videoTexture;

    public RawImage videoImage; 

    private List<NetInfo> _infos;
    private CameraInfo _cameraInfo;
    private SDLPanel sdlPanel; 
    private Task _task; 

    private int width = 1920;  
    private int height = 1080;
    private bool frameReady = false;
    public void InitializeCamera()
    {
        SLcamNet.NetCamera.InitNet();
        _infos = SLcamNet.NetCamera.SearchAvailableCameras();
        if (_infos.Count < 1)
        {
            Thread.Sleep(100);
            _infos = SLcamNet.NetCamera.SearchAvailableCameras();
        }

    }

    public void OpenCamera()
    {
        sdlPanel?.Dispose();
        sdlPanel = new SDLPanel
        {
            Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right
        };

        Debug.LogError("_infos>>>>  " + _infos.Count);
        if (_infos.Count < 1)
        {
            Thread.Sleep(100);
            _infos = SLcamNet.NetCamera.SearchAvailableCameras();
            return;
        }

        CameraSettings settings = new CameraSettings
        {
            netInfo = _infos[0],
            resolution = new CameraResolution(1920, 1080),
            streamFormat = StreamFormat.kMJPEG,
            pixFormat = PixFormat.kRGBA8888,
            rcMode = "cbr",
            quality = "high",
            frameRate = 25,
            bitrate = 86000,
            keyFrameInterval = 90
        };
        settingsInfo.Username = "admin";
        settingsInfo.Password = "123456";

        _camera = SLcamNet.NetCamera.Create(settingsInfo.Model);
        _camera.SetCameraSettings(settings);
        _camera.Login();
        _cameraInfo = _camera.GetNetCameraAllInfo();
        _camera.Open();

        isRunning = true;
        _task = Task.Run(() => StartCameraCapture());
    }

    private void StartCameraCapture()
    {
        Task.Run(() =>
        {
            Thread.Sleep(500); 
            IntPtr frameDataPtr = IntPtr.Zero;

            while (isRunning)
            {
                try
                {
                    if (!_camera.GetVideoFrameData(ref frameDataPtr))
                        continue;

                    SLcamNet.l_md_data_t frameData = SLcamNet.l_md_data_t.FromIntPtr(frameDataPtr);
                    Debug.LogError("frameData>>>>>" + frameData.p_rgb);



                }
                catch (Exception ex)
                {
                    Debug.LogError($"Error in StartCameraCapture: {ex.Message}");
                }
            }
        });
    }

    private void OnApplicationQuit()
    {
        isRunning = false;
        _task = null;

        if (sdlPanel != null)
        {
            sdlPanel.Dispose(true);
            sdlPanel = null;
        }

        _camera?.Close();
    }
}

本文标签: cUnity obtains sdl and collects RGB raw dataStack Overflow