In a previous example we shown you how you can use B4J to create a desktop application that connects to a number of devices and shows the camera frames: B4J CCTV example
In this example we implement a similar solution, however using the server framework the network related code is much simpler and as this is a server solution you can connect to the server from anywhere you like and see the camera frames inside the browser:
The device code is very similar to the code used in the other example. The only difference is that HttpUtils2 is used to send the images (instead of a Socket + AsyncStreams).
The server project is made of three handlers:
SendImage - The devices call this handler with the cameras frames. This handler then stores the image data in a process global Map named images:
The device IP address is used as the key.
Main.images is a special Map. It is created in the Main module code:
Server.CreateThreadSafeMap returns a special Map that can be safely accessed by multiple threads. This is very useful for all kinds of cache implementations.
GetImages - This handler creates a simple html that includes the images "urls":
As you can see in the img src value the images are actually handled by another handler named GetImage.
Note that a time based parameter is added. The parameter value will be different on each request. This way we force the browser to redownload the images.
GetImage - Returns a stored image based on the IP parameter:
How it all connects
The main page (index.html file) uses ajax requests to refresh one of the "divs" every 500ms. This is done with the GetImages handler. Later as a result of the img src values the GetImage handler is called for each image url and returns the most recent image based on the specified IP address.
Note that the server should be able to handle a large number of connected devices. Probably more than 50 or 100.
In this example we implement a similar solution, however using the server framework the network related code is much simpler and as this is a server solution you can connect to the server from anywhere you like and see the camera frames inside the browser:
The device code is very similar to the code used in the other example. The only difference is that HttpUtils2 is used to send the images (instead of a Socket + AsyncStreams).
The server project is made of three handlers:
SendImage - The devices call this handler with the cameras frames. This handler then stores the image data in a process global Map named images:
B4X:
Public Sub Handle(req As ServletRequest, resp As ServletResponse)
Dim out As OutputStream
out.InitializeToBytesArray(0)
File.Copy2(req.InputStream, out) 'read the image from the input stream and write it into a memory stream
Main.images.Put(req.RemoteAddress, out.ToBytesArray)
resp.Write("Ok")
End Sub
Main.images is a special Map. It is created in the Main module code:
B4X:
Sub Process_Globals
Private srvr As Server
Public images As Map
End Sub
Sub AppStart (Args() As String)
srvr.Initialize("")
srvr.Port = 51042
srvr.AddHandler("/SendImage", "SendImage", False)
srvr.AddHandler("/GetImages", "GetImages", False)
srvr.AddHandler("/GetImage", "GetImage", False)
srvr.Start
images = srvr.CreateThreadSafeMap
StartMessageLoop
End Sub
GetImages - This handler creates a simple html that includes the images "urls":
B4X:
Public Sub Handle(req As ServletRequest, resp As ServletResponse)
For Each ip As String In Main.images.Keys
resp.Write("<p>").Write(ip).Write("</p>")
resp.Write("<img width=500 height=300 src='/GetImage?IP=" & ip & "&avoidcache=" & DateTime.Now & "'/>")
Next
End Sub
Note that a time based parameter is added. The parameter value will be different on each request. This way we force the browser to redownload the images.
GetImage - Returns a stored image based on the IP parameter:
B4X:
Public Sub Handle(req As ServletRequest, resp As ServletResponse)
Dim ip As String = req.GetParameter("IP")
Dim img() As Byte = Main.images.Get(ip)
Dim In As InputStream
In.InitializeFromBytesArray(img, 0, img.Length)
File.Copy2(In, resp.OutputStream)
End Sub
How it all connects
The main page (index.html file) uses ajax requests to refresh one of the "divs" every 500ms. This is done with the GetImages handler. Later as a result of the img src values the GetImage handler is called for each image url and returns the most recent image based on the specified IP address.
Note that the server should be able to handle a large number of connected devices. Probably more than 50 or 100.
Attachments
Last edited: