Hi,
I have to develop one script that has to:
- take a picture
- get the picture data buffer bytes
- encode the data to base64
- finally upload it to a server
The server side receives that data buffer, creates and saves the picture as png. The point for that is to not save the picture on the phone at all; the server takes care of it.
The uploading part is not a problem I have some ready code. Taking the picture is not a problem. I have done the following script which only waits to be implemented with the base64 encoding (the _encode() method):
Code:#! /usr/bin/env python ######################################## # file: MyCamMod.py # author: LEFEVRE Damien # date: 28 Aug 2006 # time: 15:47:47 # version: 0.1 ######################################## # script imports import e32, appuifw import camera from graphics import Image from key_codes import EScancodeSelect import thread import time import base64 # module for encoding # keyboard events class 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 #little problem with code tag :) self._keyboard_state\[code\]=1 # same here 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 # camera class class MyCamMod(object): """ camera module able to get base64 encoded image data buffer """ def __init__(self, settings = None, camset = None, menu = None): """ class constructor """ self.script_lock = e32.Ao_lock() # class variables self._run = 0 self._screen_picture = None self._img = Image.new((176,208)) self._data= None # keyboard instance self._keyboard = Keyboard() # canvas instance for displaying pictures self._canvas = appuifw.Canvas(event_callback=self._keyboard.handle_event, redraw_callback=self._handle_redraw) # app elements appuifw.app.title = u"My Camera Module" appuifw.app.body = self._canvas appuifw.app.screen = "normal" appuifw.app.menu = [(u"Start", self.start), (u"Exit", self.__del__)] appuifw.app.exit_key_handler = self.__del__ self.script_lock.wait() def start(self, menu=None): """ start camera viewer """ self._run = 1 if menu == None: appuifw.app.menu = [(u"Capture", self.capture), (u"Stop", self.stop), (u"Exit", self.__del__)] else: appuifw.app.menu = menu thread.start_new_thread(self._key_listener,()) while self._run == 1: self._screen_picture = camera.take_photo(size = (160,120)) self._img.blit(self._screen_picture,target=(8,10,168,130),scale=1) self._handle_redraw(()) e32.ao_yield() def stop(self, menu=None): """ stop the camera viewer """ self._run = 0 if menu == None: appuifw.app.menu = [(u"Start", self.start), (u"Exit", self.__del__)] else: appuifw.app.menu = menu def capture(self): """ capture one img """ self.stop() self._encode() def _key_listener(self): """ capture img if Select key is pressed """ while self._run == 1: if self._keyboard.pressed(EScancodeSelect): self.capture() def _handle_redraw(self, rect): """ redraw handler for the canvas """ if self._img != None: self._canvas.blit(self._img) def _encode(self, *args): """ encode picture image buffer to base 64 for upload """ #TODO: encode image data buffer pass def get_data(self): """ return base64 encoded data as a string """ return self._data def __del__(self): """ class destructor """ self.stop() time.sleep(0.2) self.script_lock.signal() if __name__ == '__main__' : MyCamMod()
My problem is that the Python base64 module seems to work only with strings or files. Working with files would mean to save the picture to a new file, close it, re-open it for reading, encode every lines, create a second file for writing, write the encoded lines to it, close both files, re-open the second file, read it then send the data to the server… Too much processing time and anyway the server accepts only picture data buffers.
So I’m stuck here!
Does anybody have a clue on how to do that?
B.R,
LFD

Reply With Quote

