www.pudn.com > S60_remote_camera.zip > remotecam.cpp


/* 
 
        RemoteCam.CPP - source file for RemoteCam application Averell 
        C++ implementation 
 
 
*/ 
 
 
//  Include Files 
#include 		// for the CleanupStack 
#include          // for TBool enums 
#include        // for ClientRect, CreateAppUiL 
#include 			// for CEikonEnv - "Eikon Environment" 
#include 			// for UIKON definitions 
#include    // for CAknViewAppUi 
#include         // for Active Scheduler 
#include         // for common enumerations 
#include         // for Averell enumerations 
#include            // for CSmsMessageSettings 
#include      // for RCameraServ 
#include          // for CMsvSession 
#include        // for CBaseMtm 
#include         // for CClientMtmRegistry 
#include          // for RNotifier 
 
#include "RemoteCam.h"                  // own header 
#include "RemoteCam.hrh"                // own enumerations 
#include                 // own resources 
 
 
// Constants 
const TUid KUidRemoteCamApp = { 0x101F402A };      // RemoteCam application UID 
const TUid KUidRemoteCamMsg = { 0x101F3CD9 };      // RemoteCam Bio message UID  
const TUid KRemoteCamViewId = { 1 };               // UID of RemoteCam view 
const TInt KJpgSavingQualityFactor = 55;           // Quality factor for JPG saving 
 
 
 
// 
// CRemoteCamContainer  
// This container does not have any controls in it, it is only used to draw an image. 
// 
 
/* 
------------------------------------------------------------------------------- 
 
    ~CRemoteCamContainer(); 
 
    Description: Destructor. 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
CRemoteCamContainer::~CRemoteCamContainer() 
    { 
    delete iBmp; 
    iFormView = NULL; 
    } 
 
/* 
------------------------------------------------------------------------------- 
 
    ConstructL(); 
 
    Description: 2nd phase Constructor. 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
void CRemoteCamContainer::ConstructL(CRemoteCamFormView& aFormView) 
    { 
    iBmp = new (ELeave) CFbsBitmap; 
    iFormView = &aFormView;             // take a handle to the object owninf this container 
    iImageReady = EFalse;               // flag: image ready to be drawn or not 
    ConstructContainerControlL();       // Construct self 
    } 
 
 
 
/* 
------------------------------------------------------------------------------- 
 
    ConstructContainerControlL(); 
 
    Description: Constructing container control 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
void CRemoteCamContainer::ConstructContainerControlL() 
    { 
    CreateWindowL();                         // Makes the control window owning 
    } 
 
 
/* 
------------------------------------------------------------------------------- 
 
    Draw(); 
 
    Description: This function draws the application view on the screen. 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
void CRemoteCamContainer::Draw(const TRect& /*aRect*/) const 
    { 
    CGraphicsContext& gc=SystemGc();  // Get the graphics context in which to draw. 
     
    if(iImageReady) 
        { 
        gc.DrawBitmap( Rect(), iBmp); // draw our picture 
        gc.DrawRect(Rect());          // Draw a rectangle. 
        } 
    else 
        { 
        // no image to draw, draw a text saying "Preview" to inform user on behaviour of the directional button 
 
        // Draw white background 
        gc.SetBrushStyle(CGraphicsContext::ESolidBrush); 
        gc.SetBrushColor(KRgbWhite); 
        gc.DrawRect(Rect()); 
 
        // Draw black outline 
        gc.SetBrushStyle(CGraphicsContext::ENullBrush); 
        gc.SetBrushColor(KRgbBlack); 
        gc.DrawRect(Rect()); 
          
        // Draw text "Preview" 
        gc.SetPenColor(KRgbBlack);  
        const CFont* fontUsed = iEikonEnv->TitleFont(); 
        gc.UseFont(fontUsed); 
 
        TInt baseline = Rect().Height() - fontUsed->AscentInPixels()*2; // set text 2 * text ascent abowe the lower border 
        TInt margin=0; // margin is zero so that the text will be cenetred 
 
        _LIT(KPreviewText,"Preview"); 
        gc.DrawText(KPreviewText,Rect(),baseline,CGraphicsContext::ECenter, margin); 
         
        } 
    } 
 
 
