Showing posts with label thesis. Show all posts
Showing posts with label thesis. Show all posts

Tuesday, April 24, 2007

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 '<'

Tuesday, March 06, 2007

my j2me camera program snapshots #3

my j2me camera program snapshots #2

my j2me camera program snapshots #1

Code:How to write an image in mmapi

How to write an image in mmapi?

I installed and executed this code in my old fashion nokia 6600.
String name;
int recId; // returned by addRecord but not used
TextField tmp = (TextField)get_saveForm().get(0);
name = tmp.getString();
int width = image.getWidth();
int height = image.getHeight();
byte [] rgbData = pngData;//
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(baos);
try{
imageStore = RecordStore.openRecordStore("myimages",true);//image store name
outputStream.writeUTF(name);
outputStream.writeInt(width);
outputStream.writeInt(height);
outputStream.writeInt(rgbData.length);
for(int i=0;i TO rgbData.length;i++)
outputStream.writeByte(rgbData[i]);//write byte and forget out of memory error!

outputStream.flush();
outputStream.close();
byte[] b = baos.toByteArray();
recId = imageStore.addRecord(b, 0, b.length);
// Alert alert = new Alert("ImageSaved" ,name +" Saved",null,AlertType.INFO);
// Display.getDisplay(midlet).setCurrent(alert,this);
Display.getDisplay(midlet).setCurrent(this);
// System.out.println("Image Saved");
}catch(IOException ioe){
Alert alert = new Alert("Image" ,name +"IOException",null,AlertType.ERROR);
Display.getDisplay(midlet).setCurrent(alert,this);
}
catch (Exception e) {
// Log the exception
Alert alert = new Alert("Image" ,name +" Not Saved",null,AlertType.ERROR);
Display.getDisplay(midlet).setCurrent(alert,this);
} finally {
try {
// Close the Record Store
if (imageStore != null) imageStore.closeRecordStore();
} catch (Exception ignore) {
// Ignore
Alert alert = new Alert("Exception" ,ignore.getMessage(),null,AlertType.ERROR);
Display.getDisplay(midlet).setCurrent(alert,this);
}
}

}

TO will be '<' i cant write because of HTML error of silly blogspot editor

getsnapshot(null) problem in nokia6600

In java spec it write that when you give imagetype null your mobile phone take snapshot in default imagetype, but forget it!!! when simply you copy paste code and store it via RMS and later try to retrieve it you ll get an exception!.Where did it come? In Netbeans IDE 5.5 you dont get an exception but when you decide to install and execute your .jar program it simply throw an exception even simply wrote a meaningful expression abou what exception is. Try - catch blocks doesnt really do not anything in my nokia66oo and you can't see anything about problem. You will get an exception which see Exception 0 (oh really??)If you have a problem like this i suggest you give up studying and simply give more attention to your girlfriend :)) When you get ready (your mind is free) look at your device specification then you will get the answer.If you don't get anything,write a better code and give up cursing

Monday, March 05, 2007

getSnapshot(java.lang.String imageType) in mmapi

getSnapshot(java.lang.String imageType) returns a byte array which is encoded with given imageType.If imageType is null then default image type will be used.

How to learn supported image types?

System.out.println(System.getProperty("video.snapshot.encodings"));

Getting a jpeg image

byte[] imageData = videoControl.getSnapshot("encoding=jpeg"); //160x120
byte[] imageData = videoControl.getSnapshot("encoding=jpeg width="200" height="150");

Getting a png image

byte[] imageData = videoControl.getSnapshot("encoding=png"); //160x120

image types

http://en.wikipedia.org/wiki/Png

A good comparison of snapshot speed of the Nokia 6600 in different image types



Sunday, March 04, 2007

Code example for RMS in j2me

An example code shows how to store and load png images

Wednesday, February 28, 2007

Code : how to record data in j2me

private RecordStore recordStore=null;
byte [] data=.....
try{
int id = recordStore.addRecord(data,0,data.length);

}catch(RecordStoreException rse){

}
where data will be recorded?

Example:
In Nokia6600, E:\System\Midlets\[1012...]\rms.db

E : Disk (Memory Card)
[1012...] : midlet number

You can explore your device with FExplorer and see that when u add a new record size of rms.db will increase.

Tuesday, February 27, 2007

Code : how to reach camera in j2me

import javax.microedition.media.*;
import javax.microedition.media.control.*;

player=Manager.createPlayer("capture://video");
player.realize();
videoControl=(VideoControl)(player.getControl("VideoControl"));

videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO,
canvas);

