Wednesday, April 8, 2015

Android Meets Python And they have a picture together!

I recently wrote a simple script to take a picture from an android phone through Python in SL4A and send it over the network through socket programming to a client which saves it to file. Below is the code for the client on android! Please be sure to change your IPs.  A couple of other things, SL4A Python is not working in Android 5.0+. I tested this code successfully on Android 4.4 camera phone as the client and my PC running Windows 7 as the server. If you need more information you can check this out: http://faculty.pieas.edu.pk/fayyaz/ippy/html_demos/android.html

Android Client
Exported from Notepad++
# Client code (for android!) import sys import os import android import socket droid = android.Android() fpath = '/sdcard/foo.jpg' droid.cameraInteractiveCapturePicture(fpath) s = socket.socket() s.connect(('192.168.233.1',9999)) droid.makeToast('Connected!') if os.path.isfile(fpath): str = "file exists...!" else: str = "nope...!" droid.makeToast(str) with open (fpath, "rb") as f: l = f.read(1024) while (l): s.send(l) l = f.read(1024) s.close() droid.makeToast('Success!')







Exported from Notepad++
#Server code import socket import sys s = socket.socket() s.bind(('192.168.233.1',9999)) s.listen(10) sc, address = s.accept() print address l = sc.recv(1024) with open ("pic2.jpg",'wb') as f: while (l): f.write(l) l = sc.recv(1024) sc.close() s.close()

No comments:

Post a Comment