/* 
------------------------------------------------------------------------------- 
 
    GetBmp(); 
 
    Description: This function returns a pointer to the bitmap. 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
CFbsBitmap* CRemoteCamContainer::GetBmp() 
    { 
    // this function is used to get the bitmap to take a new picture 
    // so reseting the bmp first 
    iImageReady = EFalse; 
    iBmp->Reset(); 
    return iBmp; 
    } 
 
/* 
------------------------------------------------------------------------------- 
 
    GetBmpForSaving(); 
 
    Description: This function returns a pointer to the bitmap. 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
CFbsBitmap* CRemoteCamContainer::GetBmpForSaving() 
    { 
    // this fuction is used to get the current image for saving 
    // so no reseting this time 
    return iBmp; 
    } 
 
 
 
// 
// CRemoteCamFormView 
// 
 
/* 
------------------------------------------------------------------------------- 
 
    ConstructL(); 
 
    Description: 2nd phase Constructor. 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
void CRemoteCamFormView::ConstructL() 
    { 
    // Current image index starts from zero (could used when saving images (not used at the moment)) 
    iCurrentImage = 0; 
    iSavingImage = EFalse;  // flag to tell when the app is saving the image asynchronously (can't take a new pict when saving)  
 
    // Initialise a client to the camera server 
    iCamserv = new(ELeave) RCameraServ; 
     
    // Init a file saver utility 
    iFileSaver = CMdaImageBitmapToFileUtility::NewL(*this); 
    iFormat = new (ELeave) TMdaJfifClipFormat; 
    } 
 
 
/* 
------------------------------------------------------------------------------- 
 
    ~CRemoteCamFormView(); 
 
    Description: Destructor. 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
CRemoteCamFormView::~CRemoteCamFormView() 
    { 
    if(iContainer) 
        AppUi()->RemoveFromStack(iContainer); 
 
    delete iCamserv; 
    delete iFileSaver; 
    delete iFormat; 
    } 
 
/* 
------------------------------------------------------------------------------- 
 
    Id(); 
 
    Description: Returns the id of the view object. 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
TUid CRemoteCamFormView::Id() const 
    { 
    return KRemoteCamViewId; 
    } 
 
 
/* 
------------------------------------------------------------------------------- 
 
    DoActivateL(); 
 
    Description: Activate this view. 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
void CRemoteCamFormView::DoActivateL(const TVwsViewId& /*aPrevViewId*/, TUid /*aCustomMessageId*/, const TDesC8& /*aCustomMessage*/ ) 
    { 
    // Connect to Camera Server 
    User::LeaveIfError(iCamserv->Connect()); 
     
    if (!iContainer) // container hasn't been created yet 
        { 
        // Then construct the UI components 
        iContainer = new(ELeave) CRemoteCamContainer;              
        iContainer->ConstructL(*this);             // Construct a view control with a ref. to this CRemoteCamFormView 
        iContainer->SetRect(ClientRect());         // Sets view control's extent to the space available 
        } 
 
    iContainer->ActivateL();                       // Activate the view control 
    } 
 
 
