Search This Blog

Friday, 1 June 2012

How to integrate camera in your Blackberry app

Hello friends,
                  Till today afternoon, i was wondering how can i integrate camera in my app to take picture and send it to server....then i get it.....follow the code,and you still need to do some work as this will return the captured image in byte array , after that you have to convert it into an image....

CODE:

import javax.microedition.media.Player;
import net.rim.device.api.ui.Field;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;


Player camera;
VidepControl _videoControl;

try{
            camera = Manager.createPlayer("capture://video?encoding=jpeg&width=1024&height=768");
            camera.realize();
            _videoControl = (VideoControl)camera.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);
               
                camera.start();
               
//                EnhancedFocusControl efc = (EnhancedFocusControl)camera.getControl("net.rim.device.api.amms.control.camera.EnhancedFocusControl");
//                efc.startAutoFocus();
               
                if(videoField!=null){
                    _main.add(videoField);
                }else{
                    Dialog.alert("Video Field is null");
                }
            }else{
                Dialog.alert("Video Control is null");
            }
        }catch(Exception e){
           
            Dialog.alert("Error Found :"+e.toString());
        }


Then use this below function to get snapshot on click on touchscreen,touchpad or trackball....

protected boolean invokeAction(int action)
    {
        boolean handled = super.invokeAction(action);
   
        if(!handled)
        {
            if(action == ACTION_INVOKE)
            {  
                try
                {                     
                    byte[] rawImage = _videoControl.getSnapshot(null);  
                    System.out.println("Capturedddddd.......: "+rawImage.length);
                }
                catch(Exception e)
                {
                    Dialog.alert(e.toString());
                }
            }
        }          
        return handled;               
    } 

No comments:

Post a Comment