10-13-2009 09:06 AM
Hi,
I am trying to develop an application on blackberry which requires access to the camera.
My code is as below:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import com.sun.lwuit.Command;
import com.sun.lwuit.Display;
import com.sun.lwuit.MediaComponent;
import com.sun.lwuit.Form;
import com.sun.lwuit.Graphics;
import com.sun.lwuit.Image;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.layouts.BorderLayout;
import java.io.IOException;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;
/**
*
* @author nortons
*/
public class Camera implements ActionListener {
private Command cameraCommand;
private Command backCommand, captureCommand;
private Player player;
private VideoControl videoControl = null;
private static Camera thisInstance;
Form form = new Form(" ");
public Camera() {
cameraCommand = new Command("Camera");
backCommand = new Command("Back");
captureCommand = new Command("Capture");
}
public static Camera getInstance() {
if (thisInstance == null) {
thisInstance = new Camera();
}
return thisInstance;
}
public void showCamera() {
form.removeAll();
form.setLayout(new BorderLayout());
form.addCommand(backCommand);
form.setDefaultCommand(captureCommand);
form.setCommandListener(this);
try {
player = Manager.createPlayer("capture://video");
player.prefetch();
player.realize();
videoControl = (VideoControl) player.getControl("VideoControl");
MediaComponent mediaComponent = new MediaComponent(player);
int w = Display.getInstance().getDisplayWidth() - 10;
int h = Display.getInstance().getDisplayHeight() - 100;
mediaComponent.setWidth(w);
mediaComponent.setHeight(h);
mediaComponent.setPreferredSize(new Dimension(w, h));
form.addComponent(BorderLayout.CENTER, mediaComponent);
player.start();
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
} catch (MediaException me) {
System.err.println(me.getMessage());
}
form.show();
}
private void capture() {
try {
// Get the image.
byte[] raw = videoControl.getSnapshot(null);
System.out.println("Bytes Before:" + raw.toString());
player.close();
player = null;
videoControl = null;
CapturedImage.getInstance().setRaw(raw);
CapturedImage.getInstance().show();
// Shut down the player.
} catch (MediaException me) {
System.err.println(me.getMessage());
}
}
public void actionPerformed(ActionEvent arg0) {
if (arg0.getCommand() == cameraCommand) {
showCamera();
} else if (arg0.getCommand() == backCommand) {
player.close();
form.removeAll();
PatientDetailsForm.getInstance().show();
} else if (arg0.getCommand() == captureCommand) {
capture();
}
}
}
This works fine on the WTK. But when i try the same on blackberry emulator and when i try capturing the image i get the following exception i get the following exception:
java.lang.illegalStateException:initDisplayMode() has not been called.
Then i added the following line of code after the player.getControl(); :
videoControl.initDisplayMode(VideoControl.USE_GUI_
now when i try capturing i get a nullPointerException().
I even tried the following
Item item=(Item)videoControl.initDisplayMode(VideoContr
i still get nullPointerException().
Can someone pls help me with this.
FYI: I am using JDE 4.6 and Device Simulator 9000
Thanking you,
Regards
S.A.Norton Stanley.
10-14-2009 12:54 AM
10-14-2009 01:21 AM
Hi,
I went through the sample and the code is the same as above. The only difference is that i am not using RIM api for UI.
Regards,
S.A.Norton Stanley
10-14-2009 01:54 AM
10-14-2009 02:42 AM
Hi,
The first thing is that i am not able to get the View finder itself displayed.It just displays an empty form and when i click on capture on executing the following line of code byte[] raw = videoControl.getSnapshot(null); i get java.lang.illegalStateException:initDisplayMode() has not been called.
Thanking you,
Regards,
S.A.Norton Stanley