我正在开发一个简单的程序,可以从远程 IP 摄像机抓取图像。经过几天的研究,我能够使用我获得的示例代码从 MJPEG 直播流中提取 JPEG 图像。
我使用 Windows 窗体制作了一个原型(prototype)。使用 Windows Form,我每 10 秒从 IP 摄像头接收大约 80 张图像。
现在我将代码移植到 Unity3D,每 10 秒大约获得 2 帧。
所以基本上78图像未收到。 这东西看起来像中世纪的PowerPoint幻灯片。
我正在新线程中运行该函数,就像在 Windows 窗体中一样。我首先认为 Unity 中的问题是因为我正在显示图像,但事实并非如此。
我删除了将图像显示为纹理的代码,并使用整数来计算收到的图像数量。不过,我每 10 秒 就会收到大约 2 到 4 张图像。这意味着在Windows Form应用程序中,我每10秒获得大约80到100图像。
在Unity中10秒内接收2张图像对于我正在做的事情来说是 Not Acceptable 。我编写的代码似乎不是问题,因为它在 Windows 窗体 中运行良好。
我尝试过的事情:
虽然问题出在 Unity3D 编辑器运行时,所以我调用 Windows 10 64 位并运行它,但这并没有解决问题。
将脚本后端从Mono2x更改为IL2CPP,但问题仍然存在。
将 Api 兼容性级别从 .NET 2.0 更改为 .NET 2.0 子集,但没有任何变化。
里>
下面是一个存在该问题的简单函数。即使我从另一个线程调用它,它在 Unity 上运行也慢。
bool keepRunning = true;
private void Decode_MJPEG_Images(string streamTestURL = null)
{
keepRunning = true;
streamTestURL = "http://64.122.208.241:8000/axis-cgi/mjpg/video.cgi?resolution=320x240"; //For Testing purposes only
// create HTTP request
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(streamTestURL);
// get response
WebResponse resp = req.GetResponse();
System.IO.Stream imagestream = resp.GetResponseStream();
const int BufferSize = 5000000;
byte[] imagebuffer = new byte[BufferSize];
int a = 2;
int framecounter = 0;
int startreading = 0;
byte[] start_checker = new byte[2];
byte[] end_checker = new byte[2];
while (keepRunning)
{
start_checker[1] = (byte)imagestream.ReadByte();
end_checker[1] = start_checker[1];
//This if statement searches for the JPEG header, and performs the relevant operations
if (start_checker[0] == 0xff && start_checker[1] == 0xd8)// && Reset ==0)
{
Array.Clear(imagebuffer, 0, imagebuffer.Length);
//Rebuild jpeg header into imagebuffer
imagebuffer[0] = 0xff;
imagebuffer[1] = 0xd8;
a = 2;
framecounter++;
startreading = 1;
}
//This if statement searches for the JPEG footer, and performs the relevant operations
if (end_checker[0] == 0xff && end_checker[1] == 0xd9)
{
startreading = 0;
//Write final part of JPEG header into imagebuffer
imagebuffer[a] = start_checker[1];
System.IO.MemoryStream jpegstream = new System.IO.MemoryStream(imagebuffer);
Debug.Log("Received Full Image");
Debug.Log(framecounter.ToString());
//Display Image
}
//This if statement fills the imagebuffer, if the relevant flags are set
if (startreading == 1 && a < BufferSize)
{
imagebuffer[a] = start_checker[1];
a++;
}
//Catches error condition where a = buffer size - this should not happen in normal operation
if (a == BufferSize)
{
a = 2;
startreading = 0;
}
start_checker[0] = start_checker[1];
end_checker[0] = end_checker[1];
}
resp.Close();
}
现在我将这个问题归咎于HttpWebRequest。也许它在 Unity 中实现得不好。不确定....
发生什么事了?为什么会发生这种情况?我该如何修复它?
最佳答案
是否可能需要使用Read[a lot]
而不是Read
??
阅读[很多]
:
https://msdn.microsoft.com/en-us/library/system.io.stream.read(v=vs.110).aspx
Return Value Type: System.Int32 The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available
可以想象,ReadAsync
可以提供帮助,manual ,尽管它会产生截然不同的代码。
关于c# - Windows Form 与 Unity3D HttpWebRequest 性能对比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35829285/