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

📄 camtimer.cpp

📁 symbian s60 2nd下的手机定时拍照
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*

        CamTimer.CPP - source file for CamTimer application, Series 60
        C++ implementation

History:
Version 1.10, October 17th 2002
Added features: Snap sound, Launching Photo album from menu for 
				further use of the picture 
				and an updated pkg file for Series 60

Version 1.0, December 1st 2001
CamTimer Released in Forum Nokia

*/


//  Include Files
#include <e32base.h>			// for the CleanupStack
#include <aknapp.h>             // for CAknApplication
#include <akndoc.h>             // for CAknDocument
#include <aknviewappui.h>		// for CAknViewAppUi
#include <aknview.h>            // for CAknView
#include <eikenv.h>				// for CEikonEnv - "Eikon Environment"
#include <akndialog.h>			// for the dialog
#include <eikmfne.h>			// for CEikNumberEditor
#include <CameraServ.h>			// for RCameraServ
#include <MdaImageConverter.h>  // for MMdaImageUtilObserver
#include <f32file.h>			// for handling files
#include <aknsoundsystem.h>		// for playing keysounds - snap

#include "CamTimer.h"			// own header
#include "CamTimer.hrh"			// own enumerations
#include <CamTimer.rsg>			// own resources


// Constants
// CamTimer application UID
const TUid KUidCamTimerApp = { 0x101F5462 };
// UID of CamTimer view
const TUid KCamTimerViewId = { 1 };
// Quality factor for JPG saving
const TInt KJpgSavingQualityFactor = 55;           
// Initial value for waiting time.
const TInt KInitialValue=10;   
// UID of Photo album application
const TUid KPhotoAlbumUid = { 0x101F4CD1 };


_LIT(KWelcomeText,"Welcome");
_LIT(KWelcomeText1,"to");
_LIT(KWelcomeText2,"CamTimer");
_LIT(KThumbnailFilename, "C:\\Nokia\\Images_tn\\CamTimerPicture.jpg");
_LIT(KCamTimerFilename, "C:\\Nokia\\Images\\CamTimerPicture.jpg");

// Definitions
#define KSnapSoundId 2

//
// CCamTimerContainer 
// This container does not have any controls in it, it is only used 
// to draw an image.
//

/*
-------------------------------------------------------------------------------

    ConstructL();

    Description: 2nd phase Constructor.

    Return value: N/A

-------------------------------------------------------------------------------
*/
void CCamTimerContainer::ConstructL(CCamTimerFormView& aFormView)
    {
    iBmp = new (ELeave) CFbsBitmap;
	// take a handle to the object owninf this container
    iFormView = &aFormView;
	// flag: image ready to be drawn or not
    iImageReady = EFalse;    
	// Construct self
    ConstructContainerControlL();
    }


/*
-------------------------------------------------------------------------------

    ~CCamTimerContainer();

    Description: Destructor.

    Return value: N/A

-------------------------------------------------------------------------------
*/
CCamTimerContainer::~CCamTimerContainer()
    {
    delete iBmp;
    iFormView = NULL;
    }


/*
-------------------------------------------------------------------------------

    ConstructContainerControlL();

    Description: Constructing container control

    Return value: N/A

-------------------------------------------------------------------------------
*/
void CCamTimerContainer::ConstructContainerControlL()
    {
	// Makes the control window owning
    CreateWindowL(); 
    }


/*
-------------------------------------------------------------------------------

    Draw();

    Description: This function draws the application view on the screen.

    Return value: N/A

-------------------------------------------------------------------------------
*/
void CCamTimerContainer::Draw(const TRect& /*aRect*/) const
    {
	// Get the graphics context in which to draw.
    CGraphicsContext& gc=SystemGc();
    
    if(iImageReady)
        {
		// draw our picture
        gc.DrawBitmap( Rect(), iBmp);
		// Draw a rectangle.
        gc.DrawRect(Rect());     
        }
    else
        {
        // no image to draw, draw a text saying "Welcome to CamTimer" 
		// 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 "Welcome to CamTimer"
        gc.SetPenColor(KRgbBlack); 
        const CFont* fontUsed = iEikonEnv->TitleFont();
        gc.UseFont(fontUsed);

		// set text position on screen
        TInt baseline = Rect().Height() - fontUsed->AscentInPixels()*3;
		// margin is zero so that the text will be cenetred
        TInt margin=0; 

        gc.DrawText(KWelcomeText2,Rect(),baseline,CGraphicsContext::ECenter, 
																		margin);
		// set text position on screen
		baseline = Rect().Height() - fontUsed->AscentInPixels()*5; 
		margin=0;
        gc.DrawText(KWelcomeText1,Rect(),baseline,CGraphicsContext::ECenter, 
																		margin);
		// set text position on screen
		baseline = Rect().Height() - fontUsed->AscentInPixels()*7; 
		margin=0;
        gc.DrawText(KWelcomeText,Rect(),baseline,CGraphicsContext::ECenter, 
																		margin);
        
        }
    }


