You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中渲染ESP32摄像头MJPEG流无显示问题求助

在Unity中渲染ESP32摄像头MJPEG流视频无显示问题求助

我是StackOverflow新手,很高兴认识大家。我希望在Unity中实时渲染ESP32摄像头捕获的MJPEG流视频,已知摄像头的流地址(例如http://192.1.1.1:81/stream),想要将视频显示在Canvas的RawImage对象上。

但运行项目时,游戏场景仅显示Unity默认背景,控制台没有任何错误。我已经将编写的脚本添加到RawImage对象上,RawImage对象的检查器设置已配置完成。恳请大家帮忙解决这个问题!

以下是我使用的脚本:

using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Net;
using System.Collections;
using System.Linq;  

public class MJPEGStream : MonoBehaviour
{
    public string url = "http://<ESP32_IP>:81/stream"; // MJPEG流URL
    public RawImage rawImage; // RawImage组件

    private Texture2D texture;

    void Start()
    {
        if (rawImage == null)
        {
            Debug.LogError("未分配RawImage组件!");
            return; // 如果rawImage为空则终止函数
        }

        texture = new Texture2D(0, 0);
        rawImage.texture = texture; // 为RawImage分配纹理

        StartCoroutine(StartStream());
    }

    IEnumerator StartStream()
    {
        if (string.IsNullOrEmpty(url))
        {
            Debug.LogError("流URL为空!");
            yield break;
        }

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        WebResponse response = request.GetResponse();
        Stream stream = response.GetResponseStream();

        byte[] boundary = System.Text.Encoding.ASCII.GetBytes("--123456789000000000000987654321"); // 设置分隔符

        while (true)
        {
            byte[] jpegData = ReadNextJPEGFrame(stream, boundary);
            if (jpegData != null)
            {
                texture.LoadImage(jpegData);
                Debug.Log("Texture width: " + texture.width + ", height: " + texture.height);
                rawImage.texture = texture;
                Debug.Log("Texture format: " + texture.format);
            }
            yield return null;
        }
    }

    // 以分隔符为基准读取下一帧JPEG数据
    byte[] ReadNextJPEGFrame(Stream stream, byte[] boundary)
    {
        MemoryStream jpegStream = new MemoryStream();

        while (true)
        {
            int byteRead = stream.ReadByte();
            if (byteRead == -1)
                return null; // 到达流末尾则返回空

            jpegStream.WriteByte((byte)byteRead);

            // 查找分隔符并提取JPEG数据
            if (jpegStream.Length >= boundary.Length)
            {
                byte[] lastBytes = new byte[boundary.Length];
                jpegStream.Position = jpegStream.Length - boundary.Length;
                jpegStream.Read(lastBytes, 0, boundary.Length);

                if (lastBytes.SequenceEqual(boundary))  
                {
                    // 找到分隔符
                    byte[] jpegData = new byte[jpegStream.Length - boundary.Length];
                    jpegStream.Position = 0;
                    jpegStream.Read(jpegData, 0, jpegData.Length);
                    return jpegData; // 返回JPEG数据
                }
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者YOUNGEUN SONG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 13:24:52