我创建了一个黑莓相机应用程序。 在此应用程序中,我尝试在调用相机时自动拍照。 我的相机被调用但没有自动拍照。 我正在遵循此代码。
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);
}
}
答案 0 :(得分:0)
试试这段代码。
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;
public class ImageCaptureDemo extends UiApplication
{
public static void main(String[] args)
{
ImageCaptureDemo app = new ImageCaptureDemo();
app.enterEventDispatcher();
}
public ImageCaptureDemo()
{
pushScreen(new ImageCaptureDemoScreen());
}
class ImageCaptureDemoScreen extends MainScreen
{
Player _p;
VideoControl _videoControl;
public ImageCaptureDemoScreen()
{
try
{
_p = javax.microedition.media.Manager.createPlayer("capture://video?encoding=jpeg&width=1024&height=768");
_p.realize();
_videoControl = (VideoControl) _p.getControl("VideoControl");
if (_videoControl != null)
{
Field videoField = (Field) _videoControl.initDisplayMode (VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
_videoControl.setDisplayFullScreen(true);
_videoControl.setVisible(true);
_p.start();
/*EnhancedFocusControl efc = (EnhancedFocusControl)p.getControl("net.rim.device.api.amms.control.camera.EnhancedFocusControl");
efc.startAutoFocus();*/
if(videoField != null)
{
add(videoField);
}
}
}
catch(Exception e)
{
Dialog.alert(e.toString());
}
}
protected boolean invokeAction(int action)
{
boolean handled = super.invokeAction(action);
if(!handled)
{
if(action == ACTION_INVOKE)
{
try
{
byte[] rawImage = _videoControl.getSnapshot(null);
Bitmap image = Bitmap.createBitmapFromBytes(rawImage, 0, rawImage.length, 100);
BitmapField bitmapField = new BitmapField();
bitmapField.setBitmap(image);
this.add(bitmapField);
}
catch(Exception e)
{
Dialog.alert(e.toString());
}
}
}
return handled;
}
}
}