Code sample: Taking a picture in a BlackBerry device application

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.amms.control.camera.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;

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

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

Was this information helpful? Send us your comments.