A logo showing the text blog.marcnuri.com
Español
Home»Java»Viewing and storing images from an IP Camera

Recent Posts

  • Fabric8 Kubernetes Client 7.2 is now available!
  • Connecting to an MCP Server from JavaScript using AI SDK
  • Connecting to an MCP Server from JavaScript using LangChain.js
  • The Future of Developer Tools: Adapting to Machine-Based Developers
  • Connecting to a Model Context Protocol (MCP) Server from Java using LangChain4j

Categories

  • Artificial Intelligence
  • Front-end
  • Go
  • Industry and business
  • Java
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Tools

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • August 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • February 2020
  • January 2020
  • December 2019
  • October 2019
  • September 2019
  • July 2019
  • March 2019
  • November 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • July 2017
  • January 2017
  • December 2015
  • November 2015
  • December 2014
  • March 2014
  • February 2011
  • November 2008
  • June 2008
  • May 2008
  • April 2008
  • January 2008
  • November 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

Viewing and storing images from an IP Camera

2007-09-13 in Java / Legacy tagged Camera / ImageIO / IP Camera / Java / JPEG / Surveillance / Swing by Marc Nuri | Last updated: 2025-01-21

If you’ve recently purchased an IP camera and want to programmatically retrieve and store images using Java, this post is for you. Recently I bought a couple of IP Cameras for testing purposes. I noticed that each IP camera has different surveillance tools and web interfaces. But most of these IP cameras have in common that they provide a static image URL that you can access directly via your browser. Leveraging this feature, you can build a simple Java application to grab and store those images.

In this post, I'll show you how to see that image and store it using a very simple Java application. For this demonstration, I’ll be using a LinkSys WVC200 "Wireless PTZ Internet Camera with Audio". This camera was the most affordable option I found with decent features at the time of purchase.

A picture of the LinkSys WVC200 IP camera
A picture of the LinkSys WVC200 IP camera

This camera includes a functional web interface. However, for visualization, you’re tied to a Windows desktop since it relies on an ActiveX control to display and control the image. Thankfully, LinkSys provides the camera’s firmware source code, which gives helpful insights, including the URL to access the static image.

If you analyze the source code you can find some hints to get the URL where the static image is served (there's an URL for ASF video format too, with some time you can develop an app to store and play the video). For this camera, the URL is http://CAMERAIP/img/snapshot.cgi?size=640x480.

With this information, we can create a simple Java application to grab and store the image. If you have a different type of camera, you can adapt the program by specifying the URL for that camera.

IPCameraImageFetcher.java
public class IPCameraImageFetcher {
  public static void main(String[] args) {
    try {
      // Build a new URL specifying the path to where the image is served
      URL url = new URL ( "http://192.168.10.80/img/snapshot.cgi?size=640x480");
      // String with the user and password to access the camera interface
      // (userName and password that you would enter in the browser
      String userPassword = "USER:PASSWORD";
      // We must encode with Base64 to supply it with the header.
      String encoding = new Base64Encoder(userPassword).processString();
      // Open the connection
      URLConnection uc = url.openConnection();
      // Specify the authorization
      uc.setRequestProperty ("Authorization", "Basic " + encoding);
      // Load a copy of the image into memory
      BufferedImage img = ImageIO.read(uc.getInputStream());
      // Store the image in the specified path
      ImageIO.write(img, "jpg", new File("pathToFile"));
    } catch (MalformedURLException ex) {
      ex.printStackTrace();
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }
}

With just a few lines of code, you can extend this application's functionality to create a robust surveillance application or tool. Many commercial surveillance systems share the same basic functionality as the 10 lines of code above, despite their higher price tags. With some effort, you could even add features like periodic image capture, video archiving, or advanced controls.

Twitter iconFacebook iconLinkedIn iconPinterest iconEmail icon

Comments in "Viewing and storing images from an IP Camera"

  • Avatar for cc
    cc
    2007-09-27 23:15
    What kind of performance can you get with

    the above code?

    Can you get 15fps?

    Thanks.
  • Avatar for Marc Nuri
    Marc Nuri
    2007-09-28 08:37
    Hi 'CC'

    The problem with the above code is that each time you get a photo you must open a new connection/request which takes some time, depending on the network (but even with good Intranets, it's slow). It would be the same as pressing the refresh [F5] button in your browser. This is why most cameras offer another type of stream, normally an mjpg (aka many jpg images joint together in one file). If you need such a quick refresh rate (15 fps) you can study capturing the mjpg stream and then separating each jpg from the stream. It's more complicated but possible.

    To do this you should analayze the input stream and search for the BOMof a jpg then save that part of the stream to a separate jpg file.

    You can also try to find a library that can process mjpg or any other type of stream.

    Regards
  • Avatar for IP Camera Guru
    IP Camera Guru
    2008-11-11 20:52
    This is a great tool. The only problem is the static image can sometimes be password protected along with viewing the camera video. The way to get around this is via the FTP feature of the camera.
  • Avatar for IP7138
    IP7138
    2008-12-29 17:43
    This is very handy! Is there any way around password protected video images like in Vivotek cameras?
  • Avatar for IP7138
    IP7138
    2008-12-29 17:44
    This is a handy tool. Is there any way around password protected video images like on Vivotek cameras?
  • Avatar for Network Camera
    Network Camera
    2009-01-21 22:25
    If you know the username and password to the camera, you might be able to setup some type of script with PHP and curl to access a vivotek camera image.
  • Avatar for jmf using ip camera
    jmf using ip camera
    2012-12-26 08:17
    hey can anybody help me jmf using ip camera
  • Avatar for Kumail Haider
    Kumail Haider
    2013-08-15 10:37
    Hello. Do you have any sample programs with complete codes that utilize this snippet? Would really appreciate the help! thanks
  • Avatar for sjpapa
    sjpapa
    2013-12-15 14:37
    Hello Marc,

    Many thanks for the code!

    I have only two issues(still):

    String encoding = new Base64Encoder(userPassword).processString();

    cannot find Base64Encoder import?

    and JLabel ? jLImagen imports:

    /* Show the image in a JLabel */
    jLImagen.setIcon(new ImageIcon(img));


    can you please advize?
  • Avatar for sjpapa
    sjpapa
    2013-12-15 19:32
    Cannot logon.

    I get the error:

    java.io.IOException: Server returned HTTP response code: 401 for URL:...

    any ideas why:

    BufferedImage img = ImageIO.read(uc.getInputStream());

    fails?

    thank you
  • Avatar for AUSDOM
    AUSDOM
    2014-10-21 05:07
    Great!

Post navigation
Reading UTF-8 encoded documents in javajava.lang.OutOfMemoryError: PermGen space // More memory for Tomcat under windows
© 2007 - 2025 Marc Nuri