1+ #!/usr/bin/python
2+ # Authoer: Spencer.Luo
3+ # Date: 12/08/2018
4+
5+ # Version 1.0
6+ #=======================================================================================================================
7+ import random
8+ # 引入随机数模块
9+
10+ class Camera :
11+ """相机机身"""
12+
13+ # 对焦类型
14+ SingleFocus = "单点对焦"
15+ AreaFocus = "区域对焦"
16+ BigAreaFocus = "大区域对焦"
17+ Focus45 = "45点自动对焦"
18+
19+ def __init__ (self , name ):
20+ self .__name = name
21+ self .__aperture = 0.0 # 光圈
22+ self .__shutterSpeed = 0 # 快门速度
23+ self .__ligthSensitivity = 0 # 感光度
24+ self .__lens = Lens () # 镜头
25+ self .__sdCard = SDCard () # SD卡
26+ self .__display = Display () # 显示器
27+
28+ def shooting (self ):
29+ """拍照"""
30+ print ("[开始拍摄中" )
31+ imageLighting = self .__lens .collecting ()
32+ # 通过快门、光圈和感光度、测光来控制拍摄的过程,省略此部分
33+ image = self .__transferImage (imageLighting )
34+ self .__sdCard .addImage (image )
35+ print ("拍摄完成]" )
36+
37+ def viewImage (self , index ):
38+ """查看图像"""
39+ print ("查看第%d张图像:" % (index + 1 ))
40+ image = self .__sdCard .getImage (index )
41+ self .__display .showImage (image )
42+
43+ def __transferImage (self , imageLighting ):
44+ """接收光线并处理成数字信号,简单模拟"""
45+ print ("接收光线并处理成数字信号" )
46+ return Image (6000 , 4000 , imageLighting )
47+
48+ def setting (self , aperture , shutterSpeed , ligthSensitivity ):
49+ """设置相机的拍摄属性:光圈、快门、感光度"""
50+ self .__aperture = aperture
51+ self .__shutterSpeed = shutterSpeed
52+ self .__ligthSensitivity = ligthSensitivity
53+
54+ def focusing (self , focusMode ):
55+ """对焦,要通过镜头来调节焦点"""
56+ self .__lens .setFocus (focusMode )
57+
58+ def showInfo (self ):
59+ """显示相机的属性"""
60+ print ("%s的设置 光圈:F%0.1f 快门:1/%d 感光度:ISO %d" %
61+ (self .__name , self .__aperture , self .__shutterSpeed , self .__ligthSensitivity ))
62+
63+
64+ class Lens :
65+ """镜头"""
66+
67+ def __init__ (self ):
68+ self .__focusMode = '' # 对焦
69+ self .__scenes = {0 : '风光' , 1 : '生态' , 2 : '人文' , 3 : '纪实' , 4 : '人像' , 5 : '建筑' }
70+
71+ def setFocus (self , focusMode ):
72+ self .__focusMode = focusMode
73+
74+ def collecting (self ):
75+ """图像采集,采用随机的方式来模拟自然的拍摄过程"""
76+ print ("采集光线,%s" % self .__focusMode )
77+ index = random .randint (0 , len (self .__scenes )- 1 )
78+ scens = self .__scenes [index ]
79+ return "美丽的 " + scens + " 图像"
80+
81+
82+ class Display :
83+ """显示器"""
84+
85+ def showImage (self , image ):
86+ print ("图片大小:%d x %d, 图片内容:%s" % (image .getWidth (), image .getHeight (), image .getPix ()))
87+
88+
89+ class SDCard :
90+ """SD存储卡"""
91+
92+ def __init__ (self ):
93+ self .__images = []
94+
95+ def addImage (self , image ):
96+ print ("存储图像" )
97+ self .__images .append (image )
98+
99+ def getImage (self , index ):
100+ if (index >= 0 and index < len (self .__images )):
101+ return self .__images [index ]
102+ else :
103+ return None
104+
105+
106+ class Image :
107+ """图像(图片), 方便起见用字符串来代码图像的内容(像素)"""
108+
109+ def __init__ (self , width , height , pixels ):
110+ self .__width = width
111+ self .__height = height
112+ self .__pixels = pixels
113+
114+ def getWidth (self ):
115+ return self .__width
116+
117+ def getHeight (self ):
118+ return self .__height
119+
120+ def getPix (self ):
121+ return self .__pixels
122+
123+
124+ # Version 2.0
125+ #=======================================================================================================================
126+ # 代码框架
127+ #==============================
128+
129+ # 基于框架的实现
130+ #==============================
131+
132+
133+ # Test
134+ #=======================================================================================================================
135+ def testCamera ():
136+ camera = Camera ("EOS 80D" )
137+ camera .setting (3.5 , 60 , 200 )
138+ camera .showInfo ()
139+ camera .focusing (Camera .BigAreaFocus )
140+ camera .shooting ()
141+ print ()
142+
143+ camera .setting (5.6 , 720 , 100 )
144+ camera .showInfo ()
145+ camera .focusing (Camera .Focus45 )
146+ camera .shooting ()
147+ print ()
148+
149+ camera .viewImage (0 )
150+ camera .viewImage (1 )
151+
152+
153+ testCamera ()
0 commit comments