×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Super Contributor
    Join Date
    May 2006
    Location
    Oulu, Finland
    Posts
    622

    How to encode a picture data buffer to Base64?

    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

  2. #2
    Super Contributor
    Join Date
    Dec 2004
    Posts
    643

    Re: How to encode a picture data buffer to Base64?

    Yes, it's a bit silly that you can't encode Image objects directly to strings. That functionality should arrive in the near future. In the mean time, you can use the RAM disk (D drive) for the temporary files. It's significantly faster than writing to Flash.

  3. #3
    Super Contributor
    Join Date
    May 2006
    Location
    Oulu, Finland
    Posts
    622

    Re: How to encode a picture data buffer to Base64?

    Quote Originally Posted by jplauril
    Yes, it's a bit silly that you can't encode Image objects directly to strings. That functionality should arrive in the near future. In the mean time, you can use the RAM disk (D drive) for the temporary files. It's significantly faster than writing to Flash.
    Thanks a lot for helping.

    Well, it's really too bad to not have this functionality. I just hope it won't take too much time to come. Do you have any ideas about some scheduling?

    Also, I tried to use the RAM disk as you mentioned for encoding from picture to another file ( base64.encode(input, output) ) and then read the output file. I get it done within 1 second and something for a 160x120 picture with a NOKIA 6600. So it's acceptable for now.

    B.R

    LFD

  4. #4
    Regular Contributor
    Join Date
    Aug 2004
    Posts
    295

    Re: How to encode a picture data buffer to Base64?

    Maybe you could use StringIO module in python to emulate file?

  5. #5
    Super Contributor
    Join Date
    Dec 2004
    Posts
    643

    Re: How to encode a picture data buffer to Base64?

    Quote Originally Posted by simo.salminen
    Maybe you could use StringIO module in python to emulate file?
    Doesn't work for Images since the saving is done by native code. It should work for base64 functions.

Similar Threads

  1. suckho, i have encontered the same problems. Could you tell me how you resolved it?
    By dicson_hu in forum Symbian Networking & Messaging (Closed)
    Replies: 4
    Last Post: 2005-12-09, 10:56
  2. GPRS connection problems on 6600..
    By suckho in forum Symbian Networking & Messaging (Closed)
    Replies: 10
    Last Post: 2005-12-02, 14:00
  3. Nokia 6600 Socket Server send delay Problem
    By dicson_hu in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2005-10-31, 07:37
  4. Replies: 6
    Last Post: 2005-10-03, 05:51

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×