Access ip camera android and display to our monitor using java
Tags: opencv, java
Published: 2022-08-18 04:16:00

Hi developers, today we will make a cctv monitoring using java language. Did you know how java can acces ip of cctv camera? It’s sound so attractive, right? 
Okay, in this article. We will realize how to access ip of cctv using java language 😁 
First time, we need to prepare some tools to make it perfect. There are :

1. Text editor. The text editor we will using netbeans 8.1 IDE.
2. Javacv library. You can download opencv library on https://github.com/junaid67/ip-camera-java-cv-libraries.git . And then you have to extract download folder in your pc
3. Ip webcam application to get ip of camera. You can get and install it in playstore .

Okay, let’s start it.
We must make a project on netbeans. To make a new project you can use `ctrl+shif+n` with your keyboard . Then you will see the project options, select the java application as shown below. After that click next.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8vvwb1sb1luqerrz18bc.png)
After that give project name with `konekcctv`, then `click finish button` to make a new project. Please see this image below.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ny4fobr3lquny7mxy3nd.png)
When finished, a new workspace or new project will shown as image below.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jyfrunjc17yofhmtgedn.png)
In the `konekcctv.java` source code file, there is a konekcctv class and a main class that is used to execute the program code that will be created.
After that, we need to add the javacv library that we have downloaded in the project folder. 
First, right-click on the libraries folder, then select add jar/folder and choose all of extracted javacv library in your pc.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lm22ym6b0s1g9kbv6gaq.png)
After the libraries are imported, all javacv libraries will appear in the libraries folder. See the image below.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9mkhtl5aff2iojkk68ty.png)
After everything is done, the next step is to start coding.
First we need to import the javacv library which was added in the libraries folder earlier. We import outside the konekcctv class,
`Import com.googlecode.javacv.canvasframe;
Import com.googlecode.javacv.opencvframegrabber;
Import com.googlecode.javacv.cpp.opencv_core.iplimage;`
Please see as shown below,
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/awrhsmtrpyjcl8jxvne3.png)

- Import function `com.googlecode.javacv.canvasframe;` is to display the capture frame on our pc screen.
- Then import `com.googlecode.javacv.opencvframegrabber;` we use to retrieve where the video is. Video can be a ready-made video or video from an ip camera.
- And import `com.googlecode.javacv.cpp.opencv_core.iplimage;` is to get the ip address of the camera or cctv used.

After finished of importing,
We will make a function to connect ip camera and display of video in camera to our pc as shown below,

    public static void main(String[] args) throws FrameGrabber.Exception {
        OpenCVFrameGrabber frameGrabber = new OpenCVFrameGrabber("http://192.168.87.96:8080/video?dummy=param.mjpg");
        frameGrabber.setFormat("mjpeg");
        frameGrabber.start();
        IplImage iPimg = frameGrabber.grab();
        CanvasFrame canvasFrame = new CanvasFrame("Camera");
        canvasFrame.setCanvasSize(iPimg.width(), iPimg.height());

        while (canvasFrame.isVisible() && (iPimg = frameGrabber.grab()) != null) {
            canvasFrame.showImage(iPimg);
        }
        frameGrabber.stop();
        canvasFrame.dispose();
        System.exit(0);
    }`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8k92d0b2kd01kzxloa0y.png)

The first thing to do is to create a framegrabber variable to get the video file by calling the camera's ip address using 
`opencvframegrabber.
opencvframegrabber framegrabber = new opencvframegrabber("http://192.168.0.2:8080/video?dummy=param.mjpg");`
`http://192.168.0.2:8080/video?dummy=param.mjpg` is the ip camera address on the ip camera in android application. Each application has its own ip, so match the ip camera with the ip camera on your own device.

Then we set the format of the video to be displayed on our pc with the code `framegrabber.setformat("mjpeg");`
and the frame grabber is executed with the code `framegrabber.start();`
Then to return the ip image or ip camera we create a variable `ipimg = framegrabber.grab();`
`Framegrabber.grab()` is used to return the ip of the framegrabber. 

Then we will display the capture of the ip camera with
`canvasframe canvasframe = new canvasframe("camera");`
`canvasframe.setcanvassize(ipimg.width(), ipimg.height());`
We need to create a canvasframe variable like the source code above. The canvasframe will be used to make the size of the camera view on our pc screen.
And the last step is to create a function to display frames on our pc screen as follows:
`While (canvasframe.isvisible() && (ipimg = framegrabber.grab()) != null) {
            canvasframe.showimage(ipimg);
        }`

After everything is done, we can run the file in the run project section of netbeans, or we can use the shortcut `shif+f6` . But before running the project, the ip camera application on android needs to be turned on first. Then our android and pc need to use the same network otherwise this framegrabber will not find the ip of the camera that has been activated.

This image below is the result of running application,
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xybbo7klih56emstkdwc.png)

Good Luck!! 😁