|
|
|
![]() :: Research Group Embedded Interaction :: Media Informatics :: Ludwig-Maximilians-University Munich Picture transmission over HTTPThis presented Java source code allows the capturing of an image on a mobile phone and the transfer of this image form the mobile phone over a HTTP connection to the server. As some bytes were lost by transfering in our first implementation, we decided to encode the image data by the Base64 algorithm for the transmission. Tested with this hardware
Used technologies
Source code All souce codes can be found at HTTPConnection.zip. Code on the mobile phone (client): After a picture has been taken, this date should be transfered to a server. At the first step the byte[] of the image data is encoded by the Base64 algorthim. The return value is a String. String imageString = Base64.encode(imageByteArray); Now the URL and the path to the servlet have to be defined. Afterwards a connection to the given url is opened. Moreover the http Connection object need to know which method (e.g. POST) should be used on the server side. String url = new String("http://XXX.XXX.XXX.XXX:XXXX/..../PostServlet");
HttpConnection hc = (HttpConnection)Connector.open(url);
Now the connection is ready to send data. Therefore a Outputream can be set and the data can be written into the Outputstream. Finally the Outputstream and the Connection are closed. Outputstream out = hc.openOutputStream(); out.write(imageString.getBytes()); if (out != null) out.close(); if (hc !=null) hc.close(); Code on the server side: The following code is part of the doPost method of the servlet. At first a Inputstream is opened and a BufferedReader receives the data of the InputStream. Afterwards the BufferedReader reads out the received String. This String includes the encoded image data. InputStream in = request.getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuffer buf = new StringBuffer();
String line;
//Read the BufferedReader out and receives String data
while ((line = r.readLine())!=null) {
buf.append(line);
}
String imageString = buf.toString();
Now the String data can be decoded by the same Base64 algorithm. The input data is the encoded String. The return value is the decoded byte[] of the image. byte[] imageByteArray = Base64.decode(imageString); Now the original data of the image can be written into a file. We use the FileOutputStream. This stream writes a byte[] into a given filepath. FileOutputStream f = new FileOutputStream("C:/..../image.png");
ReferencesIf you have any comments or questions, please feel free to ask us! |