@ Mike,@ Dat - I believe the camera is capable of taking 20fps on smaller resolutions and is also capable of taking 2 or more frames per second on the max resolution of 320240. The camera module is this one([url]https://www.ghielectronics.com/catalog/product/283[/url]). I think the bottle neck is the web server which responds with the image byte array. I have been sending one picture as 3202403 byte array. On average it takes 4-5 secs when I send two images((320240*3)*2) at once. The code below is what i have been using on the device.I have also posted my client code which makes a request to server every 5 seconds. Any suggestions on how to make it faster is appreciated.
using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Net.NetworkInformation;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using System.Threading;
namespace Test123
{
public partial class Program
{
private string webServerIpAddress = string.Empty;
byte[] buff;
byte[] buff1;
byte[] buff2;
int count = 0;
// This method is run when the mainboard is powered up or reset.
void ProgramStarted ( )
{
NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged; // event listener when wifi network is available
NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged; // event listener when device gets ip address
camera.CameraConnected += this.camera_CameraConnected;
camera.BitmapStreamed += this.camera_BitmapStreamed; // event listener every time a picture is taken
if (!wifiRS21.NetworkInterface.Opened)
{
Debug.Print("open");
wifiRS21.NetworkInterface.Open();
}
if (wifiRS21.NetworkInterface.Opened)
{
if (!wifiRS21.NetworkInterface.IsDhcpEnabled)
{
Debug.Print("dhcp enabled");
wifiRS21.NetworkInterface.EnableDhcp();
wifiRS21.NetworkInterface.EnableDynamicDns();
}
}
// scan network for given ssid and join
GHI.Networking.WiFiRS9110.NetworkParameters[] WifiScanResult = wifiRS21.NetworkInterface.Scan();
for (int i = 0; i < WifiScanResult.Length; i++)
{
Debug.Print(WifiScanResult[i].Ssid + " ssid");
if (WifiScanResult[i].Ssid == "Connectify-test")
{
Debug.Print("trying to join");
wifiRS21.NetworkInterface.Join(WifiScanResult[i].Ssid, "abdi1990");
break;
}
}
// wait till the device gets proper ip address
while (wifiRS21.NetworkInterface.IPAddress == "0.0.0.0")
{
Debug.Print("Waiting for DHCP");
Thread.Sleep(250);
}
Debug.Print("Program Started");
}
private void camera_BitmapStreamed (Camera sender, Bitmap e)
{
// take two pictures and store in byte array buff1 and buff2
if (count == 0)
{
Debug.Print("first taken");
buff1 = new byte[320 * 240 * 3 + 54];
GHI.Utilities.Bitmaps.ConvertToFile(e, buff1);
count++;
}
else if (count == 1)
{
// after camera streams second picture stop streaming and combine byte arrays buff1 & buff2
camera.StopStreaming();
buff2 = new byte[320 * 240 * 3 + 54];
buff = new byte[buff1.Length + buff2.Length];
GHI.Utilities.Bitmaps.ConvertToFile(e, buff2);
Array.Copy(buff1, 0, buff, 0, buff1.Length);
Array.Copy(buff2, 0, buff, buff1.Length, buff2.Length);
Debug.Print("second taken");
count = 0;
}
}
private void camera_CameraConnected (Camera sender, EventArgs e)
{
camera.CurrentPictureResolution = Camera.PictureResolution.Resolution320x240;
}
private void NetworkChange_NetworkAddressChanged (object sender, EventArgs e)
{
Debug.Print(wifiRS21.NetworkInterface.IPAddress);
Debug.Print("Network address changed");
if (wifiRS21.NetworkInterface.IPAddress != "0.0.0.0")
{
WebServer.StartLocalServer(wifiRS21.NetworkInterface.IPAddress, 80);
webServerIpAddress = wifiRS21.NetworkInterface.IPAddress;
// wifi_RS21.Interface.Disconnect();
WebServer.DefaultEvent.WebEventReceived += DefaultEvent_WebEventReceived;
camera.StartStreaming(); // take 2 pictures as soon as webserver is live
}
}
private void NetworkChange_NetworkAvailabilityChanged (object sender, NetworkAvailabilityEventArgs e)
{
Debug.Print("Network availability: " + e.IsAvailable.ToString());
}
private void DefaultEvent_WebEventReceived (string path, WebServer.HttpMethod method, Responder responder)
{
string actualPath = "/";
if (!StringHelper.IsNullOrWhiteSpace(path))
{
actualPath += path;
}
string methodName = string.Empty;
switch (method)
{
case WebServer.HttpMethod.GET:
methodName = "GET";
break;
case WebServer.HttpMethod.POST:
methodName = "POST";
break;
case WebServer.HttpMethod.PUT:
methodName = "PUT";
break;
case WebServer.HttpMethod.DELETE:
methodName = "DELETE";
break;
}
Debug.Print("HTTP " + methodName + " " + actualPath + " from " + responder.ClientEndpoint);
if (method == WebServer.HttpMethod.GET && actualPath == "/")
{
Debug.Print("picture requested");
string html = "<html><body><h1>Taken!!</h1></body></html>";
byte[] bytes = System.Text.UTF8Encoding.UTF8.GetBytes(html);
if (buff != null)
{
responder.Respond(buff, "image/bmp"); // respond with combined byte array of the two images
}
else
responder.Respond(bytes, "text/html");
camera.StartStreaming(); // after responding start streaming again
}
}
}
}
Client code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private Timer timer1;
int counter = 1;
public Form1()
{
InitializeComponent();
}
private void clicked(object sender, EventArgs e)
{
// MessageBox.Show("done");
timer1 = new Timer();
timer1.Tick += new EventHandler(timer_Tick);
timer1.Interval = 5000;
timer1.Start();
}
private void timer_Tick(object sender, EventArgs e)
{
HttpWebRequest lxRequest = (HttpWebRequest)WebRequest.Create("http://192.168.137.102");
// returned values are returned as a stream, then read into a string
String lsResponse = string.Empty;
using (HttpWebResponse lxResponse = (HttpWebResponse)lxRequest.GetResponse())
{
using (BinaryReader reader = new BinaryReader(lxResponse.GetResponseStream()))
{
Byte[] lnByte = reader.ReadBytes((320 * 240 * 3 + 54) * 2);
Byte[] first = new Byte[320 * 240 * 3 + 54];
Byte[] second = new Byte[320 * 240 * 3 + 54];
Array.Copy(lnByte, 0, first, 0, first.Length);
Array.Copy(lnByte, first.Length, second, 0, second.Length);
using (FileStream lxFS = new FileStream(counter + ".bmp", FileMode.Create))
{
lxFS.Write(first, 0, first.Length);
counter++;
}
using (FileStream lxFSs = new FileStream(counter + ".bmp", FileMode.Create))
{
lxFSs.Write(second, 0, second.Length);
counter++;
}
}
}
}
}
}
Thanks in advance