www.migniot.com

Code blog

SyncAdapter android

Nov 8, 2011 5:28:47 PM

Application : todolist android. L'ensemble des copies d'écran en grand format est visible à http://www.migniot.com/Photos.html?album=Droid

/photos/Droid/mini/screen01.PNG

L'application liste les listes de taches, comme sur

/photos/Droid/mini/screen02.PNG

Pour cela on crée un custom Authenticator comme indiqué à http://developer.android.com/resources/samples/SampleSyncAdapter/index.html

/photos/Droid/mini/screen03.PNG

Ce compte doit être déclaré Syncable

/photos/Droid/mini/screen04.PNG

Puis on écrit une liste de taches

/photos/Droid/mini/screen05.PNG

On crée la liste des taches dans Zope après avoir codé le produit correspondant avec ses compétences pythonesques

/photos/Droid/mini/screen06.PNG /photos/Droid/mini/screen07.PNG

On vérifie qu'elle a bien été importée dans zope

/photos/Droid/mini/screen09.PNG

Revenant à android, on lance écrit un SyncAdapter comme indiqué à http://developer.android.com/reference/android/content/AbstractThreadedSyncAdapter.html .

/photos/Droid/mini/screen10.png

Puis en ouvrant l'application android à nouveau, les listes sont apparues, avec leur contenu

/photos/Droid/mini/screen11.PNG /photos/Droid/mini/screen12.PNG

Et on peut exulter à ne pas avoir de compte Google mais un compte Custom

/photos/Droid/mini/screen13.PNG

Capture d'écran en PIL et wxPython

Apr 19, 2011 8:29:53 AM

La capture d'écran en python avec PIL est relativement triviale:

from PIL import ImageGrab
pilimage = ImageGrab.grab()

L'image peut être convertie au format Bitmap de wxPython:

from wx import EmptyImage, BitmapFromImage
wximage = EmptyImage(pilimage.size[0], pilimage.size[1])
wximage.SetData(pilimage.convert('RGB').tostring())
bitmap = BitmapFromImage(wximage)

Une application wxPython peut être construite autour de cette Bitmap pour en sélectionner une partie. L'application complète est disponible ci-dessous:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

from PIL import ImageGrab
from wx import App,Frame, EmptyImage, BitmapFromImage, NO_BORDER
from wx import StaticBitmap, PaintDC, EVT_PAINT, EVT_LEFT_DOWN
from wx import EVT_LEFT_UP, EVT_MOTION, Pen, Colour, SOLID, Brush
from wx import TRANSPARENT, TheClipboard, BitmapDataObject, Rect

class GrabbedFrame(Frame):
    def __init__(self, title, image):
        Frame.__init__(self, None, title=title,
            size=image.size, style=NO_BORDER)
        wximage = EmptyImage(image.size[0], image.size[1])
        wximage.SetData(image.convert('RGB').tostring())
        self.bitmap = BitmapFromImage(wximage)
        self.down,self.start,self.end = False,None, None
        EVT_PAINT(self, self.OnPaint)
        EVT_LEFT_DOWN(self, self.OnMouse)
        EVT_LEFT_UP(self, self.OnMouse)
        EVT_MOTION(self, self.OnMouse)
    def OnPaint(self, event):
        dc = PaintDC(self)
        dc.DrawBitmap(self.bitmap, 0,0)
        if self.start and self.end:
            x = min(self.start[0], self.end[0])
            y = min(self.start[1], self.end[1])
            w = abs(self.end[0]-self.start[0])
            h = abs(self.end[1]-self.start[1])
            dc.SetBrush(Brush(Colour(255,255,255),TRANSPARENT))
            dc.SetPen(Pen(Colour(200,200,200),1,SOLID))
            dc.BeginDrawing()
            dc.DrawRectangle(x,y,w,h)
            dc.EndDrawing()
    def OnMouse(self, event):
        if event.LeftDown():
            self.down = True
            self.start = event.GetPosition()
        if event.LeftUp():
            self.down = False
            self.end = event.GetPosition()
            self.Refresh(False)
            self.capture()
        if self.down and event.Dragging():
            self.end = event.GetPosition()
            self.Refresh(False)
    def capture(self):
        x = min(self.start[0], self.end[0])
        y = min(self.start[1], self.end[1])
        w = abs(self.end[0]-self.start[0])
        h = abs(self.end[1]-self.start[1])
        data = BitmapDataObject()
        data.SetBitmap(self.bitmap.GetSubBitmap(
            Rect(x,y,w,h)))
        if TheClipboard.Open():
            TheClipboard.SetData(data)
            TheClipboard.Flush()
            TheClipboard.Close()
        self.Close()

def grab():
    pilimage = ImageGrab.grab()
    frame = GrabbedFrame('Image grabbed', pilimage)
    frame.Show()

if __name__ == '__main__':
    app = App()
    grab()
    app.MainLoop()
Temps de saison (nuit) -1°