Openimaj capture and save video from integrated camera or usb camera to hardisk (java) - openimaj

I just installed almost all libraries of openimaj instruction wise from the site: http://sourceforge.net/p/openimaj/wiki/OpenIMAJ%20From%20Source/ libraries are installed and working. I just need a sample code to capture video from webcam and save it to hard disk. For example:
This is the code to turn on the camera and show you video:
import org.openimaj.image.MBFImage;
import org.openimaj.video.VideoDisplay;
import org.openimaj.video.VideoDisplayListener;
import org.openimaj.video.capture.VideoCapture;
public class VideoDemo {
...
VideoCapture vc = new VideoCapture( 320, 240 );
VideoDisplay<MBFImage> vd = VideoDisplay.createVideoDisplay( vc );
...
}
How do I save [vd] on disk?

You need to use the XuggleVideoWriter class. The following code displays the video content on the screen and writes the content to a file until the escape key is pressed. The format of the video is controlled by the name of the file (i.e. "video.flv" creates an FLV format video).
package org.openimaj.demos.sandbox;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.swing.SwingUtilities;
import org.openimaj.image.MBFImage;
import org.openimaj.video.Video;
import org.openimaj.video.VideoDisplay;
import org.openimaj.video.VideoDisplayListener;
import org.openimaj.video.capture.VideoCapture;
import org.openimaj.video.xuggle.XuggleVideoWriter;
/**
* Record the webcam to a file.
*
* #author Jonathon Hare (jsh2#ecs.soton.ac.uk)
*/
public class VideoRecorder extends KeyAdapter implements VideoDisplayListener<MBFImage> {
private Video<MBFImage> video;
private VideoDisplay<MBFImage> display;
private XuggleVideoWriter writer;
private boolean close = false;
/**
* Default constructor
* #throws IOException
*/
public VideoRecorder() throws IOException {
//open webcam
video = new VideoCapture(320, 240);
//open display
display = VideoDisplay.createVideoDisplay(video);
//open a writer
writer = new XuggleVideoWriter("video.flv", video.getWidth(), video.getHeight(), 30);
//set this class to listen to video display events
display.addVideoListener(this);
//set this class to listen to keyboard events
SwingUtilities.getRoot(display.getScreen()).addKeyListener(this);
}
#Override
public void afterUpdate(VideoDisplay<MBFImage> display) {
//Do nothing
}
#Override
public void beforeUpdate(MBFImage frame) {
//write a frame
if (!close) {
writer.addFrame(frame);
}
}
#Override
public void keyPressed(KeyEvent key) {
//wait for the escape key to be pressed
close = key.getKeyCode() == KeyEvent.VK_ESCAPE;
}
/**
* Main method
* #param args ignored
* #throws IOException
*/
public static void main(String[] args) throws IOException {
new VideoRecorder();
}
}

Related

Connecting to Bluetooth when trying to get Current Geographic Location in J2ME

I had done A Simple Application to Record Current Geographic Location and Display it on Mobile Screen then its working fine in j2me emulator but when application installed in mobile(Nokia Asha 210) it installed and opening directly to connecting to bluetooth. It is opening the bluetooth settings where the problem i cant understand so i need help regarding this issue..
This is my Entire Code....
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.location.Location;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;
import javax.microedition.location.QualifiedCoordinates;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class LocationWithPolling extends MIDlet implements Runnable, CommandListener {
Form mainform;
Thread t;
LocationProvider lp;
Display display;
StringItem latitude;
StringItem longitude;
Command exitCommand=new Command("Exit",Command.OK,0);
protected void destroyApp(boolean arg0) {}
protected void pauseApp() {}
protected void startApp() throws MIDletStateChangeException
{
mainform=new Form("Location Polling");
latitude=new StringItem("Latitude:","N/A");
longitude=new StringItem("Longitude:","N/A");
Display.getDisplay(this).setCurrent(mainform);
mainform.append(latitude);
mainform.append(longitude);
mainform.addCommand(exitCommand);
mainform.setCommandListener(this);
t=new Thread(this);
t.start();
}
public void run()
{
try{
lp = LocationProvider.getInstance(null);
while(true)
{
Location loc=lp.getLocation(5000);
QualifiedCoordinates c=loc.getQualifiedCoordinates();
latitude.setText(String.valueOf(c.getLatitude()));
longitude.setText(String.valueOf(c.getLongitude()));
Thread.sleep(5000);
}
}catch(Exception e)
{
Alert alert =
new Alert("Error", "Could not retrieve location!", null, AlertType.ERROR);
display.setCurrent(alert);
}
}
public void providerStateChanged(LocationProvider provider, int newState) { }
public void commandAction(Command cmd, Displayable arg1)
{
if(cmd==exitCommand)
{
notifyDestroyed();
}
}
}
Asha phones do not have internal GPS, so it attempts to connect to a bluetooth GPS, and you apparently have none paired with the phone. That is why I think it opens BT settings. If you had one, it would connect to it.

