我创建了一个黑莓相机应用程序。在此应用程序中,我尝试在调用相机时自动拍照。我的相机被调用但没有自动拍照。我正在遵循此代码。
public class Test extends MainScreen implements FileSystemJournalListener {
long _lastUSN;
ButtonField btnTakePhoto;
String capturedImgPath = "";
VideoControl videoControl;
Timer objTimer;
Player player;
public Test()
{
super();
btnTakePhoto = new ButtonField("Take Picture",ButtonField.VCENTER|ButtonField.BOTTOM);
btnTakePhoto.setChangeListener(TakePictureListener);
HorizontalFieldManager hfm=new HorizontalFieldManager();
hfm.add(btnTakePhoto);
add(hfm);
System.out.println("Inside Construct");
UiApplication.getUiApplication().addFileSystemJournalListener(this);
_lastUSN = FileSystemJournal.getNextUSN();
this.setTitle("Camera Class");
}
public void backGroundPaint(Graphics g)
{
System.out.println("Inside backGroundPaint");
g.setBackgroundColor(Color.PINK);
g.clear();
}
FieldChangeListener TakePictureListener = new FieldChangeListener(){
public void fieldChanged(Field field, int context) {
System.out.println("Inside fieldChanged");
doTakePicture();
}
};
public void doTakePicture(){
try
{
System.out.println("Inside doTakePicture");
Invoke.invokeApplication(Invoke.APP_TYPE_CAMERA,new CameraArguments());
player = javax.microedition.media.Manager.createPlayer("capture://video??encoding=jpeg&width=240&height=240");
player.realize();
videoControl = (VideoControl) player.getControl("VideoControl");
player.start();
if(videoControl!=null)
{
Field videoField = (Field) videoControl.initDisplayMode (VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
videoControl.setDisplayFullScreen(true);
add((Field) videoControl);
videoControl.setVisible(true);
}
}
catch(Exception ex)
{
System.out.println(ex);
Dialog.alert(toString());
}
}
public void invokeAction()
{
doTakePicture();
System.out.println("Inside Invoke Action");
try {
byte[] snapshot = videoControl.getSnapshot(null);
//player.close();
System.out.println("snapshot=="+snapshot);
EncodedImage bitmap=EncodedImage.createEncodedImage(snapshot, 0, snapshot.length);
System.out.println("bitmap=="+bitmap);
BitmapField field1 = new BitmapField();
System.out.println("field1=="+field1);
field1.setImage(bitmap);
add(field1);
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void fileJournalChanged()
{
System.out.println("Inside fileJournalChanged");
long nextUSN = FileSystemJournal.getNextUSN();
String msg = null;
String path = null;
for (long lookUSN = nextUSN - 1; lookUSN >= _lastUSN && msg == null; --lookUSN)
{
FileSystemJournalEntry entry = FileSystemJournal.getEntry(lookUSN);
if (entry == null)
{
break;
}
path = entry.getPath();
System.out.println("Path=="+path);
if (path != null)
{
if (path.endsWith("png") || path.endsWith("jpg") || path.endsWith("bmp") || path.endsWith("gif") ){
switch (entry.getEvent())
{
case FileSystemJournalEntry.FILE_ADDED:
System.out.println("Inside FILE_ADDED");
msg = "File was added.";
break;
case FileSystemJournalEntry.FILE_DELETED:
System.out.println("Inside FILE_DELETED");
msg = "File was deleted.";
break;
}
}
}
}
_lastUSN = nextUSN;
if ( msg != null )
{
Dialog.alert(msg);
capturedImgPath = path;
closeCamera();
}
}
private void closeCamera()
{
int menuOrder =6;
System.out.println("Inside Close Camera");
EventInjector.invokeEvent(new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.KEY_DOWN, (char)Keypad.KEY_MENU, KeypadListener.STATUS_NOT_FROM_KEYPAD, 0));
EventInjector.invokeEvent(new EventInjector.TrackwheelEvent(EventInjector.TrackwheelEvent.THUMB_ROLL_DOWN, menuOrder, KeypadListener.STATUS_NOT_FROM_KEYPAD));
EventInjector.invokeEvent(new EventInjector.TrackwheelEvent(EventInjector.TrackwheelEvent.THUMB_CLICK, 1, KeypadListener.STATUS_NOT_FROM_KEYPAD));
Dialog.alert("The captured Image path is "+capturedImgPath);
}
}