我一直在从事一个私人项目,我想学习如何在 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 我认为足够小)
找到明显的问题(谷歌和朋友的好主意用完了,这就是我在这里的原因)