filmreel2container.cpp

来自「最新官方例子,图形,描述副,基本控件,通讯协议,等等,」· C++ 代码 · 共 562 行

CPP
562
字号
/**
*
* @brief Definition of CFilmReel2Container
*
* Copyright (c) EMCC Software Ltd 2003
* @version 1.0
*/

// INCLUDE FILES

// Class include
#include "FilmReel2Container.h"

// System includes
#include <aknnavi.h>			// CAknNavigationControlContainer
#include <aknnavide.h>			// CAknNavigationDecorator
#include <aknnavilabel.h>		// CAknNaviLabel
#include <aknnotewrappers.h>	// CAknErrorNote
#include <AknAppUi.h>			// CEikButtonGroupContainer
#include <eikapp.h>				// CEikApplication
#include <palbsettings.h>		// TPAlbSettings
#include <palbimagefactory.h>	// KWidth_VGA && KHeight_VGA
#include <StringLoader.h>		// StringLoader

// User includes
#include "CameraTimer.h"		// CCameraTimer
#include <filmreel2.rsg>

// CONSTANTS

const TInt KNumberOfPicturesInRow = 4;
const TInt KNumberOfPicturesInColumn = 4;
const TInt KTotalNumberOfPictures = KNumberOfPicturesInRow * KNumberOfPicturesInColumn;
const TInt KHorizontalDistance = KWidth_VGA / KNumberOfPicturesInRow;
const TInt KVerticalDistance = KHeight_VGA / KNumberOfPicturesInColumn;

_LIT(KDefaultFileName, "FilmReel2.jpg");

const TInt KJpgSavingQualityFactor = 55; // Quality factor for JPG saving
const TInt KFractionOfFontSizeToLowerText = 3;


// ================= MEMBER FUNCTIONS =======================

/**
* Symbian OS 2nd phase constructor.  Creates a Window for the controls, which it contains.
* Constructs a label and adds it to the window, which it then activates.
* @param aRect The rectangle for this window
*/
void CFilmReel2Container::ConstructL(const TRect& aRect)
	{
	iCamera = CCamera::NewL(*this, 0);
	iCamera->Reserve();

    iBitmap = new (ELeave) CFbsBitmap();
	iCollageBitmap = new (ELeave) CFbsBitmap();
	User::LeaveIfError(iCollageBitmap->Create(TSize(KWidth_VGA, KHeight_VGA), EColor4K));

	iBitmapDevice = CFbsBitmapDevice::NewL(iCollageBitmap);
	User::LeaveIfError(iBitmapDevice->CreateContext(iBGc));

	// Init a file saver utility
	iFileSaver = CMdaImageBitmapToFileUtility::NewL(*this);
	iFormat = new (ELeave) TMdaJfifClipFormat;

	// Camera Timer
	iCameraTimer = CCameraTimer::NewL(*this);

	// Text strings
	iNotReadyText = StringLoader::LoadL(R_NOT_READY_TEXT);
	iTimeOutText = StringLoader::LoadL(R_TIMED_OUT_TEXT);

	// Fetch pointer to the default navi pane control
	CEikStatusPane* sp = ((CAknAppUi*)iEikonEnv->EikAppUi())->StatusPane();
	iNaviPane = (CAknNavigationControlContainer*)sp->ControlL(TUid::Uid(EEikStatusPaneUidNavi));
	iNaviDecoratorForLabel = iNaviPane->ResourceDecorator();

	if (iNaviDecoratorForLabel)
		{
		iNaviLabel=(CAknNaviLabel*)iNaviDecoratorForLabel->DecoratedControl();
		iNaviLabel->SetNaviLabelType(CAknNaviLabel::EAdditionalInfoLabel);
		}

	SetNaviLabelTextL(R_NOT_READY_TEXT);

	CreateWindowL();
	SetRect(aRect);
	ActivateL();
	}

/**
* Symbian OS 2 phase constructor.
* Constructs the CFilmReel2Container using the NewLC method, popping
* the constructed object from the CleanupStack before returning it.
*
* @param aRect The rectangle for this window
* @return The newly constructed CFilmReel2Container
*/
CFilmReel2Container* CFilmReel2Container::NewL(const TRect& aRect)
	{
	CFilmReel2Container* self = CFilmReel2Container::NewLC(aRect);
	CleanupStack::Pop(self);
	return self;
	}

