Miniware logo
Articles / Take a picture and upload it to a web server

Take a picture and upload it to a web server

This article presents how to create a midlet that takes a picture and then uploads it to a web server.It uses the mobile media api (JSR-135) and a php page.

Initially we need to create a php file that will handle the picture upload. The following code is what we need:

if($_FILES['picture']['name']!=""){
    $uploaddir = 'files/';
    $file = basename($_FILES['picture']['name']);
    $uploadfile = $uploaddir . $file;
    if(!move_uploaded_file($_FILES['picture']['tmp_name'], $uploadfile)){
        exit();
    }else{
          chmod($uploadfile,0755);
    }
}


Save this code as file named upload.php . Upload it to the web server and create a folder named files in the same directory as the upload.php. We also suppose that the file upload.php is located in the 10.0.0.1 ip address. The following code is the code of the midlet

import javax.microedition.midlet.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

public class MediaMain extends MIDlet implements CommandListener, Runnable {
    Display display;
    Alert alert;
    Form form;
    Command cmdCapture;
    Player player = null;
    VideoControl vc = null;
    public void startApp() {
        form = new Form("Mobile media");
        cmdCapture = new Command("Capture",Command.SCREEN,1);
        form.addCommand(cmdCapture);
        form.setCommandListener(this);
        display = Display.getDisplay(this);
        //Check if captured is supported
        if(System.getProperty("supports.video.capture").equalsIgnoreCase("true")){
            //Capture is supported
            try{
                player = Manager.createPlayer("capture://video");
                player.realize();
                vc = (VideoControl)player.getControl(
                           "VideoControl");
                if(vc != null) {
                   Item video = (Item)vc.initDisplayMode(
                                 vc.USE_GUI_PRIMITIVE, null);
                   form.append(video);
                   display.setCurrent(form);
                }
                player.start();
                vc.setVisible(true);
            }catch(Exception e){
                //handle exception
            }
            display.setCurrent(form);
         
            
        }else{
            //Capture is not supported
            alert = new Alert("Mobile Media");
            alert.setTimeout(Alert.FOREVER);
            alert.setString("Capture is not supported");
            display.setCurrent(alert);
        }
    }
    
    public void commandAction(Command com, Displayable dis) {
        if (com == cmdCapture){
            //Capture has been pressed
            try{
                Thread t = new Thread(this);
                t.start();
            }catch(Exception e){
                    //handle exception
            }
        }
    }
    
    public void run(){
        try{
            byte[] pngImage = vc.getSnapshot(null);
            String message1, message2;
            message1  = "--pic\r\n";
            message1 += "Content-Disposition: form-data; name=\"picture\"; filename=\"capture.png\"\r\n";
            message1 += "Content-Type: image/png\r\n\r\n";
            message2  = "\r\n--pic--\r\n";
            OutputStream os = null;
            HttpConnection conn = (HttpConnection)Connector.open("http://10.0.0.1/upload.php",Connector.READ_WRITE);
            conn.setRequestMethod(HttpConnection.POST);
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=pic");
            os = conn.openOutputStream( );
            os.write(message1.getBytes());
            os.write(pngImage);
            os.write(message2.getBytes());
            os.close( );
            os = null;
        }catch(Exception e){
            //handle exception
        }
    }
    
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
}


The content of this page can be reproduced as long as the author and the source are mentioned. For questions please use the forum. Nikos Fotiou
Comments
(Post new comment)

2007-10-10 14:00:24 Federico wrote:
I have a big problem. When i show the picture on the web, the size is 160px x 120px. how I can change the measurement? Thanks!