2

I am developing application which has camera functionality. i want to save that captured image in my project folder.Can any one help me please. thanx in advance.

2
  • What do you mean by project folder?
    – rfsk2010
    Dec 30, 2011 at 10:25
  • project folder mean simple folder present in src.
    – Hasmukh
    Dec 30, 2011 at 10:52

4 Answers 4

2

This below code may help you:

Add this to Main Screen which you want to show the camera:

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

The code for captureImage() method is:

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

The below code is save the image into the sdcard or device card. Override the invokeAction(int action) Method

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

try this for image snap

Note: you can not store in resource folder you can only store in SDcard

 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){

                }
            }

        }
    }

for more operations please use this http://supportforums.blackberry.com/rim/attachments/rim/java_dev@tkb/226/1/SnapshotSample.zip

0
1

if you want to save an image from camera to your device you can use the following code.

 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

If you want to save the images in your Project/res or Project/src during runtime, you cant do that.

You can save image in SDcard/device.

Why would you want to save it in your project source?

2
  • 1
    Once you save the image in the SDcard/device you can read the image and use it in your app. does that help?
    – rfsk2010
    Dec 30, 2011 at 11:11
  • You can use it if you save the image in device; Dec 30, 2011 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.