Wednesday, August 30th, 2006Wednesday, August 30th, 2006

Canon VB-50i PHP Control Class

I tried to find a PHP class to control the new camera, and I couldn't find one... so a little packet sniffing later, I was able to figure out how it's little Java applet communicates with the camera. Anyway, I spent a few minutes to make a PHP class to control the camera, so if you need one, here you go (it hasn't been extensively tested, so it probably has problems... if it does, leave a comment)...

So here's a basic PHP script that uses the class to take control of the camera, set the pan/tilt/zoom to a certain position, capture the image and save it to a file... handy for uhm... I dunno... a cron job to take time lapse photography. :)

<?php
	include ('class_camera.php');
	
	$cam =& new Camera();
	
	$cam->connect('your.camera.ip.address');
	$cam->get_control();
	$cam->goto( -7463, -548, 4126);
	sleep(5);  // Wait for movement to finish and lens to focus
	
	$handle = fopen('images/' . time() . '.jpg', 'w');
	fwrite ($handle, $cam->image());
	fclose ($handle);
	
	$cam->disconnect();
?>

class_camera.php:

<?php
	
// #############################################################################
// Canon Camera Control Class
// http://www.shawnhogan.com/2006/08/canon-vb-50i-php-control-class.html
	
/**
*
* Class to interface with a Canon VB-C50i or VB-C50iR
*
*/
	
class Camera
{
	/**
	* Connects to the specified camera
	*
	* @param	string	Hostname or IP address of the camera
	*
	* @return	connection_id
	*/
	function connect($host)
	{
		$this->host = $host;
		return $this->connection_id = substr(file_get_contents ('http://' . $host . '/-wvhttp-01-/OpenCameraServer?client_version=LiveApplet_4125&image_size=640x480'), 14, 9);
	}
	
	/**
	* Disconnects from the camera
	*
	* @return	none
	*/
	function disconnect()
	{
		file_get_contents ('http://' . $this->host . '/-wvhttp-01-/CloseCameraServer?connection_id=' . $this->connection_id);
	}
	
	/**
	* Attempts to get control of camera
	*
	* @return	none
	*/
	function get_control()
	{
		file_get_contents ('http://' . $this->host . '/-wvhttp-01-/GetCameraControl?connection_id=' . $this->connection_id);
	}
	
	/**
	* Get camera info
	*
	* @return	array of camera info variables
	*/
	function get_info()
	{
		preg_match_all ('#(.*?)=(.*)#', file_get_contents ('http://' . $this->host . '/-wvhttp-01-/GetCameraInfo?connection_id=' . $this->connection_id), $matches);
		foreach ($matches[1] as $key => $value) {
			$this->camera_info[$value] = $matches[2][$key];
		}
		return($this->camera_info);
	}
	
	/**
	* Move camera
	*
	* @param	string	Movement type (pan | tilt | zoom)
	* @param	number	Position to move to
	*
	* @return	none
	*/
	function move($operation, $position = 0)
	{
		file_get_contents ('http://' . $this->host . '/-wvhttp-01-/OperateCamera?' . $operation . '=' . $position . '&connection_id=' . $this->connection_id);
	}
	
	/**
	* Set pan/tilt/zoom on camera with one call
	*
	* @param	number	pan position
	* @param	number	tilt position
	* @param	number	zoom position
	*
	* @return	none
	*/
	function goto($pan, $tilt, $zoom)
	{
		$this->move('pan', $pan);
		$this->move('tilt', $tilt);
		$this->move('zoom', $zoom);
	}
	
	/**
	* Set parameter
	*
	* @param	string	paramter to set
	* @param	string	value
	*
	* @return	none
	*/
	function set_value($parameter, $value)
	{
		$mapping = array (
						'back_light' => 'B',
					);
	
		$parameter = $mapping[$parameter];
	
		file_get_contents ('http://' . $this->host . '/-wvhttp-01-/OperateCamera?' . $parameter . '=' . $value . '&connection_id=' . $this->connection_id);
	}
	
	/**
	* Get current image
	*
	* @return	none
	*/
	function image()
	{
		return file_get_contents ('http://' . $this->host . '/-wvhttp-01-/GetOneShot?image_size=640x480');
	}
	
}
?>

13 Responses to “Canon VB-50i PHP Control Class”

  1. Ron Says:

    So when can we see the real thing? :D

  2. Rachel Dizzel Says:

    Nobody likes your code talk!!!

  3. Shawn Says:

    Are you kidding me? Everyone likes it… especially you.

  4. Rachel Dizzel Says:

    ok, your right, I love it! More please!

  5. Shawn Says:

    See? I knew you would enjoy it.

    Okay, here’s something I did this morning (it automatically downloads the time lapse pictures from the camera… but of course you would know that from looking at it)…

    	$files = explode (\r\n, shell_exec(‘/usr/bin/curl -ls ftp://mylogin:mypassword@my.ip.address/var/tmp/images/’));
    	foreach ($files as $key => $file) {
    		if (strlen($file) == 20) {
    			$files[$key] = substr($file, 0, 16);
    		} else {
    			unset($files[$key]);
    		}
    	}
    	
    	//	print_r ($files);
    	if (count($files)) {
    		chdir (‘images’);
    		shell_exec(‘/usr/bin/curl -s’ . str_pad(‘’, count($files), ‘O’) . ‘ ftp://mylogin:mypassword@my.ip.address/var/tmp/images/{’ . implode (‘,’, $files). ‘}.jpg’);
    	}

  6. Damnz Says:

    You’d think after you write all this class, the guy(Shawn) would use it :) . But seems it seems he doesn’t :) . Check out the “/images/” directory. By the way how come you didn’t put an index for it, you might get hacked :D ?

    So the Java applet comes with the cam?

  7. Shawn Says:

    Yes, the Java applet comes with the camera.

    I do use the class, why do you think I’m not?

    Just because you can’t find the images, doesn’t mean they don’t exist (they aren’t on a public web server).

    Also, how is not having an index file for the images directory for my blog going to get my site hacked? :)

  8. Damnz Says:

    LOL! Okay, sorry I thought you were gonna use the cam on this server :) . I dunno how, but I read once that it was dangerous, probably if you have some imporant files that you don’t want anyone to know about. :)

  9. Shawn Says:

    I don’t have any dangerous files I don’t want people to know about… the images folder is just images from this blog. If they read all the entries, ultimately they would see every one anyway, so… :)

  10. Damnz Says:

    Ya, I guess you’re right. Sorry.. :) .

  11. Eric Says:

    Shawn,

    I’d like to modify the camera class and blog about it. Can I have permission to use and modify it. I will put an attribution in the file back to this blog post and you….

    Thanks
    Eric

  12. dirk maas Says:

    Jim Palmer trying to do the same in flash
    http://www.overset.com/2007/01/24/canon-network-camera-control-protocol-hack-vb-c50i-control-port-communication/
    http://www.overset.com/2007/01/22/live-streaming-of-a-webcam-feed-from-canon-vb-c10c50-from-a-relay-proxy-server/#comment-389

  13. Jim Says:

    Great idea to use the camera’s internal webserver! I did a little hacking around to use the camera’s control port directly:

    http://www.overset.com/2007/01/24/canon-network-camera-control-protocol-hack-vb-c50i-control-port-communication/

Leave a Reply