:: Research Group Embedded Interaction :: Media Informatics :: Ludwig-Maximilians-University Munich


Picture transmission over HTTP

This 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

  • Nokia 6600 (Software version 4.09.1), MIDP 2.0 / CLDC 1.0
  • Acer Bluetooth USB Dongle (BT-600, Price ca. 25-30 Euro)

Used technologies

  • Mobile Phone: We implemented a program to access the integrated camera of a mobile phone. This provides the possibility to take pictures in different kinds of resolutions and different kinds of encoding formats. The pictures can be stored as Record Stores and can be afterwards shown on the display. For further information read the article about Image evaluation on mobile phones.
  • Connection: We used the connetion Internet over Bluetooth to get connected with the internet. For further information read the mentioned article. We used this connection to transfer data over HTTP to a server and store the data into an image file.
  • Server: We used the Tomcat server which is a servlet container for Java Servlets and JavaServer Pages. A servlet can be seen as an applet that runs on the server side. Such a servlet also provides a possibility for data transfers in our chase for receiving and storing image data.
  • Encoding algorithm: As already mentioned, during the first tests some data have been lost. Therefore we decided to encode the image data before the transmission. For the Base64 encoding and decoding we used an implementation which was developed by Stefan Haustein.
    The algorithm encodes a byte array into a String. Base64 is a data encoding scheme whereby binary-encoded data is converted to printable ASCII characters. It is defined as a MIME content transfer encoding for use in internet e-mail. The only characters used are the upper- and lower-case Roman alphabet characters (A-Z, a-z), the numerals (0-9), and the "+" and "/" symbols, with the "=" symbol as a special suffix code.

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);

// Modifying the headers of the request
hc.setRequestMethod(HttpConnection.POST);

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");
f.write(imageByteArray); f.close();

References



If you have any comments or questions, please feel free to ask us!