/**
* Symbian OS 2 phase constructor.
* Constructs the CFilmReel2Container using the constructor and ConstructL
* method, leaving the constructed object on the CleanupStack before returning it.
*
* @param aRect The rectangle for this window
* @return The newly constructed CFilmReel2Container
*/
CFilmReel2Container* CFilmReel2Container::NewLC(const TRect& aRect)
	{
	CFilmReel2Container* self = new (ELeave) CFilmReel2Container;
	CleanupStack::PushL(self);
	self->ConstructL(aRect);
	return self;
	}

/**
* Destructor.  Frees up memory.
*/
CFilmReel2Container::~CFilmReel2Container()
	{
	if (iBitmapViewer)
		{
		iBitmapViewer->Reset();
		delete iBitmapViewer;
		}
	if (iCamera)
		{
		iCamera->PowerOff();
		iCamera->Release();
		delete iCamera;
		}
	delete iBitmap;
	delete iCollageBitmap;
	delete iBGc;
	delete iBitmapDevice;

	delete iFileSaver;
	delete iFormat;

	delete iCameraTimer;

	delete iNotReadyText;
	delete iTimeOutText;

	delete iNaviDecoratorForLabel;
	}

/**
* Provides user feedback using the navigation pane for displaying messages.
* @param aTextResourceId the resource ID of the message to display
*/
void CFilmReel2Container::SetNaviLabelTextL(TInt aTextResourceId)
	{
	HBufC* text = StringLoader::LoadLC(aTextResourceId);
	SetNaviLabelTextL(*text);
	CleanupStack::PopAndDestroy(text); // text
	}

/**
* Provides user feedback using the navigation pane for displaying messages.
* @param aText the text message to display
*/
void CFilmReel2Container::SetNaviLabelTextL(const TDesC& aText)
	{
	if (iNaviLabel)
		{
		iNaviLabel->SetTextL(aText);
		iNaviPane->DrawNow();
		}
	}

/**
* Called by the framework to draw this control.  Clears the area in
* aRect. Draws preview bitmap while previewing otherwise draws the
* created collage bitmap.
* @param aRect in which to draw
*/
void CFilmReel2Container::Draw(const TRect& aRect) const
	{
	CWindowGc& gc = SystemGc();
	gc.Clear(aRect);

	switch(iContainerState)
		{
		case EPreviewing:
			gc.DrawBitmap(Rect(), iBitmapViewer, iBitmapViewer->SizeInPixels());
			break;
		case ERecording:
		case EDisplayingResult:
		case ESavingImage:
			gc.DrawBitmap(Rect(), iCollageBitmap, iCollageBitmap->SizeInPixels());
			break;
		case EPoweredDown:
			DisplayMessage(*iTimeOutText, gc);
			break;
		case ENotReady:
		default:
			DisplayMessage(*iNotReadyText, gc);
			break;
		}
	}

/**
* Called by the framework to draw this control.  Clears the area in
* aRect. Draws preview bitmap while previewing otherwise draws the
* created collage bitmap.
* @param aMessage string to display
* @param aGc Window graphic context
*/
void CFilmReel2Container::DisplayMessage(const TDesC& aMessage, CWindowGc& aGc) const
	{
	// Draw white background
	aGc.SetBrushStyle(CGraphicsContext::ESolidBrush);
	aGc.SetBrushColor(KRgbWhite);
	aGc.DrawRect(Rect());

	// Draw black outline
	aGc.SetBrushStyle(CGraphicsContext::ENullBrush);
	aGc.SetBrushColor(KRgbBlack);
	aGc.DrawRect(Rect());

	aGc.SetPenColor(KRgbBlack);
	const CFont* fontUsed = iEikonEnv->TitleFont();
	aGc.UseFont(fontUsed);

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

	aGc.DrawText(aMessage, Rect(), baseline, CGraphicsContext::ECenter, margin);
	}

void CFilmReel2Container::PowerOnCompleteL()
	{
	SetNaviLabelTextL(R_PREVIEWING_TEXT);
	TSize size;

	// Configure the camara
	iCamera->PrepareImageCaptureL(CCamera::EFormatFbsBitmapColor4K, 1);
	iCamera->EnumerateCaptureSizes(size, 1, CCamera::EFormatFbsBitmapColor4K);

	// Start the viewfinder.
	iCamera->StartViewFinderBitmapsL(size);
	}