/* 
------------------------------------------------------------------------------- 
 
    DoDeactivate(); 
 
    Description: Deactivate this view. 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
void CRemoteCamFormView::DoDeactivate() 
    { 
    if (iContainer) 
        { 
        delete iContainer; 
        iContainer = NULL; 
        } 
 
    // Disconnect from Camera server 
    if(iCamserv) 
        { 
        iCamserv->Close(); 
        } 
 
    } 
 
 
 
/* 
------------------------------------------------------------------------------- 
 
    TakePreviewL(); 
 
    Description: Take a preview picture or a "snapshot" to ease the aiming of  
                 the camera. 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
void CRemoteCamFormView::TakePreviewL() 
    { 
    if(!iSavingImage) 
        {   
 
        // turn on camera 
        TurnCameraOnL(); 
     
        // if the container is currently showing an image, iImageReady = ETrue 
        if(iContainer->iImageReady) 
            { 
            // if we are shoving a picture on screen, the next keypress will only clear the screen 
            iContainer->iImageReady = EFalse; 
            iContainer->DrawNow(); 
            } 
        else 
            { 
            // Call TakePictureL() to take a low quality image 
            TakePictureL(ERCLow); 
            } 
 
        // turn off camera 
        TurnCameraOffL(); 
        } 
    } 
 
/* 
------------------------------------------------------------------------------- 
 
    TurnCameraOnL(); 
 
    Description: Turn on the camera. Turning on the camera takes some time 
                 and you can do something meaningful between turning on and 
                 taking a picture. 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
void CRemoteCamFormView::TurnCameraOnL() 
    { 
    // Status variable for async function calls 
    TRequestStatus status( KErrNone ); 
 
    // Turn camera ON 
    iCamserv->TurnCameraOn(status); 
    User::WaitForRequest(status); 
    if( status.Int() != KErrNone ) 
        { 
        // error while turning ON, closing connection to server 
        iCamserv->Close(); 
        User::Leave(status.Int()); 
        } 
    } 
 
/* 
------------------------------------------------------------------------------- 
 
    TurnCameraOffL(); 
 
    Description: Turn off the camera. A separate method to ease using 
                 TurnCameraOnL() from a location other than from where the  
                 actual picture is taken.  
                 You can also turn camera off straight after taking the picture. 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
void CRemoteCamFormView::TurnCameraOffL() 
    { 
    // Turn camera OFF 
    User::LeaveIfError(iCamserv->TurnCameraOff()); 
    } 
 
/* 
------------------------------------------------------------------------------- 
 
    TakePictureL(); 
 
    Description: Take a picture and show it on screen. This function sets up, 
                 image quality and lighting settings and then gets one image 
                 from the camera server. 
 
    Note:        The camera has been turned ON earlier and it will be turned OFF 
                 after returning from this function. 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
void CRemoteCamFormView::TakePictureL(TRemoteCamImageQuality aQuality) 
    { 
    // Check the given image quality and set up quality for camera server 
    RCameraServ::TImageQuality imageQuality = RCameraServ::EQualityLow; 
 
    if(aQuality == ERCHigh)     // taking a real picture (no snapshot) 
        imageQuality = RCameraServ::EQualityHigh; 
 
    // Get a bmp handle from our container 
    CFbsBitmap* bmp = iContainer->GetBmp(); 
//	CleanupStack::PushL(bmp); 
 
    // Set image quality (High = 640x480 16M colors, Low = 160x120 4096 colors) 
    User::LeaveIfError(iCamserv->SetImageQuality(imageQuality)); 
     
    // Set lighting conditions (normal or night) 
    User::LeaveIfError(iCamserv->SetLightingConditions(RCameraServ::ELightingNormal)); 
    
 
    // Status variable for async function calls 
    TRequestStatus status( KErrNone ); 
 
    // Get an image 
    iCamserv->GetImage( status, *bmp ); 
    User::WaitForRequest(status); 
    if( status.Int() != KErrNone ) 
        { 
        iCamserv->TurnCameraOff(); 
        iCamserv->Close(); 
        User::Leave(status.Int()); 
        } 
 
    // Show the image on screen 
    iContainer->iImageReady = ETrue; 
    iContainer->DrawNow(); 
 
    // clear stack 
//    CleanupStack::Pop();  // bmp 
 
    // clear local pointer 
    bmp = NULL; 
    } 
 
/* 
------------------------------------------------------------------------------- 
 
    SaveImageL(); 
 
    Description: Save an image. This function starts an asynchronous series of 
                 functions to save the image into a file. 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
void CRemoteCamFormView::SaveImageL() 
    { 
    // Be sure that we have an image to be saved   
    if(iContainer->iImageReady) 
        { 
        // gets path where to save the image 
        TFileName newFilePathAndName(_L("C:\\aaaReca.jpg")); // now saving only as "aaaReca.jpg" 
                                                             // Image name and path could be given  
                                                             // in the bio message that triggers the camera. 
        // Create Format and Codec 
        iFormat->iSettings.iQualityFactor = KJpgSavingQualityFactor; // quality factor from 0 to 100 (55 used here) 
        iFormat->iSettings.iSampleScheme = TMdaJpgSettings::TColorSampling(TMdaJpgSettings::EColor420); 
 
        TMdaPackage* codec = NULL; 
        CleanupStack::PushL(codec); 
 
        // create a file to save the image into 
        // this is done in an asynch function 
        iFileSaver->CreateL( newFilePathAndName, iFormat, codec, NULL ); 
 
        iSavingImage = ETrue; // set flag, can't take new pictures when saving 
 
        CleanupStack::PopAndDestroy(1); // codec 
       
        iCurrentImage++; // increase the "index" by one (not used for anything at the moment) 
        } 
    } 
 
/* 
------------------------------------------------------------------------------- 
 
    MiuoCreateComplete(); 
 
    Description: This function is called when the asynch. function  
                 iFileSaver->CreateL() completes. The error value tells if the 
                 creation was successful. If it was, we have created a file 
                 into which we can now save the image. 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
void CRemoteCamFormView::MiuoCreateComplete(TInt aError) 
    { 
    if (( aError == KErrNone) && iFileSaver) 
        { 
        TRAP( aError, iFileSaver->ConvertL(*(iContainer->GetBmpForSaving()), TRect(0,0,0,0), 0)); 
        } 
    else  
        { 
        User::Leave(aError); 
        } 
    } 
 
/* 
------------------------------------------------------------------------------- 
 
    MiuoConvertComplete(); 
 
    Description: This function is called when the asynch. function  
                 iFileSaver->ConvertL() completes. Now the image saving has 
                 completed. 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
void CRemoteCamFormView::MiuoConvertComplete(TInt /*aError*/)        
    { 
    iSavingImage = EFalse; // set flag, can take new pictures  
    } 
 
