Tuesday, April 24, 2007

About codes

All codes are written and tested by me.

how to send image from mobile phone to servlet :Server Side

BufferedReader reader = request.getReader();
ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
int width=160;
int height=120;
int [] pixels = new int[width*height];
int ch;
BufferedImage img=
new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);

FileWriter fw1;
BufferedWriter bw1=null;
try{
File myfile = new File("C:\\picture.jpg");

fw1 = new FileWriter("C:\\imagedata.txt");
bw1 = new BufferedWriter(fw1);
for(int i =0;i LT 120;i++){
for(int j=0;j LT 160;j++){
reader.skip(1);
ch=reader.read();
while ((ch = reader.read()) != '?')
bStrm.write(ch);
String str = new String(bStrm.toByteArray());
pixels[i*160+j] = Integer.parseInt(str);
bw1.write(""+pixels[i*160+j]);
bStrm.reset();

}
bw1.newLine();
}
img.setRGB(0,0,160,120,pixels,0,160);
ImageIO.write(img,"jpg",myfile);
}catch(Exception e){

}
bw1.close();

how to send image from mobile phone to servlet :Mobile Side

public synchronized void run(){
HttpConnection c = 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();
Image myimage = Image.createImage(imageData,0,imageData.length);
int w = myimage.getWidth();
int h = myimage.getHeight();
DataOutputStream out = new DataOutputStream(os);
System.out.println("width:"+myimage.getWidth());
System.out.println("height:"+myimage.getHeight());
int [] rgbdata = new int[160*120];
System.out.println("length :"+rgbdata.length);
myimage.getRGB(rgbdata, 0, myimage.getWidth(), 0, 0, myimage.getWidth(), myimage.getHeight());

for(int i = 0 ;i LT h;i++){
for(int j=0;j LT w;j++){
out.writeUTF(rgbdata[i*w+j]+"?");
}
}
}
catch(Exception e){
}
}

Friday, April 06, 2007

Image Sending Test


Codes in the last 2 post is tested in netbeans Ide 5.5 and passed successfully.It can transfer 225kb bmp image that you see left successfully.

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

how to send image from servlet to mobile phone

Server side
Read image
send width
send height
while(MorePixels){
Take a pixel from getRGB(x,y)
Put a split character
send it
}
here is the JAVA code:


/*
* BTServlet.java
*
* Created on February 12, 2007, 9:45 PM
*/

package servlet;

import java.io.*;
import java.net.*;
import java.awt.image.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.imageio.ImageIO;

/**
*
* @author sezer
* @version
*/
public class BTServlet extends HttpServlet {

/** Processes requests for both HTTP GET and POST methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
BufferedReader reader = request.getReader();
String msg = reader.readLine();
response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

BufferedImage bufImg=null;
try{
File f = new File("C:\\picture.png");
bufImg = ImageIO.read(f);
}
catch(IOException e){
e.printStackTrace();
}


int w = bufImg.getWidth();
int h = bufImg.getHeight();


System.out.println("width:"+w);
System.out.println("height:"+h);




out.print(w+"@"+h+"n");


for(int i = 0 ;i LT h;i++){
for(int j=0;j LT w;j++){
out.print(bufImg.getRGB(j,i)+"?");
}

}

out.flush();
out.close();
}
}
LT : is '<'