player.start();

byte[] image = videoControl.getSnapshot(null);

Monday, February 26, 2007

how to store data and retrieve

Here is an overview about javax.microedition.rms package which is used for storing data and later retrieving it.

Thursday, February 22, 2007

Installing and Executing j2me applications in real mobile phones

Every mobile phone dont support every API and Configuration!

Remembering this basic rule rescue you from errors like "File Manager Closed" "Wrong version".

You can see which device supports which API and configuration in J2mepolish website.

For example if you(like me) try to install a application which has CLDC 1.1 version it may not be installed in Nokia 6600 which supports CLDC 1.0.

How to install??

open file manager -->select jar file-->and installing...

Wednesday, February 21, 2007

next step

The codes below simply send data that user types to server and receive message from server which is echos of it.Next step doing a file browser, browsing a photo and sending it via HttpConnection.If you send byte you can send everything so i ll send image in this way.

Example Code:HttpConnection in J2ME #2

use SendDataViaHttp.java and HttpConnector.java together
/**
*
* @author sezera.blogspot.com
* @version 0010
*/
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class HttpConnector extends Thread{

private String url="http://localhost:8084/YourWebApp/YourServlet";
private String msg;
private SendDataViaHttp midlet;
/** Creates a new instance of HttpConnector */
public HttpConnector(SendDataViaHttp 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();

int contentLength = (int)c.getLength();
if (contentLength == -1) contentLength = 255;
byte[] raw = new byte[contentLength];
int length = is.read(raw);
String response = new String(raw,0,length);
midlet.get_response(response);
os.close();
is.close();
c.close();
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
}

Example Code:HttpConnection in J2ME #1

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;

/**
*
* @author sezera.blogspot.com
* @version 0001
*/
public class SendDataViaHttp extends MIDlet implements CommandListener{

private Form sendForm;
public TextField sendTextField;
public TextField responseTextField;
private TextBox askUserTextBox;
private Command sendCommand;
private Command exitCommand;
private String msgString;
private String responseString;

public SendDataViaHttp(){
}

public void startApp() {
getDisplay().setCurrent(get_sendForm());
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}
public Display getDisplay()
{
return Display.getDisplay(this);
}
public Form get_sendForm()
{
if(sendForm == null){
sendForm = new Form(null,new Item[]{get_sendTextField(),get_responseTextField()});
sendForm.addCommand(get_sendCommand());
sendForm.addCommand(get_exitCommand());
sendForm.setCommandListener(this);
}
return sendForm;
}
public TextField get_sendTextField(){
if(sendTextField == null){
sendTextField = new TextField("Your Message",null,20,TextField.ANY);
}
return sendTextField;
}
public TextField get_responseTextField(){
if(responseTextField == null){
responseTextField = new TextField("Response From Server",null,20,TextField.ANY);
}
return responseTextField;
}
public Command get_sendCommand()
{
if(sendCommand == null){
sendCommand = new Command("Send",Command.OK,1);
}
return sendCommand;
}
public Command get_exitCommand()
{
if(exitCommand == null){
exitCommand = new Command("Exit",Command.EXIT,2);
}
return exitCommand;
}
public void commandAction(Command command,Displayable displayable){
if(displayable == sendForm){
if(command == sendCommand){
//send written data to JavaServlet via HttpConnection
msgString = new String(sendTextField.getString());
HttpConnector conn = new HttpConnector(this,msgString);
conn.start();
}else if(command == exitCommand){
//exit Midlet
exitMiddlet();

}
}else
{
getDisplay().setCurrent(get_sendForm());
}
}
public void exitMiddlet()
{
getDisplay().setCurrent(null);
destroyApp(true);
notifyDestroyed();
}
public void get_response(String response)
{
responseTextField.setString(response);
}
}

Deadlock problem in J2ME networking

Warning: To avoid potential deadlock, operations that may block, such as
networking, should be performed in a different thread than the
commandAction() handler.
The warning above says if you simply do a httpconnection in J2Me memory,you may cause a deadlock.So we should Java Threads to connect server and send/receive data.