/* 
------------------------------------------------------------------------------- 
 
    MiuoOpenComplete(); 
 
    Description: From MMdaImageUtilObserver, implement this if you use this  
                 observer to open a imagefile. 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
void CRemoteCamFormView::MiuoOpenComplete(TInt /*aError*/) 
    { 
    } 
 
// 
//  CRemoteCamAppUi 
// 
 
/* 
------------------------------------------------------------------------------- 
 
    ~CRemoteCamAppUi 
 
    Description: Destructor 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
CRemoteCamAppUi::~CRemoteCamAppUi() 
    { 
    delete iMtm; 
    delete iMtmReg; 
    delete iSession; // must be last to be deleted 
    } 
 
     
/* 
------------------------------------------------------------------------------- 
 
    ConstructL(); 
 
    Description: 2nd phase Constructor. Setting up attributes, construction 
                 continues in CompleteConstructL() below. 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
void CRemoteCamAppUi::ConstructL() 
    { 
    BaseConstructL(); 
 
 
    // Create CMsvSession 
    iSession = CMsvSession::OpenAsyncL(*this); // new session is opened asynchronously 
                                               // CompleteConstructL() is called when async finishes 
 
    // Averell view launching 
    CRemoteCamFormView* view=new(ELeave)CRemoteCamFormView(); 
    CleanupStack::PushL(view); 
    view->ConstructL(); 
     
    AddViewL(view);      // add created view to this AppUi 
     
    CleanupStack::Pop(); // view 
 
    ActivateLocalViewL( view->Id() );   // activate view 
    } 
 
 
/* 
----------------------------------------------------------------------------- 
 
    CompleteConstructL() 
 
    Creates client MTM registry when session is ready for use.  
    This completes model construction and is called after 'server 
    ready' event is received from async opening of CMsvSession. 
 
----------------------------------------------------------------------------- 
*/ 
 