How do I 'initialize' my graphics?

My friend has make me a simple JFrame, and I am meant to work on it and develop it. I have encountered a problem within the first 30 mins of having it: it's not drawing the graphics!!
Here is the code that draws the graphics, and the code from the class that brings up the JFrame.
Thanks in advance.
GameCanvas.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package danielballtest;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.LinkedList;
/**
*
* #author Kris
*/
public class GameCanvas extends Canvas{
Player p;
LinkedList<Stalker> s = new LinkedList<>();
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
p.paintItAll(g2d);
for(int i = 0; i < s.size(); i++) {
s.get(i).paintItAll(g2d);
}
}
public GameCanvas() {
initComponents();
repaint();
}
public void initComponents() {
p = new Player(new Point(50,50));
s.add(new Stalker(new Point(50,100)));
}
public Point getPlayerPos() {
return p.getPos();
}
}
MainWindow.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package danielballtest;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
*
* #author Kris
*/
public class MainWindow extends JFrame{
static Toolkit tk = Toolkit.getDefaultToolkit();
static int xSize = ((int) tk.getScreenSize().getWidth());
static int ySize = ((int) tk.getScreenSize().getHeight());
/**
* #param args the command line arguments
*/
public MainWindow() {
super("STALKER!!!!!!!!!!");
GameCanvas canvas = new GameCanvas();
add(canvas, BorderLayout.CENTER);
setSize(xSize, ySize);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
canvas.createBufferStrategy(2);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainWindow();
}
});
}
}
You are using the old AWT Canvas class as the base component. This class follows the AWT painting mechanism, where there is no paintComponent method and you were supposed to override paint to do custom graphics.
Change the base class to JComponent and you can use the newer painting mechanism:
...
import javax.swing.JComponent;
public class GameCanvas extends JComponent {
This article compares how painting works in AWT and Swing if you want to know more about them.

Dialling a phone number by platformrequest

I have tried for two days to write a code that dial a phone number , but I failed
I have written a midlet and a form called main as a Displayable the form contains a textfield and
command .
When the application starts up the form should appears and calls the dialed number written in
textField when the command pressed.
the dialing process didn't work with me .
import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Displayable.*;
import javax.microedition.lcdui.Form.*;
import javax.microedition.midlet.MIDlet;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class DiallNumber extends MIDlet implements CommandListener
{
String num;
private Form main;
private Command call,dial;
private TextField phoneField;
Display display;
public DiallNumber()
{
main = new Form("main");
phoneField = new TextField("label","",10,phoneField.PHONENUMBER);
call = new Command("call",call.OK,0);
main.append(phoneField);
main.addCommand(call);
num=this.phoneField.getString().trim();
main.setCommandListener(this);
}
/**}
* From MIDlet. Called when the MIDlet is started.
*/
public void startApp()
{
// The initial display is the first form
display = Display.getDisplay(this);
display.setCurrent(main);
}
public void call( String number) {
try {
platformRequest("tel:"+number);
} catch (ConnectionNotFoundException ex) {
// TODO: Exception handling
}
}
public void commandAction(Command c, Displayable d){
if(d==main)
{
if(c==call)
call(""+num);
}
}
public void pauseApp()
{
// No implementation required
}
/*
* /
*/
/**
* From MIDlet. Called to signal the MIDlet to terminate.
*
* #param unconditional
* whether the MIDlet has to be unconditionally terminated
*/
public void destroyApp(boolean unconditional)
{
// No implementation required
}
/**
* From CommandListener. Called by the system to indicate that a command has
* been invoked on a particular displayable.
*
* #param command
* the command that was invoked
* #param displayable
* the displayable where the command was invoked
*/
}
My log trace
Copying 1 file to C:\Users\ELHADI\Documents\NetBeansProjects\DiallNumber2\dist\nbrun5217990045006831680
Copying 1 file to C:\Users\ELHADI\Documents\NetBeansProjects\DiallNumber2\dist\nbrun5217990045006831680
Jad URL for OTA execution: http://localhost:8082/servlet/org.netbeans.modules.mobility.project.jam.JAMServlet/C%3A/Users/ELHADI/Documents/NetBeansProjects/DiallNumber2/dist//DiallNumber2.jad
Starting emulator in execution mode
Installing suite from: http://127.0.0.1:49320/DiallNumber2.jad
[WARN] [rms ] javacall_file_open: _wopen failed for: C:\Users\ELHADI\javame-sdk\3.0\work\0\appdb\_delete_notify.dat
I have solved the problem
import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Displayable.*;
import javax.microedition.lcdui.Form.*;
import javax.microedition.midlet.MIDlet;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class DiallNumber extends MIDlet
{
private Display display;
private MIDlet midlet;
Form f = new form("f");
/**}
* From MIDlet. Called when the MIDlet is started.
*/
public void startApp()
{
display = Display.getDisplay(this);
display.setCurrent(f);
}
public void pauseApp()
{
// No implementation required
}
/*
* /
*/
/**
* From MIDlet. Called to signal the MIDlet to terminate.
*
* #param unconditional
* whether the MIDlet has to be unconditionally terminated
*/
public void destroyApp(boolean unconditional)
{
// No implementation required
}
class form extends Form implements CommandListener
{
private Command call;
private TextField phoneField;
private String n;
public form(String title )
{
super(title);
call =new Command("call",call.OK,0);
this.phoneField =new TextField("number","",20,phoneField.PHONENUMBER);
addCommand(call);
append(this.phoneField);
this.setCommandListener(this);
}
public void commandAction(Command c,Displayable d)
{
if(c==call)
{
try
{
n=phoneField.getString().trim();
platformRequest("tel:"+n);
}
catch(javax.microedition.io.ConnectionNotFoundException e)
{e.printStackTrace();}
}
}
}
}

Displaying a message box or notification bar in java apps?

In my J2ME app, I have some forms, and some threads running on background. If in any of these threads I decide to display a message box or notification bar on top of the app, I have the problem of not knowing in which form I am, therefore I don't know which form to show after the messagebox or notification bar is hidden.
Does anyone have any suggestions?
You can get current form that is already displaying with "Display.getCurrent()".For example this canvas is a SplashScreen that get current form before displays in the screen:
import javax.microedition.lcdui.Canvas;
/* */ import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
/* */ import javax.microedition.lcdui.Graphics;
/* */ import javax.microedition.lcdui.Image;
public class StaticSplashScreen extends Canvas
implements Runnable {
private HelloMIDlet mainMidlet;
private boolean isSplashOver;
private long currentTime;
private long previousTime;
private Form currentForm;
public StaticSplashScreen(HelloMIDlet mid) {
this.mainMidlet = mid;
currentForm = (Form) this.mainMidlet.getDisplay().getCurrent();
this.previousTime = System.currentTimeMillis();
new Thread(this).start();
}
protected void paint(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0, 0, 0);
g.drawString("In the name of God", 40, 70, 0);
}
public void run() {
while (!this.isSplashOver) {
this.currentTime = System.currentTimeMillis();
if (this.currentTime - this.previousTime >= 10000L) {
this.isSplashOver = true;
}
}
this.mainMidlet.getDisplay().setCurrent(currentForm);
}
}
In this midlet you can see two forms with some commands.When you press "help" in each form,method() calls and SplashScreen diplays and after 10 seconds you can see the form that launched it again:
public class HelloMIDlet extends MIDlet implements CommandListener {
...
public void commandAction (Command command, Displayable displayable) {
...
if (command == helpCommand) {
method ();
}
...
}
public Form getForm () {
if (form == null) {
form = new Form ("Welcome");
form.addCommand (getHelpCommand());
form.setCommandListener (this);
}
return form;
}
public void method () {
if (true) {
StaticSplashScreen sss = new StaticSplashScreen(this);
this.getDisplay().setCurrent(sss);
} else {
}
}
public Form getForm1 () {
if (form1 == null) {
form1 = new Form ("form1");
form1.addCommand (getHelpCommand ());
form1.setCommandListener (this);
}
return form1;
}
}
A ticker is an object that provides scrolling text across the top of the display. A Ticker is associated with the display, not with the screen. You place a Ticker on a screen using the Screen.setTicker(Ticker t) method, as shown in the code below.
You can associate the same Ticker object with multiple screens, however. The implementation renders the Ticker on some constant portion of the display, in this case at the top of the display. Ticker is not an Item. Its derivation directly from java.lang.Object gives you a clue as to why a Ticker can be tied to the display and not to a screen. It doesn't need to be derived from Item, because it really is not something that is placed in a Form.
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Ticker;
import javax.microedition.lcdui.Form;
/**
This class demonstrates use of the Ticker MIDP UI
component class.
#see javax.microedition.lcdui.Gauge
*/
public class TickerDemo extends Form
implements CommandListener
{
private String str =
"This text keeps scrolling until the demo stops...";
private Ticker ticker = new Ticker(str);
private Command back = new Command("Back", Command.BACK, 1);
private static Displayable instance;
/**
Constructor.
*/
public TickerDemo()
{
super("Ticker demo");
instance = this;
addCommand(back);
setTicker(ticker);
setCommandListener(this);
}
...
}
Hope this will help you. Thanks

