I am new in blackberry. i create Application now my requirement is when camera is invoke automatic a picture is capture.Is it possible in blackberry. i am using this code every thing work but not capture picture automatic please suggest what i change on my code it's work on my Application.
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");
}
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");
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);
videoControl.setVisible(true);
add(videoField);
// System.out.println("videoControl=="+videoControl);
// String imagetype = "encoding=jpeg&width=1024&height=768&quality=fine";
//
// byte[] snapshot = videoControl.getSnapshot(imagetype);
// Bitmap image = Bitmap.createBitmapFromBytes(snapshot, 0, snapshot.length, 5);
// System.out.println("snapshot=="+snapshot);
// System.out.println("image=="+image);
// BitmapField bitmapField = new BitmapField();
// bitmapField.setBitmap(image);
// this.add(bitmapField);
// if(videoField != null)
// {
// add(videoField);
//
// }
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
public boolean invokeAction(int action)
{
System.out.println("Action=="+action);
boolean handled = super.invokeAction(action);
//handled=true;
System.out.println("handled=="+handled);
System.out.println("Inside Invoke Camera");
if(!handled)
{
System.out.println("Inside First If Blog");
if(action == ACTION_INVOKE)
{
System.out.println("Inside Second If Blog");
try
{
System.out.println("If Blog of invoke Action");
System.out.println("videoControl11=="+videoControl);
String imagetype = "encoding=jpeg&width=1024&height=768&quality=fine";
byte[] snapshot = videoControl.getSnapshot(imagetype);
Bitmap image = Bitmap.createBitmapFromBytes(snapshot, 0, snapshot.length, 5);
System.out.println("snapshot=="+snapshot);
System.out.println("image=="+image);
BitmapField bitmapField = new BitmapField();
bitmapField.setBitmap(image);
this.add(bitmapField);
}
catch(Exception e)
{
Dialog.alert(e.toString());
}
}
}
return handled;
}
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);
}
}
Try this code, this will automatically take picture.
You are using a legacy approach to take pictures. This is why you shouldn't use it:
So now, for OSes > 4.6, you can use MMAPI to take snapshot inside your app. This is the recommended method and you should not use the one you posted unless you are programming for legacy OSes. Check this tutorial. Also, from 5.0 onwards you can use AMMAPI to control some camera settings. Not all controls are implemented, but most basic ones (zoom, focus, flash, etc) are supported in the latest OSes. Check the
CameraDemoVideoRecordingDemo sample included in the latest BlackBerry JDE.