Module Camera
[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 raspibrickpi 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 code 
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  import picamera 
18  import StringIO 
19   
20 -class Camera():
21 ''' 22 Class that represents a camera attached to servo motor platform. 23 Horizontal servo at servo port 1 (S13 header) 24 Vertical servo at servo port 2 (S14 header) 25 '''
26 - def __init__(self):
27 ''' 28 Creates a camera instance. 29 ''' 30 Tools.debug("Camera instance created")
31
32 - def captureJPEG(self, width, height):
33 ''' 34 Takes a camera picture with given picture size and returns the image 35 in JPEG format. The picture resolution is width x height (max: 5 MPix) 36 @param width: the width of the picture in pixels (max: 2592) 37 @param height: the height of the picture in pixels (max: 1944) 38 return: the image in JPEG format (as string); None, if the capture fails 39 ''' 40 camera = picamera.PiCamera() 41 imageData = StringIO.StringIO() 42 w = int(width) 43 h = int(height) 44 45 try: 46 Tools.debug("Camera capture with (width, height) = (%d, %d)" % (w, h)) 47 camera.capture(imageData, format = "jpeg", resize = (w, h)) 48 imageData.seek(0) 49 data = imageData.getvalue() 50 Tools.debug("Captured jpeg size: " + str(len(data))) 51 return data 52 finally: 53 camera.close() 54 return None # error
55
56 - def saveData(self, data, filename):
57 ''' 58 Writes the given string data into a binary file. 59 @param data: the data to store (as string type) 60 @param filename: a valid filename in the local file space 61 ''' 62 file = open(filename, "wb") 63 file.write(data) 64 file.close()
65
66 - def captureAndSave(self, width, height, filename):
67 ''' 68 Takes a camera picture with given picture size and stores is 69 in JPEG format. 70 The picture resolution is width x height (max: 5 MPix) 71 @param width: the width of the picture in pixels (max: 2592) 72 @param height: the height of the picture in pixels (max: 1944) 73 @param filename: a valid filename in the local file space, e.g. /home/pi/shot1.jpg 74 ''' 75 data = self.captureJPEG(width, height) 76 if data != None: 77 self.saveData(data, filename)
78