我一直在做一个私人项目,我想学习如何在Windows手机上编程,在某一点上我开始摆弄插座和相机,一个好主意想到了视频输入(愚蠢的我甚至尝试)。
但现在我在这里,我有一些很好的东西,它就像一个魅力,但Lumia 800不能足够快地通过for循环。它发送一个帧,每个让我们说7-8秒,我觉得很奇怪,它应该足够强大。感觉和看起来像是在没有色情片的56k调制解调器上观看色情片。
我还意识到一帧是317000像素,每帧总共大约1MB我也发送xy坐标所以我的每帧占用2.3MB仍然以不同的方式工作以解决这个问题。所以我猜我需要做圆顶魔法才能使位置和像素值都达到可接受的大小。因为atm我会以一个可接受的速度获得它,它需要至少60MB / s来获得30fps之类的东西,但那是另一天的问题。
//How many pixels to send per burst (1000 seems to be the best)
const int PixelPerSend = 1000;
int bSize = 7 * PixelPerSend;
//Comunication thread UDP feed
private void EthernetComUDP() //Runs in own thread
{
//Connect to Server
clientUDP = new SocketClientUDP();
int[] ImageContent = new int[(int)cam.PreviewResolution.Height * (int)cam.PreviewResolution.Width];
byte[] PacketContent = new byte[bSize];
string Pixel,l;
while (SendingData)
{
cam.GetPreviewBufferArgb32(ImageContent);
int x = 1, y = 1, SenderCount = 0;
//In dire need of a speedup
for (int a = 0; a < ImageContent.Length; a++) //this loop
{
Pixel = Convert.ToString(ImageContent[a], 2).PadLeft(32, '0');
//A - removed to conserve bandwidth
//PacketContent[SenderCount] = Convert.ToByte(Pixel.Substring(0, 8), 2);//0
//R
PacketContent[SenderCount] = Convert.ToByte(Pixel.Substring(8, 8), 2);//8
//G
PacketContent[SenderCount + 1] = Convert.ToByte(Pixel.Substring(16, 8), 2);//16
//B
PacketContent[SenderCount + 2] = Convert.ToByte(Pixel.Substring(24, 8), 2);//24
//Coordinates
//X
l = Convert.ToString(x, 2).PadLeft(16, '0');
//X bit(1-8)
PacketContent[SenderCount + 3] = Convert.ToByte(l.Substring(0, 8), 2);
//X bit(9-16)
PacketContent[SenderCount + 4] = Convert.ToByte(l.Substring(8, 8), 2);
//Y
l = Convert.ToString(y, 2).PadLeft(16, '0');
//Y bit(1-8)
PacketContent[SenderCount + 5] = Convert.ToByte(l.Substring(0, 8), 2);
//Y bit(9-16)
PacketContent[SenderCount + 6] = Convert.ToByte(l.Substring(8, 8), 2);
x++;
if (x == cam.PreviewResolution.Width)
{
y++;
x = 1;
}
SenderCount += 7;
if (SenderCount == bSize)
{
clientUDP.Send(ConnectToIP, PORT + 1, PacketContent);
SenderCount = 0;
}
}
}
//Close on finish
clientUDP.Close();
}
为了简单起见,我试图使用
发送像素BitConverter.GetBytes(ImageContent[a]);
而不是我创建的字符串解析混乱(要修复只是想要一个概念证明)但是做简单的BitConverter并没有加快它的速度。
所以现在我的最后一个想法是UDP发送器套接字巫婆与msdn的库中的那个完全相同。
public string Send(string serverName, int portNumber, byte[] payload)
{
string response = "Operation Timeout";
// We are re-using the _socket object that was initialized in the Connect method
if (_socket != null)
{
// Create SocketAsyncEventArgs context object
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
// Set properties on context object
socketEventArg.RemoteEndPoint = new DnsEndPoint(serverName, portNumber);
// Inline event handler for the Completed event.
// Note: This event handler was implemented inline in order to make this method self-contained.
socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
{
response = e.SocketError.ToString();
// Unblock the UI thread
_clientDone.Set();
});
socketEventArg.SetBuffer(payload, 0, payload.Length);
// Sets the state of the event to nonsignaled, causing threads to block
_clientDone.Reset();
// Make an asynchronous Send request over the socket
_socket.SendToAsync(socketEventArg);
// Block the UI thread for a maximum of TIMEOUT_MILLISECONDS milliseconds.
// If no response comes back within this time then proceed
_clientDone.WaitOne(TIMEOUT_MILLISECONDS);
}
else
{
response = "Socket is not initialized";
}
return response;
}
总而言之,我最终得到了3个解决方案
接受失败(但不会发生,所以让我们看看2)
减少发送的数据量(破坏质量640x480我认为足够小)
找到明显的问题(谷歌和朋友用完好主意,这就是为什么我在这里)
答案 0 :(得分:2)
问题几乎可以肯定是数据的混乱。将一兆字节的二进制数据转换为几兆字节的文本,然后提取和发送单个字符将为每个字节的源数据增加大量开销。循环通过单个像素来构建发送缓冲区将采用(相对而言)地质时间尺度。
执行此操作的最快方法可能是从摄像头获取二进制数据缓冲区,并通过一次UDP写入发送。如果必须,只处理或分解手机上的数据,并小心直接访问原始二进制数据 - 不要将其全部转换为字符串并返回二进制。您添加到此过程中的每个额外方法调用都会增加开销。如果必须使用循环,请尝试在循环外尽可能多地预先计算,以最大限度地减少每次迭代所做的工作。
答案 1 :(得分:0)