Module Camera
[hide private]
[frames] | no frames]

Source Code for Module Camera

 1  # Camera.py 
 2  # Remote mode 
 3   
 4  from RobotInstance import RobotInstance 
 5   
6 -class Camera():
7 ''' 8 Class that represents the Raspberry Pi camera. 9 ''' 10
11 - def __init__(self):
12 ''' 13 Creates an instance of a camera. 14 ''' 15 self.device = "cam" 16 robot = RobotInstance.getRobot() 17 if robot == None: # deferred registering, because Robot not yet created 18 RobotInstance._partsToRegister.append(self) 19 else: 20 self._setup(robot)
21
22 - def _setup(self, robot):
23 robot.sendCommand(self.device + ".create") 24 self.robot = robot
25
26 - def captureAndSave(self, width, height, filename):
27 ''' 28 Takes a camera picture with given picture size and stores is 29 in JPEG format on the remote device. 30 The picture resolution is width x height (max: 5 MPix) 31 @param width: the width of the picture in pixels (max: 2592) 32 @param height: the height of the picture in pixels (max: 1944) 33 @param filename: a valid filename in the remote file space, e.g. /home/pi/shot1.jpg 34 ''' 35 self._checkRobot() 36 filename = filename.replace(".", "`") # . is used as command separator 37 self.robot.sendCommand(self.device + ".captureAndSave." 38 + str(width) + "." + str(height) + "." + filename)
39
40 - def captureAndTransfer(self, width, height):
41 ''' 42 Performs a camera capture with given resolution width x height in pixels. 43 The camera picture is hold in memory on the remote device and transferred in JPEG format 44 to the local device. 45 @return: Binary data holding the jpeg formated image (as string). 46 @rtype: str 47 ''' 48 self._checkRobot() 49 self.robot.isBinaryReply = True 50 return self.robot.sendCommand(self.device + ".captureJPEG." + str(width) + "." + str(height))
51
52 - def saveData(self, data, filename):
53 ''' 54 Writes the given string data into a binary file. 55 @param data: the image data (as string) to store 56 @param filename: a valid filename in the local file space 57 ''' 58 file = open(filename, "wb") 59 file.write(data) 60 file.close()
61
62 - def _checkRobot(self):
63 if RobotInstance.getRobot() == None: 64 raise Exception("Create Robot instance first")
65