/*
-------------------------------------------------------------------------------

    GetBmp();

    Description: This function returns a pointer to the bitmap.

    Return value: N/A

-------------------------------------------------------------------------------
*/
CFbsBitmap* CCamTimerContainer::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* CCamTimerContainer::GetBmpForSaving()
    {
    // this fuction is used to get the current image for saving
    // so no reseting this time
    return iBmp;
    }



//
// CCamTimerFormView
//

/*
-------------------------------------------------------------------------------

    ConstructL();

    Description: Default C++ Constructor.

    Return value: N/A

-------------------------------------------------------------------------------
*/
CCamTimerFormView::CCamTimerFormView(TInt* aValue)	// C++ constructor
    : iWaitingTime(aValue)                          // Initialise data
    {
    }


/*
-------------------------------------------------------------------------------

    ConstructL();

    Description: 2nd phase Constructor.

    Return value: N/A

-------------------------------------------------------------------------------
*/
void CCamTimerFormView::ConstructL()
    {
    // Current image index starts from zero (could used when saving images 
	// (not used at the moment))
    iCurrentImage = 0;
	// flag to tell when the app is saving the image asynchronously 
	// (can't take a new pict when saving) 
    iSavingImage = EFalse;  

    // Initialise a client to the camera server
    iCamserv = new(ELeave) RCameraServ;
    
    // Init a file saver utility
    iFileSaver = CMdaImageBitmapToFileUtility::NewL(*this);
    iFormat = new (ELeave) TMdaJfifClipFormat;

	// Create player for snap sound
    iSoundPlayer = (STATIC_CAST(CAknAppUi*,
									CEikonEnv::Static()->AppUi()))->KeySounds();
    TRAPD(error, iSoundPlayer->AddAppSoundInfoListL( R_CAMERA_SNAP_SOUND ));
    if ( ( error != KErrAlreadyExists ) && ( error != KErrNone) )
        {
        User::LeaveIfError(error);
        }

    }


/*
-------------------------------------------------------------------------------

    ~CCamTimerFormView();

    Description: Destructor.

    Return value: N/A

-------------------------------------------------------------------------------
*/
CCamTimerFormView::~CCamTimerFormView()
    {
    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 CCamTimerFormView::Id() const
    {
    return KCamTimerViewId;
    }


/*
-------------------------------------------------------------------------------

    DoActivateL();

    Description: Activate this view.

    Return value: N/A

-------------------------------------------------------------------------------
*/
void CCamTimerFormView::DoActivateL(const TVwsViewId& /*aPrevViewId*/, 
	 TUid /*aCustomMessageId*/, const TDesC8& /*aCustomMessage*/ )
    {
    // Connect to Camera Server
    User::LeaveIfError(iCamserv->Connect());
    
    if (!iContainer) // if container hasn't been created yet, let's create it
        {
        // Then construct the UI components
        iContainer = new(ELeave) CCamTimerContainer;             
		// Construct a view control with a ref. to this CCamTimerFormView
        iContainer->ConstructL(*this);      
        // Sets view control's extent to the space available
        iContainer->SetRect(ClientRect()); 
        }

	// Activate the view control
    iContainer->ActivateL();   
    }


/*
-------------------------------------------------------------------------------

    DoDeactivate();

    Description: Deactivate this view.

    Return value: N/A

-------------------------------------------------------------------------------
*/
void CCamTimerFormView::DoDeactivate()
    {
    if (iContainer)
        {
        delete iContainer;
        iContainer = NULL;
        }

    // Disconnect from Camera server
    if(iCamserv)
        {
        iCamserv->Close();
        }

    }

// ---------------------------------------------------------------------------
// CCamTimerFormView::HandleForegroundEventL(TBool aForeground)
// Can be used to open and close the camera depending on the foreground event
// ---------------------------------------------------------------------------
void CCamTimerFormView::HandleForegroundEventL(TBool /*aForeground*/)
    {
	}

/*
-------------------------------------------------------------------------------

    TimerL();

    Description: Do timing showing a series of preview pictures. 
				 Take picture and save it.
    Return value: N/A

-------------------------------------------------------------------------------
*/
void CCamTimerFormView::TimerL()
    {
    if(!iSavingImage)
        {  

        // turn on camera
        TurnCameraOnL();

		// For timing
		TTime begin;
		TTime end;
		TTimeIntervalMicroSeconds fromBeginToEndMicroseconds ;
		fromBeginToEndMicroseconds =0;
		begin.HomeTime();
		TTimeIntervalMicroSeconds maxtime;
		maxtime = *iWaitingTime;
		// Loop preview pictures unti given time has reached.
		while (fromBeginToEndMicroseconds <maxtime)
			{
			// Call TakePictureL() to take a low quality image
			TakePictureL(ERCLow);
			end.HomeTime();
		    fromBeginToEndMicroseconds  = end.MicroSecondsFrom(begin);
			}

		// Finally, take the picture
		TakePictureL();

		// Play the camera snap -sound
	    if ( iSoundPlayer )
		    {
			iSoundPlayer->PlaySound(KSnapSoundId);
			}

		// Save the final picture
		SaveImageL();

		// 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 CCamTimerFormView::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 CCamTimerFormView::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 CCamTimerFormView::TakePictureL(TCamTimerImageQuality 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));

    // 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

⌨️ 快捷键说明

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