Greatly appreciate the advise, aaaaapo. The suggested setup has worked well.
As you might recall, the primary concern for this thread is json decoding jpeg. Following is a synopsis:
A photoclient runs on the phone which clicks a photo and sends it to server. The client code is as follows:
Code:
import json, socket, camera, appuifw
PHOTO_FILE = u"E:\\Images\\temp.jpg"
def send_photo(name, jpeg):
msg = {"jpeg": jpeg, "name": name}
sock = socket.socket(socket.AF_INET,\
socket.SOCK_STREAM)
sock.connect(("192.168.0.2", 9000))
out = sock.makefile("w")
out.write(json.write(msg))
out.close()
while True:
name = appuifw.query(u"Photo name", "text")
if not name:
break
print "Taking photo.."
img = camera.take_photo(size = (640, 480))
img.save(PHOTO_FILE)
jpeg = file(PHOTO_FILE).read()
print "Sending photo.."
send_photo(name, jpeg)
print "Photo sent ok"
print "Bye!"
A server runs waiting for the photo on a PC. Server code:
Code:
import SocketServer, json
class Server(SocketServer.TCPServer):
allow_reuse_address = True
class Handler(SocketServer.StreamRequestHandler):
def handle(self):
msg = json.read(self.rfile.read())
fname = msg["name"] + ".jpg"
f = file(fname, "w")
f.write(msg["jpeg"])
f.close()
print "Received photo %s (%d bytes)" %\
(fname, len(msg["jpeg"]))
server = Server(('', 9000), Handler)
print "WAITING FOR NEW CONNECTIONS.."
server.serve_forever()
The photo is indeed snapped on the phone (I hear the internals clickin'), and sent correctly and received too by the server. The jpg appears in the same directory as the server script. It is typically 30-40Kb.
However, on opening the image with standard windows picture & fax viewer, a "no preview available" error is generated. Its as if the picture is empty.
Following troubleshooting measures were taken:
- I attempted to open the picture with Photoshop but the error persists
- I attempted running the server using windows cmd line as well as python cmd line with server.py & json.py placed in C:\Program Files\Python26. But the error persists.
Would appreciate advise on addressing the above.
Best regards,
wirefree