void CFilmReel2Container::ViewFinderFrameReadyL(CFbsBitmap& aBitMap)
	{
	if (iBitmapViewer)
		{
		iBitmapViewer->Reset();
		delete iBitmapViewer;
		iBitmapViewer = NULL;
		}

	// Sets the viewer bitmap image to be displayed
	iBitmapViewer = new (ELeave) CFbsBitmap();
	iBitmapViewer->Duplicate (aBitMap.Handle());
	iContainerState = EPreviewing;
	DrawNow();
	}

void CFilmReel2Container::ImageReadyL(CFbsBitmap* aBitmap, HBufC8* /*aData*/)
	{
	delete iBitmap;
	iBitmap = aBitmap;
	// picture has been taken, so display the bitmap
	DrawNow();

	switch (iContainerState)
		{
		case ERecording:
			{
			// copy image onto the collage bitmap
			iBGc->BitBlt(TPoint(iX * KHorizontalDistance, iY * KVerticalDistance), iBitmap);

			iCount++;
			if (iCount == KTotalNumberOfPictures)
				{
				// When the collage bitmap is filled, reset the
				// iCount to 0, and set iX and iY back to the top-corner
				iCount = 0;
				iX = 0;
				iY = 0;

				iContainerState = ESavingImage; // set flag, can't take new pictures when saving
				SetNaviLabelTextL(R_SAVING_TEXT);
				SaveImageL(); // Save the image to file
				}
			else
				{
				iX++; // Increase the horizintal position of the next image
				if (iX == KNumberOfPicturesInRow)
					{
					// If at the end of a row, move to the beginning of the next row.
					iY++;
					iX = 0;
					}
				// take next picture.
				RequestPictureAfterPause();
				}
			}
			break;
		case EDisplayingResult:
			// No need to do anything here, if we are displaying the created image
			break;
		default:
			break;
		};
	}

/**
* Observer method for the Camera Server operations.
* Gets called when the iCamera->Reserve() method has completed.
* @param aError the error value
*/
void CFilmReel2Container::ReserveComplete(TInt aError)
	{
	if (aError == KErrNone)
		{
		// Once the camera has been successfully reserved, the
		// application will power up the camera.
		iCamera->PowerOn();
		}
	}

/**
* Observer method for the Camera Server operations.
* Gets called when the iCamera->PowerOn() method has completed.
* @param aError the error value
*/
void CFilmReel2Container::PowerOnComplete(TInt aError)
	{
	if (aError == KErrNone)
		{
		TRAPD (err, PowerOnCompleteL());
		}
	}

/**
* Observer method for the Camera Server operations.
* Gets called when the iCamera->StartViewFinderBitmapsL() method has completed.
* @param aBitMap the image from the view finder.
*/
void CFilmReel2Container::ViewFinderFrameReady(CFbsBitmap& aBitMap)
	{
	TRAPD (err, ViewFinderFrameReadyL(aBitMap));
	}

/**
* Observer method for the Camera Server operations.
* Gets called when the iCamera->CaptureImage() when has completed.
* @param aBitmap the image captured.
* @param aData the image data captured (not specified for this example)
* @param aError the error value
*/
void CFilmReel2Container::ImageReady(CFbsBitmap* aBitmap, HBufC8* aData, TInt aError)
	{
	if (aError == KErrNone && aBitmap)
		{
		TRAPD (err, ImageReadyL(aBitmap, aData));
		}
	}


void CFilmReel2Container::FrameBufferReady(MFrameBuffer* /*aFrameBuffer*/, TInt /*aError*/)
	{
	}

/**
* Returns the current state of the container
* @return the current state of the FilmReel2container
*/
CFilmReel2Container::TContainerState CFilmReel2Container::CurrentState() const
	{
	return iContainerState;
	}

/**
* Changes the container back into previewing mode,
* updating the softkeys and starts the preview loop
*/
void CFilmReel2Container::PreviewModeL()
	{
	if (iContainerState == EPoweredDown)
		{
		iCamera->Reserve();
		}

	if (iContainerState == EDisplayingResult)
		{
		SetNaviLabelTextL(R_PREVIEWING_TEXT);

		CEikButtonGroupContainer* cba = iAvkonAppUi->Cba();
		cba->SetCommandSetL(R_AVKON_SOFTKEYS_OPTIONS_EXIT);

		TSize size;
		iCamera->EnumerateCaptureSizes(size, 1, CCamera::EFormatFbsBitmapColor4K);
		iCamera->StartViewFinderBitmapsL(size);
		}
	}

