Android Tutorial Android based Closed Circuit TV (CCTV) example

Better example is available here: https://www.b4x.com/android/forum/threads/mjpeg-cctv-server.73792/

This example is made of two components:

Client - Basic4android program that takes the camera preview frames and sends them to the server.

Server - C# program that accepts incoming connections and shows the camera frames.

The code of both components is pretty simple and is a good example of creating network based solutions. Note that both the client and server components should overcome network problems and continue to work when the network is again available.

How to run this example
1. Find the desktop ip address (you can run ipconfig from the command line).
2. Set this address in the device program - ServerIp variable.

Most complicated step:
3. By default Windows firewall blocks incoming connection. You need to open the firewall settings and add an exception for port 17178 (TCP).
Windows 7:

SS-2012-11-22_17.12.41.png


Run the desktop program and press Start.
Run the device program and check the logs in the IDE.

The source code of both projects is attached as well as the compiled desktop executable.

This example requires Android 2.2+ as the API that converts the preview image to JPEG is not available on older devices. The reflection library is also required.

Feel free to ask any question about this implementation.

The client code was updated to use CameraEx class. Note that it requires Camera library v2.0+.
 

Attachments

  • CCTVServer.zip
    30.1 KB · Views: 4,396
  • CameraCCTVClient.zip
    10.3 KB · Views: 4,243
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this example with a local network or over the internet.

The second case is a bit more complicated. Assuming that you do not have a static ip address you will need to use a third party service that assigns a hostname to your dynamic address.

You will also need to add a "forward port" rule in the router.

There are many online tutorials that can help you with these tasks.
 

aaronk

Well-Known Member
Licensed User
Longtime User
Hi Erel,

I like it.. good one.

But how would you send the image to a phone/tablet rather than a desktop PC program ?

Also, can Audio also be done ?
 

MLDev

Active Member
Licensed User
Longtime User
It works great here! I'm going through the code now to see how it works. :D

Thanks Erel
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
But how would you send the image to a phone/tablet rather than a desktop PC program ?
It shouldn't be too difficult to create a server application that runs on the device. In that case you should initialize AsyncStreams in prefix mode and you do not need to send the size of the image.

Also, can Audio also be done ?
Audio can be done but it is more complicated as you need to implement some streaming protocol (unlike the video which is actually made of a series of frames).
 

selvamurali

Active Member
Licensed User
Longtime User
Get Error while run CCTv example

Hi Erel

when i try to run your CCTV Client example i got the following error .

let me know what i am missing. i attach the error screen shot .
 

Attachments

  • cctverror.png
    cctverror.png
    45.3 KB · Views: 1,355

Azlan

Member
Licensed User
Longtime User
Hi Just gave this a try.

App on phone crashes.

Log shows:

Installing file.
PackageAdded: package:anywheresoftware.b4a.samples.camera
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (communicator) Create **
Connecting to: 192.168.0.109:17178
** Service (communicator) Start **
Client_Connected: true
cameraexclass_setdisplayorientation (B4A line: 88)
r.target = r.GetActivity
java.lang.NoSuchFieldError: anywheresoftware.b4a.BA.activityBA
at anywheresoftware.b4a.agraham.reflection.Reflection.GetActivity(Reflection.java:638)
at anywheresoftware.b4a.samples.camera.cameraexclass._setdisplayorientation(cameraexclass.java:596)
at anywheresoftware.b4a.samples.camera.cameraexclass._camera_ready(cameraexclass.java:125)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:151)
at anywheresoftware.b4a.objects.CameraW$2$1.run(CameraW.java:137)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)

Firewall, port settings etc ok. S3 phone with version 4.1.2

Cheers.
 
Top