Friday, April 06, 2007

how to send image from servlet to mobile phone

Mobile side:

Do reverse what you did in server side
Create image with Image.createImage(....)
*GetImage :starting midlet
Here is the Java Code

/*
* HttpConnector2.java
*
* Created on February 20, 2007, 6:20 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package hello;

/**
*
* @author sezer
*/
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;


public class HttpConnector2 extends Thread{

private String url="your url";
private String msg;
private GetImage midlet;



/** Creates a new instance of HttpConnector */
public HttpConnector2(GetImage midlet,String msg) {
this.midlet =midlet;
this.msg = msg;
}
public synchronized void run(){
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
try{
c = (HttpConnection)Connector.open(url);
// Set the request method and headers
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("User-Agent",
"Profile/MIDP-2.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-US");
os = c.openOutputStream();
os.write(msg.getBytes());

is = c.openInputStream();

ByteArrayOutputStream bStrm = new ByteArrayOutputStream();

int ch;

while((ch=is.read())!='@')
bStrm.write(ch);
String widthstr = new String(bStrm.toByteArray());
System.out.println("Width:"+widthstr);

bStrm.reset();

int width = Integer.parseInt(widthstr);
System.out.println(width);

while((ch=is.read())!='n')
bStrm.write(ch);
String heightstr = new String(bStrm.toByteArray());
System.out.println("Height:"+heightstr);

bStrm.reset();

int height = Integer.parseInt(heightstr);
System.out.println(height);

int [] pixels = new int[width*height];

for(int i =0;i LT height;i++){
for(int j=0;j LT width;j++){
while ((ch = is.read()) != '?')
bStrm.write(ch);


String str = new String(bStrm.toByteArray());
pixels[i*width+j] = Integer.parseInt(str);
bStrm.reset();
}

}
System.out.println(pixels.length);

midlet.get_response(width,height,pixels);
os.close();
is.close();
c.close();
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
}
LT is '<' as usual

No comments: