如何将捕获的图像保存在黑莓的文件夹中?

时间:2011-12-30 10:11:34

标签: blackberry

我正在开发具有相机功能的应用程序。我想将捕获的图像保存在我的项目文件夹中。任何人都可以帮助我。 提前完成。

4 个答案:

答案 0 :(得分: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 action)方法

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

答案 1 :(得分: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());
                }

答案 2 :(得分: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/java_dev@tkb/226/1/SnapshotSample.zip

答案 3 :(得分:0)

如果要在运行期间将图像保存在Project / res或Project / src中,则无法执行此操作。

您可以将图像保存在SD卡/设备中。

为什么要将它保存在项目源中?