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

Source Code for Module Camera

 1  # Camera.java 
 2   
 3  ''' 
 4  Class that represents a camera attached to servo motor platform. 
 5   
 6   This software is part of the raspibrick module. 
 7   It is Open Source Free Software, so you may 
 8   - run the code for any purpose 
 9   - study how the code works and adapt it to your needs 
10   - integrate all or parts of the code in your own programs 
11   - redistribute copies of the code777 
12   - improve the code and release your improvements to the public 
13   However the use of the code is entirely your responsibility. 
14   ''' 
15   
16  from Tools import Tools 
17  from RobotInstance import RobotInstance 
18  import picamera 
19  import StringIO 
20   
21 -class Camera():
22 ''' 23 Class that represents a camera attached to servo motor platform. 24 Horizontal servo at servo port 1 (S13 header) 25 Vertical servo at servo port 2 (S14 header) 26 '''
27 - def __init__(self):
28 ''' 29 Creates a camera instance. 30 ''' 31 Tools.debug("Camera instance created")
32
33 - def captureJPEG(self, width, height):
34 ''' 35 Takes a camera picture with given picture size and returns the image 36 in JPEG format. The picture resolution is width x height (max: 5 MPix) 37 @param width: the width of the picture in pixels (max: 2592) 38 @param height: the height of the picture in pixels (max: 1944) 39 return: the image in JPEG format (as string); None, if the capture fails 40 ''' 41 self._checkRobot() 42 camera = picamera.PiCamera() 43 imageData = StringIO.StringIO() 44 w = int(width) 45 h = int(height) 46 47 try: 48 Tools.debug("Camera capture with (width, height) = (%d, %d)" % (w, h)) 49 camera.capture(imageData, format = "jpeg", resize = (w, h)) 50 imageData.seek(0) 51 data = imageData.getvalue() 52 Tools.debug("Captured jpeg size: " + str(len(data))) 53 return data 54 finally: 55 camera.close() 56 return None # error
57
58 - def saveData(self, data, filename):
59 ''' 60 Writes the given string data into a binary file. 61 @param data: the data to store (as string type) 62 @param filename: a valid filename in the local file space 63 ''' 64 file = open(filename, "wb") 65 file.write(data) 66 file.close()
67
68 - def captureAndSave(self, width, height, filename):
69 ''' 70 Takes a camera picture with given picture size and stores is 71 in JPEG format. 72 The picture resolution is width x height (max: 5 MPix) 73 @param width: the width of the picture in pixels (max: 2592) 74 @param height: the height of the picture in pixels (max: 1944) 75 @param filename: a valid filename in the local file space, e.g. /home/pi/shot1.jpg 76 ''' 77 data = self.captureJPEG(width, height) 78 if data != None: 79 self.saveData(data, filename)
80
81 - def _checkRobot(self):
82 if RobotInstance.getRobot() == None: 83 raise Exception("Create Robot instance first")
84