image not displaying in midlet

am new here. i have a slight problem; PLease look at the following code and tell me if am doing something wrong because the image is not displaying. i have made it really small so it should fit but its not displaying. i have images displaying in other screens but this main midlet would not. Here is the code:
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* #author jay
*/
public class WShop extends MIDlet implements CommandListener {
/* Declare display variables*/
private Form mainForm;
private Display display;
private Command OK,Exit,wView, mView, myView;
/* */
Categories categories = new Categories(this);
Image image;
public WShop() {
/* initialize Screen and Command buttons that will
be used when the application starts in the class constructor*/
mainForm = new Form("Wind Shopper");
OK = new Command("OK", Command.OK, 2);
Exit = new Command("Exit", Command.EXIT, 0);
wview= new Command("wview", Command.OK, 0);
mview= new Command("mview", Command.OK, 0);
try {
/* retrieving the main image of the application*/
image = Image.createImage("/main.png");
} catch (IOException ex) {
ex.printStackTrace();
}
mainForm.addCommand(OK);
mainForm.addCommand(Exit);
mainForm.addCommand(wView);
mainForm.addCommand(mView);
mainForm.setCommandListener(this);
}
public void startApp() {
/* checks to see if the display is currently empty
and then sets it to the current screen */
if (display == null) {
display = Display.getDisplay(this);
}
display.setCurrent(mainForm);
}
/* paused state of the application*/
public void pauseApp() {
}
/* Destroy Midlet state*/
public void destroyApp(boolean unconditional) {
}
Thanks in advance.
Looks to me like you forgot to Form.append() your Image to your form.

Resources