void CRemoteCamAppUi::CompleteConstructL() 
    { 
    // We get a MtmClientRegistry from our session 
    // this registry is used to instantiate new mtms. 
    iMtmReg = CClientMtmRegistry::NewL(*iSession); 
    iMtm = iMtmReg->NewMtmL(KUidMsgTypeSMS);        // create new SMS MTM 
    } 
 
 
 
/* 
------------------------------------------------------------------------------- 
 
    HandleKeyEventL 
 
    Description: Handles a key event (we are monitoring for the navibutton) 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
TKeyResponse CRemoteCamAppUi::HandleKeyEventL(const TKeyEvent& aKeyEvent,TEventCode /*aType*/) 
    { 
    TInt code = aKeyEvent.iCode; 
    switch(code) 
        { 
    	case EKeyDevice3: 
            // take a preview picture 
            // camera is turned on/off in the TakePreviewL() function 
            static_cast(View(KRemoteCamViewId))->TakePreviewL();      
            return (EKeyWasConsumed); 
            break; 
 
        default: 
            break; 
        } 
 
    return(EKeyWasNotConsumed); // default return value 
    } 
 
 
/* 
------------------------------------------------------------------------------- 
 
    HandleCommandL 
 
    Description: Handles a given command 
 
    Return value: N/A 
 
------------------------------------------------------------------------------- 
*/ 
void CRemoteCamAppUi::HandleCommandL(TInt aCommand) 
    { 
    switch (aCommand)						// A switch statement that will be controlled by the command id get from the OfferKayEvent 
		{ 
        case EAknSoftkeyExit:               // If the exit command is selected 
 
		case EEikCmdExit:					// If the exit command is selected 
         
        case EClose:						// If the exit command is selected 
			CBaActiveScheduler::Exit();		// Call the CBaActiveScheduler's Exit function that stops the application's thread and destroys it. 
			break; 
 
        // "Take picture" was selected from the Options menu 
        case ERemoteCamTakePicture: 
            static_cast(View(KRemoteCamViewId))->TurnCameraOnL(); 
            static_cast(View(KRemoteCamViewId))->TakePictureL(); 
            static_cast(View(KRemoteCamViewId))->TurnCameraOffL(); 
            break; 
 
        // "Save image" was selected from the Options menu 
        case ERemoteCamSave: 
            // Then ask the view to save the current picture (can save the snapshot, if current bmp) 
            static_cast(View(KRemoteCamViewId))->SaveImageL(); 
            break; 
 
        default:                            // If the menu command is none of the above do nothing 
            break; 
		} 
 
    } 
 
 
