簡體   English   中英

黑莓手機有可能自動捕獲圖像嗎?

[英]is it possible in blackberry to capture image automatic

我是黑莓的新手。 我現在創建應用程序,我的要求是在相機自動調用時捕獲圖片。在黑莓機中是否有可能。 我正在使用此代碼進行所有工作,但無法自動捕獲圖片,請提出我對代碼所做的更改,以確保它對我的應用程序有效。

 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);
   }



}

您正在使用傳統方法拍照。 這就是為什么您不應該使用它的原因:

  • 因為您要退出應用程序以調用外部應用程序(相機應用程序)。
  • 因為您無法控制相機設置。
  • 因為您發布的FileJournalListener代碼(RIM在其教程中發布的代碼)在某些OS和模擬器上會出現問題(事件以不同順序到達或根本沒有到達,圖像保存中出現多個事件,等等)。
  • 因為無法關閉相機應用程序,所以您必須采用hacky密鑰注入方法。

因此,現在,對於操作系統> 4.6的操作系統,您可以使用MMAPI在應用程序內部進行快照。 這是推薦的方法,除非為舊版OS編程,否則不應使用您發布的方法。 檢查本教程 另外,從5.0開始,您可以使用AMMAPI來控制某些相機設置。 並非所有控件都已實現 ,但最新的操作系統支持大多數基本控件 (縮放,對焦,閃光燈等)。 檢查最新BlackBerry JDE中包含的 CameraDemo VideoRecordingDemo示例。

嘗試使用此代碼,它將自動拍照。

import java.io.OutputStream;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
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
{   
    Timer timer ;
    Player _p;
    VideoControl _videoControl;    

    public ImageCaptureDemoScreen()
    {
        try 
        {
            _p = javax.microedition.media.Manager.createPlayer("capture://video?encoding=jpeg&width=320&height=240");
            _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());
        }


        timer = new Timer();
        timer.schedule(new CountDown(), 3000);


    }

    public class CountDown extends TimerTask {
      public void run() {
         DismissThread dThread = new DismissThread();
         invokeLater(dThread);
      }
   }
 public void dismiss() {
      timer.cancel();
     invokeAction(ACTION_INVOKE);
    _videoControl.setVisible(false);

   }
   public class DismissThread implements Runnable {
          public void run() {
             dismiss();
          }
       }



    protected boolean invokeAction(int action)
    {
        boolean handled = super.invokeAction(action); 

        if(!handled)
        {
            if(action == ACTION_INVOKE)
            {   
                try
                {                      
                    byte[] rawImage = _videoControl.getSnapshot(null);  


                    FileConnection conn = (FileConnection)Connector.open("file:///SDCard/BlackBerry/pictures/"+System.currentTimeMillis()+".jpeg", Connector.READ_WRITE);
                    conn.create();
                    OutputStream out = conn.openOutputStream();
                    out.write(rawImage);
                    out.flush();
                    out.close();
                    conn.close();

                    /*

                    Bitmap image = Bitmap.createBitmapFromBytes(rawImage, 0, rawImage.length, 5);
                    BitmapField bitmapField = new BitmapField();
                    bitmapField.setBitmap(image);
                    this.add(bitmapField);
              */

                }
                catch(Exception e)
                {
                    Dialog.alert(e.toString());
                }
            }
        }           
        return handled;                
    }  
}   
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM