# coding: utf8 ############################################################################### # Author: gaohaibo # Email: gaohb@live.cn # Datetime: 2015-04-15 08:29:41 Wednesday # Version: 0.2 # Description: 网络接入点处理 ############################################################################### import sys try: # http://discussion.forum.nokia.com/forum/showthread.php?p=575213 # Try to import 'btsocket' as 'socket' (just for 1.9.x and up) # 对于版本>=PythonForS60_1.9.x的pys60需要这样用,只是import btsocket是不行的 sys.modules['socket'] = __import__('btsocket') except ImportError: pass import socket import appuifw APID_SETTING_FILE = 'c:\\data\\python\\apid.txt' def set_accesspoint(): """ 设置网络接入点,并把接入点id号保存到文本文件中,便于后续程序读取该配置文件. 使用: 在程序初始时,执行set_accesspoint(),手机便会弹出网络接入点选择框. """ apid = socket.select_access_point() #弹出手机的接入点选择菜单 f = open(APID_SETTING_FILE,'w') #打开文件 f.write(repr(apid)) #将所选的接入点id号写进文件 f.close() appuifw.note(u"Saved default access point.", "info") #手机信息提醒框 apo = socket.access_point(apid) #通过接入点id号获取接入点完整信息 socket.set_default_access_point(apo) #设置默认接入点 def unset_accesspoint(): """清除自定义接入点,设为None.""" f = open(APID_SETTING_FILE,'w') f.write(repr(None)) #设接入点id号为None f.close() appuifw.note(u"Default access point is unset.", "info") def read_accesspoint(): """ 读取网络接入点配置文件,如此当手机无网络链接时间较长是, 便不会再此弹出网络接入点的选择框,正所谓一次配置,自动执行. 使用: 在程序要执行联网操作前,执行read_accesspoint(),便读取配置并直接联网. """ try: f=open(APID_SETTING_FILE,'rb') setting = f.read() #读取接入点文件 apid = eval(setting) #执行 f.close() if not apid == None : #是否为None,即检测是否设置了自定义接入点 apo = socket.access_point(apid) socket.set_default_access_point(apo) else: set_accesspoint() except: set_accesspoint() ############################################################################### # 使用TCP协议发送图片数据 ############################################################################### import socket HOST = '192.168.1.110' # The remote host PORT = 12008 # The same port as used by the server def set_host_and_port(): '''设置服务端IP和端口,端口到时有默认可选,存入host_ip.cfg文件中.''' pass def send_data(data): '''use tcp send image base64 code''' try: sock_clt = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock_clt.connect((HOST, PORT)) sock_clt.send(data) finally: sock_clt.close() ############################################################################### # 手机使用camera.viewfinder(cb,size),在cb中执行监控代码. ############################################################################### #Using the viewfinder import camera, e32, appuifw import base64 import time def quit(): ''' 程序退出时,停掉finder,释放摄像头and发信号给Ao_lock ''' camera.stop_finder() #Close the viewfinder camera.release() #Release the camera so other applications may use it app_lock.signal() IMG_PATH = 'c:\\data\\python\\photo.jpg' # 照片缓存完整路径 def set_img_path(): '''程序首次运行时设置缓存完整路径,存入img_path.cfg文件中''' pass def v_finder(im): ''' Define a function for the viewfinder ''' try: if im: canvas.blit(im) # 以下代码若能优化下最好,目前是先保存为图片,再打开读取此图片. # start_time = time.time() im.text((0,20),unicode(time.strftime("%Y/%m/%d %H:%M:%S")),fill=0xffff00) im.save(IMG_PATH) # 这里最好try下,如果异常则弹出框重新设置照片缓存路径(set_img_path) # print "time on save: %s" % (time.time() - start_time) try: # start_time1 = time.time() imgfile = open(IMG_PATH,'rb') # open whit byte # print "time on open: %s" % (time.time() - start_time1) # print "total time: %s" % (time.time() - start_time) image_buffer = base64.b64encode(imgfile.read()) # read and to base64 code finally: imgfile.close() send_data(image_buffer) # socket发送image buffer except: # quit() pass appuifw.app.title = u"Camera Monitor" appuifw.app.exit_key_handler = quit # 将"退出"键触发事件绑到def quit()上 appuifw.app.body = canvas = appuifw.Canvas() # 初始化一个画布 read_accesspoint() # 设置网络接入点 camera.start_finder(v_finder, size=camera.image_sizes()[-1]) app_lock=e32.Ao_lock() app_lock.wait()