/**
* Changes the container into recording mode, clearing the collagebitmap
* and slowing down the camera timer.
*/
void CFilmReel2Container::RecordModeL()
	{
	if (iContainerState == EPreviewing)
		{
		iCamera->StopViewFinder();
		if (iBitmapViewer)
			{
			iBitmapViewer->Reset();
			delete iBitmapViewer;
			iBitmapViewer = NULL;
			}

		iContainerState = ERecording; // switch to recording
		SetNaviLabelTextL(R_RECORDING_TEXT);

		// Clear  the collageBitmap to a white background
		iBGc->SetBrushColor(KRgbWhite);
		iBGc->Clear();

		// Slow down the timer for recording
		iCameraTimer->SetDelay(KFilmReel2Speed);
		RequestPictureAfterPause();
		}
	}

/**
* Changes the container state to displaying and updates the softkeys
*/
void CFilmReel2Container::DisplayModeL()
	{
	if (iContainerState == ESavingImage)
		{
		// Extract filename from full path
		iContainerState = EDisplayingResult;
		TParse parseToImageName;
		parseToImageName.Set(iFileName, NULL, NULL);
		iImageName = parseToImageName.Name();
		SetNaviLabelTextL(iImageName);

		CEikButtonGroupContainer* cba = iAvkonAppUi->Cba();
		cba->SetCommandSetL(R_FILMREEL2_SOFTKEYS_OPTIONS_DELETE);
		cba->DrawDeferred();
		}
	}

/**
* Returns a reference to the filename of the last image saved
*/
const TFileName& CFilmReel2Container::FileName() const
	{
	return iFileName;
	}

/**
* Returns a reference to the name of the last image saved
*/
const TFileName& CFilmReel2Container::ImageName() const
	{
	return iImageName;
	}

/**
* Saves the current collageBitmap to the phone album
*/
void CFilmReel2Container::SaveImageL()
	{
	// Be sure that we have an image to be saved
	HBufC* imageroot = TPAlbSettings::RootImageFolderLC(); // gets the name of the phone album folder
	iFileName = *imageroot;
	iFileName.Append(KDefaultFileName);

	// generate filename using the framework's file server connection
	CEikApplication::GenerateFileName(iCoeEnv->FsSession(), iFileName);
	CleanupStack::PopAndDestroy(imageroot);

	// Create Format and Codec
	iFormat->iSettings.iQualityFactor = KJpgSavingQualityFactor; // quality factor from 0 to 100 (55 used here)
	iFormat->iSettings.iSampleScheme = TMdaJpgSettings::TColorSampling(TMdaJpgSettings::EColor420);

	// Create a file to save the image into
	// this is done in an asynch function
	iFileSaver->CreateL(iFileName, iFormat, NULL, NULL);
	}

/**
* This function is called when the asynchronous function
* iFileSaver->CreateL() completes. Successfully or not. 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.
*
* @param aError the error value
*/
void CFilmReel2Container::MiuoCreateComplete(TInt aError)
	{
	if ((aError == KErrNone) && iFileSaver)
		{
		// TRAP is used because this function cannot leave (no trailing L; name inherited from observer)
		// any error is ignored
		TRAPD(error, iFileSaver->ConvertL(*(iCollageBitmap)));
		}
	}

/**
* This function is called when the asynchronous function
* iFileSaver->ConvertL() completes. Now the image saving has
* completed.
*
* @param aError the error value
*/
void CFilmReel2Container::MiuoConvertComplete(TInt aError)
    {
	if (!aError)
		{
		// TRAP is used because this function cannot leave (no trailing L; name inherited from observer)
		// any error is ignored
		TRAPD(error, DisplayModeL());
		}
	}

/**
 * Observer method for the Media Server operations.
 * Gets called when the CMdaImageFileToBitmapUtility has opened
 * an image file. Not used here.
 *
 * @param aError error number, e.g. KErrNone if an error occured
 */
void CFilmReel2Container::MiuoOpenComplete(TInt /*aError*/)
	{
	}

/**
* Starts the camera timer
*/
void CFilmReel2Container::RequestPictureAfterPause()
	{
	iCameraTimer->StartTimer();
	}

/**
* Called by the camera timer, when the requested time has elasped
* makes a call to the cameramanager to take a picture
*/
void CFilmReel2Container::TimerCompleted()
	{
	iBitmap->Reset();
	iCamera->CaptureImage();
	}

// End of File

⌨️ 快捷键说明

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