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. 🙂

[code=php]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();
?>[/code]

class_camera.php:[code=php]host = $host;
return $this->connection_id = substr(file_get_contents (‘//’ . $host . ‘/-wvhttp-01-/OpenCameraServer?client_version=LiveApplet_4125&image_size=640×480’), 14, 9);
}

/**
* Disconnects from the camera
*
* @return none
*/
function disconnect()
{
file_get_contents (‘//’ . $this->host . ‘/-wvhttp-01-/CloseCameraServer?connection_id=’ . $this->connection_id);
}

/**
* Attempts to get control of camera
*
* @return none
*/
function get_control()
{
file_get_contents (‘//’ . $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 (‘//’ . $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 (‘//’ . $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 (‘//’ . $this->host . ‘/-wvhttp-01-/OperateCamera?’ . $parameter . ‘=’ . $value . ‘&connection_id=’ . $this->connection_id);
}

/**
* Get current image
*
* @return none
*/
function image()
{
return file_get_contents (‘//’ . $this->host . ‘/-wvhttp-01-/GetOneShot?image_size=640×480’);
}

}
?>[/code]

13 thoughts on “Canon VB-50i PHP Control Class”

  1. 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)…

    [code=php] $files = explode (“\r\n”, shell_exec(‘/usr/bin/curl -ls ftp://mylogin:[email protected]/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:[email protected]/var/tmp/images/{‘ . implode (‘,’, $files). ‘}.jpg’);
    }[/code]

  2. 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 😀 ?

    So the Java applet comes with the cam?

  3. 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? 🙂

  4. 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. 🙂

  5. 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… 🙂

  6. 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

Leave a Reply

Your email address will not be published. Required fields are marked *