/* 
----------------------------------------------------------------------------- 
 
    HandleSessionEventL() 
 
    Handles session event observer and calls event handling functions in 
    observer.  
    The type of event is indicated by the value of aEvent. The  
    interpretation of the TAny arguments depends on this type. For most  
    event types, the action that is taken, for example, updating the  
    display, is client-specific. All clients though should respond to  
    EMsvCloseSession and EMsvServerTerminated events.  
 
----------------------------------------------------------------------------- 
*/ 
void CRemoteCamAppUi::HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* /*aArg3*/) 
    { 
 
    switch (aEvent) 
        { 
 
        case EMsvEntriesCreated:        // A new entry has been created in the message server 
            // We are interested in messages that are created in Inbox 
            TMsvId* parent; 
            parent = static_cast(aArg2);          // entry id from the session event 
 
            if ( *parent == KMsvGlobalInBoxIndexEntryId )  // new entry has been created in Inbox folder 
                { 
                // We take the created entry ids into a selection 
                CMsvEntrySelection* entries = static_cast(aArg1); 
 
                //Process each created entry, one at a time. 
                for(TInt i = 0; i < entries->Count(); i++) 
                    { 
                    iMtm->SwitchCurrentEntryL(entries->At(i)); 
                    TMsvEntry entry = iMtm->Entry().Entry();  
                    if( entry.iBioType == KUidRemoteCamMsg.iUid ) // message is our BIO message 
                        { 
                        // First turn on the camera 
                        static_cast(View(KRemoteCamViewId))->TurnCameraOnL(); 
 
                        // if message has commands, for an example save with filename "xxx", 
                        // read message here 
 
                        // delete message from server 
                        iMtm->SwitchCurrentEntryL(*parent); 
                        iMtm->Entry().DeleteL( entries->At(i) ); 
                         
                        // Then ask the Camera to take a picture 
                        static_cast(View(KRemoteCamViewId))->TakePictureL(); 
 
                        // Then turn off the camera 
                        static_cast(View(KRemoteCamViewId))->TurnCameraOffL(); 
                        } 
                    } 
                } 
            break; 
         
        // This event tells us that the messaging server session has been opened 
        case EMsvServerReady: 
            CompleteConstructL();       // Construct the mtm registry & sms mtm 
            break; 
 
        default: 
            // All other events are ignored 
            break; 
        } 
 
    } 
 
 
 
 
// 
//  CRemoteCamDocument 
// 
 
/* 
----------------------------------------------------------------------------- 
 
    CRemoteCamDocument 
 
    Creates new RemoteCam document. 
 
    Return Values:  N/A 
 
----------------------------------------------------------------------------- 
*/ 
CRemoteCamDocument::CRemoteCamDocument(CEikApplication& aApp) 
    : CEikDocument(aApp) 
    {} 
 
 
 
/* 
----------------------------------------------------------------------------- 
 
    CreateAppUiL 
 
    Creates new CRemoteCamAppUi and returns a pointer to it. 
 
    Return Values:  pointer to CRemoteCamAppUi 
 
----------------------------------------------------------------------------- 
*/ 
CEikAppUi* CRemoteCamDocument::CreateAppUiL() 
    {	  
    CRemoteCamAppUi* appui = new (ELeave) CRemoteCamAppUi; 
    return appui; 
    }	 
 
 
 
// 
//  CRemoteCamApplication 
// 
 
/* 
----------------------------------------------------------------------------- 
 
    CreateDocumentL() 
 
    Description: Creates new RemoteCam document. 
 
    Return Values:  pointer to new CRemoteCamDocument object 
 
----------------------------------------------------------------------------- 
*/ 
CApaDocument* CRemoteCamApplication::CreateDocumentL() 
    { 
    return new (ELeave) CRemoteCamDocument(*this); 
    } 
 
 
/* 
----------------------------------------------------------------------------- 
 
    AppDllUid() 
 
    Return the UID of RemoteCam application. 
 
    Return Values:  UID 
 
----------------------------------------------------------------------------- 
*/ 
TUid CRemoteCamApplication::AppDllUid() const 
    { 
    return KUidRemoteCamApp; 
    } 
 
 
 
 
//  OTHER EXPORTED FUNCTIONS 
//============================================================================= 
 
 
/* 
----------------------------------------------------------------------------- 
 
    NewApplication 
 
    Creates new CRemoteCamApplication application and returns a pointer to it. 
 
    Return Values:  pointer to CApaApplication 
 
----------------------------------------------------------------------------- 
*/ 
EXPORT_C CApaApplication* NewApplication() 
    { 
    return new CRemoteCamApplication; 
    } 
 
 
 
/* 
----------------------------------------------------------------------------- 
 
    E32Dll 
 
    Called when the DLL is loaded and unloaded. 
 
    Return Values:  KErrNone 
 
----------------------------------------------------------------------------- 
*/ 
GLDEF_C TInt E32Dll(TDllReason /*aReason*/) 
    { 
    return KErrNone; 
    } 
 
 
 
 
//  End of File