2011-12-30 20 views

回答

1

嘗試一下本作的圖像捕捉

注意:不能在資源文件夾中存儲你只能在SD卡

class screen extends MainScreen implements FieldChangeListener 
    { 
     private VideoControl vc; 
     private String encoding; 
     private Player p; 
     private Field viewFinder; 
     private BitmapField bitmapField; 
     private ButtonField btn; 
     public screen() { 
      btn=new ButtonField("snap",Field.FOCUSABLE); 
      btn.setChangeListener(this); 
      add(btn); 
     } 
     public void fieldChanged(Field field, int context) { 
      if(field==btn) 
      { 
       try{       
        p = Manager.createPlayer("capture://video");       
        p.realize(); 
        p.prefetch(); 
        p.start(); 
        vc = (VideoControl) p.getControl("VideoControl"); 
        viewFinder = (Field)vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field"); 
        vc.setVisible(true);  
        final String imageType = "encoding=jpeg&width=640&height=480&quality=superfine"; 

        UiApplication.getUiApplication().invokeLater(new Runnable(){ 
         public void run(){ 

byte[] image = vc.getSnapshot(imageType); 
         FileConnection conn = (FileConnection)Connector.open("file:///store/home/user/pictures/"+System.currentTimeMillis()+".jpeg", Connector.READ_WRITE); 
         conn.create(); 
         OutputStream out = conn.openOutputStream(); 
         out.write(image); 
         out.flush(); 
         out.close(); 
         conn.close(); 
           Bitmap image1 = Bitmap.createBitmapFromBytes(imageBytes, 0, imageBytes.length, 4); 
           bitmapField.setBitmap(image1); 
           add(bitmapField);       


         } 
        }); 

       } catch (Exception me){ 

       } 
      } 

     } 
    } 

存儲更多操作請使用此 http://supportforums.blackberry.com/rim/attachments/rim/[email protected]/226/1/SnapshotSample.zip

1

如果您想要將圖像從相機保存到設備中,您可以使用以下代碼。

try 
       { 
         focusControl.startAutoFocus(); 
         byte[] image = videoControl.getSnapshot(null); 
         String message = "Captured "+image.length+" bytes of JPG data"; 
         debug(message); 

         FileConnection conn = (FileConnection)Connector.open("file:///store/home/user/pictures/"+(new Date()).getTime()+".jpg", Connector.READ_WRITE); 
         conn.create(); 
         OutputStream out = conn.openOutputStream(); 
         out.write(image); 
         out.flush(); 
         out.close(); 
         conn.close();       

         Dialog.alert(message); 
       } 
       catch (Exception e) { 
         Dialog.alert("Capture failed due to: "+e.getMessage()); 
       } 
0

如果你想在運行時保存在您的項目/ RES或項目/ src中的圖像,你不能做到這一點。

您可以將圖像保存在SD卡/設備中。

爲什麼要將其保存到項目源中?

+0

bcz我想要在我的應用程序中使用該圖像。 – Hasmukh 2011-12-30 11:09:54

+1

將圖像保存到SD卡/設備後,您可以讀取圖像並將其用於您的應用程序。這有幫助嗎? – rfsk2010 2011-12-30 11:11:03

+0

如果將圖像保存在設備中,您可以使用它; – alishaik786 2011-12-30 11:11:35

2

這下面的代碼可能會幫助你:

這個添加到您想要顯示攝像機主屏幕:

captureImage=new MenuItem("Capture Images",10,100) 
{   
    public void run() 
    { 
     synchronized (Application.getEventLock()) 
     { 
      captureImage(); 
     }    
    } 
};   
addMenuItem(captureImage); 

captureImage()方法是代碼:

private void captureImage() 
{ 
    try 
    { 
     player = javax.microedition.media.Manager.createPlayer("capture://video?encoding=jpeg&width=1024&height=768"); 
     player.realize(); 
     _videoControl = (VideoControl) player.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); 
      player.start(); 

      if(videoField != null) 
      { 
       add(videoField); 
      } 
     } 
    } 
    catch(Exception e) 
    { 
     if(player!=null) 
     { 
      player.close(); 
     } 
     Dialog.alert(e.toString()); 
    }  
} 

下面的代碼是將圖像保存到SD卡或設備卡。覆蓋invokeAction(INT行動)方法

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

    if(SdcardTest.SdcardAvailabulity())//I am checking here that the sdcard is there of or not.....?  
    { 
      //PATH = "file:///SDCard/BlackBerry/pictures/"+"Image_"+System.currentTimeMillis()+".jpg"; 
      PATH = System.getProperty("fileconn.dir.memorycard.photos")+"Image_"+System.currentTimeMillis()+".jpg";//here "str" having the current Date and Time; 
    }    
    else 
    { 
     PATH = System.getProperty("fileconn.dir.photos")+"Image_"+System.currentTimeMillis()+".jpg"; 
    } 
    if(!handled) 
    { 
     if(action == ACTION_INVOKE) 
     { 
      try 
      {      
       byte[] rawImage = _videoControl.getSnapshot(null); 
       fileconn=(FileConnection)Connector.open(PATH); 
       if(fileconn.exists()) 
       { 
        fileconn.delete(); 
       } 
       fileconn.create(); 
       OutputStream os=fileconn.openOutputStream(); 
       os.write(rawImage); 
       fileconn.close(); 
       os.close(); 
       Status.show("Image is Captured",200); 
       if(player!=null) 
        player.close();          
      } 
      catch(Exception e) 
      { 
       if(player!=null) 
       { 
        player.close(); 
       } 
       if(fileconn!=null) 
       { 
        try 
        { 
         fileconn.close(); 
        } 
        catch (IOException e1) 
        { 
           //if the action is other than click the trackwheel(means go to the menu options) then we do nothing; 
        } 
       }      
      } 
     } 
    }   
    return handled;     
} 
相關問題