⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 remotecam.cpp

📁 Symbian S60 v2 官方远程摄像头程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*

        RemoteCam.CPP - source file for RemoteCam application Averell
        C++ implementation


*/


//  Include Files
#include <e32base.h>		// for the CleanupStack
#include <e32def.h>         // for TBool enums
#include <eikappui.h>       // for ClientRect, CreateAppUiL
#include <eikenv.h>			// for CEikonEnv - "Eikon Environment"
#include <eikdef.h>			// for UIKON definitions
#include <aknviewappui.h>   // for CAknViewAppUi
#include <basched.h>        // for Active Scheduler
#include <eikon.hrh>        // for common enumerations
#include <avkon.hrh>        // for Averell enumerations
#include <smut.h>           // for CSmsMessageSettings
#include <CameraServ.h>     // for RCameraServ
#include <msvapi.h>         // for CMsvSession
#include <mtclbase.h>       // for CBaseMtm
#include <mtclreg.h>        // for CClientMtmRegistry
#include <e32std.h>         // for RNotifier

#include "RemoteCam.h"                  // own header
#include "RemoteCam.hrh"                // own enumerations
#include <RemoteCam.rsg>                // 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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -