PAGES

Thursday, December 23, 2010

Creating a Camera Appliction in J2ME

In this tutorial you will learn to create a simple camera program.For this we will use a canvas class named "VideoCanavas" and call it in our main MIDlet "CameraMidlet".
Here we first make the camera view visible by camera button and then add the capture button to take a snapshot.Also we create a class "Video" inside "CameraMidlet" to capture the picture.
For making camera view visible we have to add a player.In the code the following statement is used for S60 devices to add player :-
player = Manager.createPlayer("capture://video");

For S40 devices replace this line with  :-

player = Manager.createPlayer("capture://image");

Also in this tutorial we have no provision for saving the clicked photo.So everytime you close application  ,image goes off.
For the memory part ,we will use Recordstore ,which we will study in next tutorial and then finally we will join the 2 to make the complete camera program. 

NOTE:- The camera view in S.D.K is not proper as it is just an emulation.Also it shows java.lang.outofmemory exception. This is due to lack of memory in S.D.K's .So better try it on mobile.

To View tutorial video Online CLICK HERE
(Download and watch for better quality)
To download Click here

 SOURCE CODE:-

CameraMidlet.java:-

import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.VideoControl;
import javax.microedition.midlet.*;

public class CameraMidlet extends MIDlet implements CommandListener {
     private Form form;
     private Command exit,camera,capture;
     private Player player;
     private VideoControl videoControl;
     public byte[] raw;
     private Video vid;

     public CameraMidlet()
     {
         form = new Form("Capture Video");
         exit = new Command("Exit", Command.EXIT, 0);
         camera = new Command("Camera", Command.SCREEN, 0);
         form.addCommand(camera);
         form.addCommand(exit);
         form.setCommandListener(this);
         capture = new Command("Capture", Command.SCREEN, 0);
     }
    public void startApp() {
        Display.getDisplay(this).setCurrent(form);
    }
    public void pauseApp() {
    }
    public void destroyApp(boolean unconditional) {
    }
    public void commandAction(Command c, Displayable d) {
         if (c == exit) {
            destroyApp(true);
            notifyDestroyed();
        }
         if (c == camera) {
            showCamera();
        }
         if (c == capture) {
                  try{
            vid = new Video(this);
            vid.start();
           }catch(Exception e)
           {}
         }
    }
    public void showCamera() {
        try {
            player = Manager.createPlayer("capture://video");
            player.realize();
            videoControl = (VideoControl)player.getControl("VideoControl");
            Canvas canvas = new VideoCanvas(this, videoControl);
            canvas.addCommand(capture);
            canvas.setCommandListener(this);
            Display.getDisplay(this).setCurrent(canvas);
            player.start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    class Video extends Thread {
    CameraMidlet midlet;
    public Video(CameraMidlet midlet) {
            this.midlet = midlet;
        }
    public void run() {
            captureVideo();
        }
     public void captureVideo()
     {
            try {
                raw = videoControl.getSnapshot(null);
                Image  image = Image.createImage(raw, 0, raw.length);
                player.close();
                player = null;
               videoControl = null;
            } catch (MediaException ex) {
                ex.printStackTrace();
            }
     }
    }
}


VideoCanvas.java:-

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

public class VideoCanvas extends Canvas {
    private CameraMidlet parent;
    public VideoCanvas(CameraMidlet midlet,VideoControl videoControl) {
        int width = getWidth();
        int height = getHeight();
        this.parent = midlet;
         videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);
        try {
            videoControl.setDisplayLocation(2, 2);
            videoControl.setDisplaySize(width - 4, height - 4);
        } catch (MediaException me) {}
        videoControl.setVisible(true);
    }
    protected void paint(Graphics g) {
         int width = getWidth();
        int height = getHeight();
        g.setColor(0x00ff00);
        g.drawRect(0, 0, width - 1, height - 1);
        g.drawRect(1, 1, width - 3, height - 3);
    }
}

1 comment:

  1. hey bro,
    i must say that ur guidance is really helpful. but ur blog is too much difficult to find on the search engines. accidentaly i found ur blog nd now i m a fan of it.

    keep posting...
    god bless.

    ReplyDelete

customised by Vaibhav