Code:
#=================================================
#===== FULL SCRREN CAMERA VIEWFINDER =====
#=================================================
from appuifw import *
from graphics import *
from key_codes import *
import camera, e32
class Keyboard(object):
def __init__(self,onevent=lambda:None):
self._keyboard_state={}
self._downs={}
self._onevent=onevent
def handle_event(self,event):
if event['type'] == appuifw.EEventKeyDown:
code=event['scancode']
if not self.is_down(code):
self._downs(code)=self._downs.get(code,0)+1
self._keyboard_state(code)=1
elif event['type'] == appuifw.EEventKeyUp:
self._keyboard_state[event['scancode']]=0
self._onevent()
def is_down(self,scancode):
return self._keyboard_state.get(scancode,0)
def pressed(self,scancode):
if self._downs.get(scancode,0):
self._downs[scancode]-=1
return True
return False
key = Keyboard()
app.body = canv = Canvas(event_callback=key.handle_event)
appuifw.app.screen = 'large' # only softkeys visible
string = ''
#--------------- flash modes ------------
flash_mode = 'auto'
def flash_auto():
global flash_mode
flash_mode = 'auto'
def flash_on():
global flash_mode
flash_mode = 'forced'
def flash_off():
global flash_mode
flash_mode = 'none'
#--------------- white balance ----------
white_bal = 'auto'
def whbal_auto():
global white_bal
white_bal = 'auto'
def whbal_daylight():
global white_bal
white_bal = 'daylight'
def whbal_cloudy():
global white_bal
white_bal = 'cloudy'
def whbal_fluorescent():
global white_bal
white_bal = 'fluorescent'
def whbal_tungsten():
global white_bal
white_bal = 'tungsten'
#------------ picture size --------------
pic_size = (1600,1200)
def set_2mpx():
global pic_size
pic_size = (1600,1200)
def set_svga():
global pic_size
pic_size = (1024,768)
def set_vga():
global pic_size
pic_size = (640,480)
#------------- exposure -----------------
exp_mode = 'auto'
def exp_auto():
global exp_mode
exp_mode = 'auto'
def exp_center():
global exp_mode
exp_mode = 'center'
def exp_night():
global exp_mode
exp_mode = 'night'
#--------- test if file exists ----------
def file_exists(f):
try:
file = open(f)
except IOError:
exists = 0
else:
exists = 1
return exists
#------------ read counter --------------
counter = 0
def read_counter():
global counter
# try to open ini file in which counter is stored
if file_exists('E:\\System\\Apps\\Python\\my\\counter.ini') == 0:
# file does not exist, so create it
file = open('E:\\System\\Apps\\Python\\my\\counter.ini','w')
# write initial value of the counter
file.write('000')
# close the file
file.close()
# now open the file again to read it
file = open('E:\\System\\Apps\\Python\\my\\counter.ini','r')
# read counter from inside
string = file.readline()
# convert string to integer
counter = int(string)
# close the file
file.close()
#------------ write counter -------------
def write_counter():
#open ini file in which counter is stored
file = open('E:\\System\\Apps\\Python\\my\\counter.ini','w')
# convert counter to string
string = str(counter)
# write counter in file
file.write(string)
# close the file
file.close()
#------------ image saving --------------
def capture():
global counter
if save_pic == 1:
# read counter from settings file
read_counter()
# convert the couter into string to save the file
string = str(counter)
#full path to file
path = 'E:\\Images\\IMG' + string + '.jpg'
# take picture at selected resolution
img = camera.take_photo('RGB',pic_size,0,flash_mode,exp_mode,white_bal,0)
# save the picture
img.save(path, bpp=24, quality=100, compression='fast')
# increase image counter
counter += 1
# store the counter in settings file
write_counter()
else:
# take a fast picture to display on screen
img = camera.take_photo('RGB16',(640,480),0,'none',exp_mode,white_bal,0)
# display aiming area
img.line([(310,240),(330,240)], 0x00FF00) # horizontal aiming line
img.line([(320,230),(320,250)], 0x00FF00) # vertical aiming line
img.line([(280,220),(280,210),(290,210)], 0x00FF00) # upper left corner
img.line([(280,260),(280,270),(290,270)], 0x00FF00) # lower left corner
img.line([(350,210),(360,210),(360,220)], 0x00FF00) # upper right corner
img.line([(350,270),(360,270),(360,260)], 0x00FF00) # lower right corner
# outline for statuses
img.rectangle([(233,139),(267,152)],0x0000FF);
img.rectangle([(267,139),(300,152)],0x0000FF);
img.rectangle([(300,139),(345,152)],0x0000FF);
img.rectangle([(345,139),(407,152)],0x0000FF);
# picture size status
if pic_size == (1600,1200):
img.text((235,150),u'2MPx',0x50FFFF,u'LatinPlain19')
elif pic_size == (1024,768):
img.text((235,150),u'SVGA',0x50FFFF,u'LatinPlain19')
elif pic_size == (640,480):
img.text((235,150),u' VGA',0x50FFFF,u'LatinPlain19')
# flash mode status
if flash_mode == 'auto':
img.text((271,150),u'Auto',0x50FFFF,u'LatinPlain19')
elif flash_mode == 'forced':
img.text((269,150),u' ON',0x50FFFF,u'LatinPlain19')
elif flash_mode == 'none':
img.text((269,150),u' OFF',0x50FFFF,u'LatinPlain19')
# exposure status
if exp_mode == 'auto':
img.text((302,150),u' Auto',0x50FFFF,u'LatinPlain19')
elif exp_mode == 'center':
img.text((302,150),u'Center',0x50FFFF,u'LatinPlain19')
elif exp_mode == 'night':
img.text((302,150),u' Night',0x50FFFF,u'LatinPlain19')
# white balance status
if white_bal == 'auto':
img.text((347,150),u' Auto',0x50FFFF,u'LatinPlain19')
elif white_bal == 'cloudy':
img.text((347,150),u' Cloudy',0x50FFFF,u'LatinPlain19')
elif white_bal == 'daylight':
img.text((347,150),u' Sunny',0x50FFFF,u'LatinPlain19')
elif white_bal == 'fluorescent':
img.text((347,150),u' Neon',0x50FFFF,u'LatinPlain19')
elif white_bal == 'tungsten':
img.text((347,150),u'Tungsten',0x50FFFF,u'LatinPlain19')
canv.blit(img, (232,136,408,344)) # cut 176x208 from picture and display it on screen
aim = Image.new((20,20), 'RGB16') # draw aiming lines in another image
aim.blit(img, (78,104,89,114)) # show the aim box in center of the screen
#----------- activate saving ------------
save_pic = 0
def activate_save():
global save_pic
save_pic = 1
#--------- deactivate saving ------------
def deactivate_save():
global save_pic
save_pic = 0
#---------- quit application ------------
running=1
def quit():
global running
running = 0
app.exit_key_handler=quit
# menu for image size, flash mode, white balance and exposure
appuifw.app.menu = [(u"Image size",((u"2MPx 1600 x 1200",set_2mpx),
(u"SVGA 1024 x 768",set_svga),
(u"VGA 640 x 480",set_vga)
)
),
(u"Flash mode",((u"Auto",flash_auto),
(u"On", flash_on),
(u"Off", flash_off)
)
),
(u"Exposure",((u"Auto", exp_auto),
(u"Center",exp_center),
(u"Night", exp_night)
)
),
(u"White balance",((u"Auto", whbal_auto),
(u"Daylight", whbal_daylight),
(u"Cloudy", whbal_cloudy),
(u"Fluorescent",whbal_fluorescent),
(u"Tungsten", whbal_tungsten)
)
),
(u"Back to Python",quit)
]
#=============== MAIN CODE ===============
while running:
# save picture if joystick was pressed otherwise just take the picture
if key.is_down(EScancodeSelect):
activate_save()
# capture with saving
capture()
else:
# reset saving
deactivate_save()
# capture without saving
capture()