Capturing Video with J2ME

Illustration below takes pictures on a J2ME device.

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

public class VideoMIDlet extends MIDlet implements CommandListener {

private Display display;
private Form form;
private Command exit,back,capture,camera;
private Player player;
private VideoControl videoControl;
private Video video;

public VideoMIDlet() {

exit = new Command("Exit", Command.EXIT, 0);
camera = new Command("Camera", Command.SCREEN, 0);
back = new Command("Back", Command.BACK, 0);
capture = new Command("Capture", Command.SCREEN, 0);

form = new Form("Capture Video");
form.addCommand(camera);
form.setCommandListener(this);
}

public void startApp() {
display = Display.getDisplay(this);
display.setCurrent(form);
}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {}

public void commandAction(Command c, Displayable s) {
if (c == exit) {
destroyApp(true);
notifyDestroyed();
} else if (c == camera) {
showCamera();
} else if (c == back)
display.setCurrent(form);
else if (c == capture) {
video = new Video(this);
video.start();
}
}

public void showCamera() {
try {
player = Manager.createPlayer("capture://video");
player.realize();

videoControl = (VideoControl)player.getControl("VideoControl");
Canvas canvas = new VideoCanvas(this, videoControl);
canvas.addCommand(back);
canvas.addCommand(capture);
canvas.setCommandListener(this);
display.setCurrent(canvas);
player.start();
} catch (IOException ioe) {} catch (MediaException me) {}
}

class Video extends Thread {
videoMIDlet midlet;
public Video(VideoMIDlet midlet) {
this.midlet = midlet;
}

public void run() {
captureVideo();

}

public void captureVideo() {
try {
byte[] raw = videoControl.getSnapshot(null);
Image image = Image.createImage(raw, 0, raw.length);
form.append(image);
display.setCurrent(form);

player.close();
player = null;
videoControl = null;
} catch (MediaException me) { }
}
};
}

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

public class VideoCanvas extends Canvas {
private VideoMIDlet midlet;

public VideoCanvas(VideoMIDlet midlet, VideoControl videoControl) {
int width = getWidth();
int height = getHeight();
this.midlet = 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);
}

public 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);
}

14 comments:

  1. can u explain how to run this application with mobile,i m new to j2me.i create a program ,but how to run with mobile .and i dont know where is captured image is stored. can u help me

    ReplyDelete
  2. hi how can i play the video in cdc application,
    i want a advance player with play, stop,next ,prv,...button in my mediaplayer in cdc application ,and how i can combine a cdc application with midp application and how i can search the text in the lable via a text box search in cdc application and how will display a advance photo albom in cdc application

    ReplyDelete
  3. coding for streaming a video in a mobile .............if any one having please forward

    ReplyDelete
  4. When i compile this application an error occurs. it says:
    "Non-identical definition of Iclass
    Ljavax/microedition/lcdui/command"

    How do i fix this problem?

    ReplyDelete
  5. i had written the code to capture image in mobile phone but it is not running on my nokia 6303. if any one knows please send me your help at maheshwari1986ashish@gmail.com

    ReplyDelete
  6. Hey is there a source code for a calendar implementing the reminders,appointment handling etc.? Could u pls help me with that?

    ReplyDelete
  7. this is for image capturing I need to capture a video....hw s it possible??? will u pls suggest...

    ReplyDelete
  8. hi...i am developing an application and i need to capture image using camera in mobile phone and have to process it... if i use the the above code, camera in mobile is invoked but its not capturing... please help me

    ReplyDelete
  9. thanx man....woking code

    ReplyDelete
  10. this code isnt working, it invokes capture but it doesnt capture anything.

    ReplyDelete
  11. i'm getting error as "internal application error:java.lang.security Exception"
    if i use the abouve code as package in my application please do help struct wit it

    ReplyDelete
  12. alow the secutirys in the app and runthat

    ReplyDelete
  13. bud dig dig bud dig

    ReplyDelete

 

Design by Blogger Buster | Distributed by Blogging Tips