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,387
  • CameraCCTVClient.zip
    10.3 KB · Views: 4,234
Last edited:

realblue

Member
Licensed User
Longtime User
Hi Erel,

I took the CCTV example as a base to my next project. Everything works fine except that if the Android device is in portrait mode and using front camera the preview image in the PC side is rotated 90 Clockwise.

How should I correct it?
 

realblue

Member
Licensed User
Longtime User
Hi Erel,

CCTV example does not create a file and send it to server (which is perfectly what I need), instead creates a stream. On the PC side stream is converted to a bitmap and picturebox object displays it. So I don't think that there is any header information is involved.

Could you check on the CCTV example?
 

realblue

Member
Licensed User
Longtime User
For those who are having rotation problem change the code in CCTVServer as following

B4X:
...
MethodInvoker mi = delegate()
 {
  form1.pictureBox1.Image = bmp;
  if (form1.chkRotate.Checked)
     {
       form1.pictureBox1.Image.RotateFlip(RotateFlipType.Rotate270FlipNone);
       form1.pictureBox1.Invalidate();
      }
  };
...

The change has to be made in clientLoop procedure and you should add a check box named chkRotate in form1.
 

sangee

Member
Licensed User
Longtime User
Hi,

Has anyone tried making the server side on Linux? I am trying hard to do this with my limited knowledge in Java using the code below and seems to go no where :D .. I want to make the android send the image to my Linux server so that I can show it in my web page. Help please... :)

B4X:
import java.io.*;
import java.net.*;

public class FileServer {

    public static void main(String[] args) throws IOException {
        int filesize = 6022386; // filesize temporary hardcoded

        long start = System.currentTimeMillis();
        int bytesRead;
        int current = 0;

        // create socket
        ServerSocket servsock = new ServerSocket(27015);
        while (true) {
            System.out.println("Waiting...");

            Socket sock = servsock.accept();
            System.out.println("Accepted connection : " + sock);

            // receive file
            byte[] mybytearray = new byte[filesize];
            InputStream is = sock.getInputStream();
            FileOutputStream fos = new FileOutputStream( "/var/www/html/test.jpg"); 

// destination
                                                                    // path and
                                                                    // name of
                                                                    // file
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            bytesRead = is.read(mybytearray, 0, mybytearray.length);
            current = bytesRead;

            // thanks to A. Cádiz for the bug fix
            do {
                bytesRead = is.read(mybytearray, current,
                        (mybytearray.length - current));
                if (bytesRead >= 0)
                    current += bytesRead;
            } while (bytesRead > -1);

            bos.write(mybytearray, 0, current);
            bos.flush();
            long end = System.currentTimeMillis();
            System.out.println(end - start);
            bos.close();

            sock.close();
        }
    }

}

BTW. The above code is stolen from http://stackoverflow.com/questions/...ient-sends-image-to-java-server-cant-send-the

and it does not even start in my linux server when i call #java Fileserver.java
 
Last edited:

YIM bunchhat

Active Member
Licensed User
Longtime User
hello Erel,

It is very cool, But it use Computer with Phone, and I would like to know how to use between Phone and Phone. Please help me I am new on Basic4android. Thank you......:)
 

YIM bunchhat

Active Member
Licensed User
Longtime User
You can combine this example with AsyncStreamsObject: AsyncStreamsObject - Send and receive objects instead of bytes

Hi Erel,
I have tried to combine this example with AsyncStreamsObject but I still cannot understand the way to receive video. It can connect to other phone but don't know how to display picture on that other phone. Totally I want to make spy camera by using 2 phones that I will replace PC in your example by other phone.
 

positrom2

Active Member
Licensed User
Longtime User
I had adjusted the Windows firewall according to the post #1. However, the Android application logs "connection refused". What else might have to be changed? I also tried disconnecting the B4A bridge on the phone, that did not help.
 

Douglas Farias

Expert
Licensed User
Longtime User
hi to all... firsth Merry XMAS for all =)
now my question xD

information :
Erel your server dont work here only in vb. from Geezel
with vb work fine your client and geezel client

question:
how can i make for use this with external ip for exemple using one vps for take images from my camera ?
it is only local ?

and my other question is how to make a imput text with my ip
for exemple.

user open the server and have START - STOP - YOUR IP LOCAL IS : XXX.XXX.XXX.XXX YOUR IP EXTERNAL : XXX.XXX.XXX.XXX
AND IN APP USER CAN CHANGE THE IP AND PRESS CONNECT.

FOR DONT NEED COMPILE ALL TIME WITH NEW IP?*-*-*

thx to all
remember i m not programmer.
if can u post a exemple i thx u =)

thx to all and Merry XMAS
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Erel your server dont work here only in vb. from Geezel
with vb work fine your client and geezel client
Note sure that I understand...

how can i make for use this with external ip for exemple using one vps for take images from my camera ?
The same code can work on a server. You just need to set the server ip address (and configure the server firewall).

You should learn about files. It will allow you to save the settings and load them when the app starts.
 

Douglas Farias

Expert
Licensed User
Longtime User
Note sure that I understand...
with your server exemple dont work for me. show a mensage no connection. and dont work with my local ip 192.168.1.3
i tested geezel server with your client apk and work and with gezeel apk work too only your server dont work.
ok but it is a only information no problem :)



The same code can work on a server. You just need to set the server ip address (and configure the server firewall).

You should learn about files. It will allow you to save the settings and load them when the app starts.

for me dont work for exemple i try 192.168.1.3 < local ip work fine
but my real ip no 187.113.xxx.xxx this dont work my firewall is off *-*
have a ideia ?
 

LucaMs

Expert
Licensed User
Longtime User
One question that will be a little like a message in a bottle.

I have tried this and it works, but not well.
For a few seconds I get "the movie", then the picture freezes and, perhaps, resumes after a few seconds.

Hardware or network problems, I guess, but I was wondering to myself if this happens to someone else
 
Top