欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

cameraview.cpp

《UIQ 3 The Complete Guide》书的源代码
CPP
第 1 页 / 共 2 页
字号:
//
// CameraView.cpp - Camera support
//
// Copyright (C) UIQ Technology AB, 2007
//
// This material is provided "as is" without any warranty to its performance or functionality. 
// In no event shall UIQ Technology be liable for any damages whatsoever arising out of the
// use or inabilty to use this material. 
//

#include "CameraView.h"
#include "SignedAppUids.h"
#include "SignedApp.hrh"
#include <SignedApp_0x20000462.rsg>
#include <QikCommand.h>
#include <QikAppUi.h>
#include <QikApplication.h>
#include <Ecam.h>
#include <DeviceKeys.h>
	
// Comment in or out depending on whether you want to use 
// MCameraObserver or MCameraObserver2 interfaces

// #define USE_CAMERA_OBSERVER_INTERFACE	1

enum TCameraViewStates
	{
	ECameraStateClosed,
	ECameraStatePoweredOn,
	ECameraStateTakingPicture,
	};

class CCameraDisplayControl : public CCoeControl, 

#ifdef USE_CAMERA_OBSERVER_INTERFACE
								public MCameraObserver
#else
								public MCameraObserver2
#endif // USE_CAMERA_OBSERVER_INTERFACE

	{
protected:

#ifdef USE_CAMERA_OBSERVER_INTERFACE
	// from MCameraObserver
	void ReserveComplete(TInt aError);
	void PowerOnComplete(TInt aError);
	void ViewFinderFrameReady(CFbsBitmap& aFrame);
	void ImageReady(CFbsBitmap* aBitmap,HBufC8* aData,TInt aError);
	void FrameBufferReady(MFrameBuffer* aFrameBuffer,TInt aError);

#else

	// from MCameraObserver2
	void HandleEvent(const TECAMEvent& aEvent);
	void ViewFinderReady(MCameraBuffer& aCameraBuffer,TInt aError);
	void ImageBufferReady(MCameraBuffer& aCameraBuffer,TInt aError);
	void VideoBufferReady(MCameraBuffer& aCameraBuffer,TInt aError);

#endif // USE_CAMERA_OBSERVER_INTERFACE

	void HandlePointerEventL(const TPointerEvent& aPointerEvent);
public:
	// new methods
	~CCameraDisplayControl();
	CCameraDisplayControl(CAppEngine* aEngine);
	void ConstructL();
	void StartViewFinderL();
	void TakePictureL();
	void CloseCamera();

protected:
	CAppEngine* iEngine;
	CCamera* iCamera;
	TCameraViewStates iState;
	};

CCameraDisplayControl::~CCameraDisplayControl()
// We no longer want to use the image.. free up those resources
	{
	CloseCamera();
	}

CCameraDisplayControl::CCameraDisplayControl(CAppEngine* aEngine) :
	iEngine(aEngine)
	{}

void CCameraDisplayControl::ConstructL()
	{
	}

void CCameraDisplayControl::CloseCamera()
//
// Close down the Camera..
//
	{
	if (iCamera)
		{
		if (iState>=ECameraStatePoweredOn)
			iCamera->PowerOff();
		iCamera->Release();
		delete(iCamera);
	    iCamera=NULL;
		}
	iState=ECameraStateClosed;
	}


/////////////////////////////////////////////////////////////////////////
#ifdef USE_CAMERA_OBSERVER_INTERFACE
// The following methods are from the MCameraObserver observer interface

void CCameraDisplayControl::ReserveComplete(TInt aError)
// From MCameraObserver
	{
	if (aError==KErrNone)
		{
		iCamera->PowerOn(); // calls back PowerOnComplete()
		}
	else
		{
		TBuf<128>bb;
		iEikonEnv->Format128(bb,R_STR_CAMERA_RESERVE_ERROR,aError);
		iEikonEnv->InfoMsg(bb);
		CloseCamera();
		}
	}

void CCameraDisplayControl::PowerOnComplete(TInt aError)
// From MCameraObserver
	{
	TCameraInfo info;
	info.iImageFormatsSupported=0;

	if (aError==KErrNone)
		{
		iState=ECameraStatePoweredOn;
		iCamera->CameraInfo(info);
		TRAP(aError,

			// tell view finder draw directly to our scn space
			TRect rect(Rect());
			rect.Move(PositionRelativeToScreen());
			iCamera->StartViewFinderDirectL(iEikonEnv->WsSession(),*iEikonEnv->ScreenDevice(),Window(),rect);

			// jpeg happens to be only format supported on P990..
			iCamera->PrepareImageCaptureL(CCamera::EFormatJpeg,info.iNumImageSizesSupported-1);
			);
		}
	if (aError!=KErrNone)
		{
		TBuf<128>bb;
		// display of info.iImageFormatsSupported is as much an internal debug aid as anything else !
		iEikonEnv->Format128(bb,R_STR_CAMERA_POWER_ERROR,aError,info.iImageFormatsSupported);
		iEikonEnv->InfoMsg(bb);
		CloseCamera();
		}
	}

void CCameraDisplayControl::ViewFinderFrameReady(CFbsBitmap& aFrame)
// From MCameraObserver
	{
	}

void CCameraDisplayControl::ImageReady(CFbsBitmap* aBitmap,HBufC8* aData,TInt aError)
// From MCameraObserver
	{
	if (aError==KErrNone)
		{
		// derive the drive and path for this applications private folder - which is 
		// where we will create the image. e.g. "c:\private\20000462\"
		TFileName installedDrive(static_cast<CEikAppUi*>(iEikonEnv->AppUi())->Application()->AppFullName());
		TFileName path;
		iEikonEnv->FsSession().PrivatePath(path); // does not supply a drive letter
		TParse parse;
		parse.Set(path,&installedDrive,NULL);	// add drive here
#ifdef __WINS__
		path=parse.FullName();
		path[0]='C';	// else emul points it at Z: 
		parse.Set(path,NULL,NULL);
#endif

		// create "c:\private\20000462\Photo.jpg" as the file to store our photo in
		path=parse.DriveAndPath();
		_LIT(KPhotoJpg,"Photo.jpg");
		parse.Set(KPhotoJpg,&path,NULL);

		RFile file;
		aError=file.Replace(iEikonEnv->FsSession(),parse.FullName(),EFileShareExclusive|EFileWrite);
		if (aError==KErrNone)
			{
			aError=file.Write(*aData);
			file.Close();
			if (aError==KErrNone)
				{
				// tell the engine about a new entry
				TRAP(aError,
					iEngine->AddEntryL(parse.FullName(),EAppCategoryImage);
					);

				// only now is it fully saved + added to internal data structures..
				iEikonEnv->InfoMsg(R_STR_PHOTO_SAVED);
				}
			}

		// failed to write all the file, or update the engine, roll back to the file not existing
		if (aError!=KErrNone)
			iEikonEnv->FsSession().Delete(parse.FullName());
		}

	if (aError!=KErrNone)
		{
		TBuf<128>bb;
		iEikonEnv->Format128(bb,R_STR_CAMERA_IMAGE_ERROR,aError);
		iEikonEnv->InfoMsg(bb);
		}

	CloseCamera();

	// return to list view. If we have managed to successfully save the photo the list view
	// will be updated to contain the entry AND the highlight will be on the photo.jpg entry iff
	// were viewing a category that contains the image
	static_cast<CQikAppUi*>(iEikonEnv->AppUi())->ActivateViewL(KViewIdListView);
	}

void CCameraDisplayControl::FrameBufferReady(MFrameBuffer* aFrameBuffer,TInt aError)
// From MCameraObserver
	{
	}

#else
/////////////////////////////////////////////////////////////////////////
// The following methods are from the MCameraObserver2 observer interface
void CCameraDisplayControl::HandleEvent(const TECAMEvent& aEvent)
//
// From MCameraObserver2
//
	{
	TBuf<128>bb;
	if (aEvent.iEventType==KUidECamEventReserveComplete)
		{ // called back after a Reserve() completes
		if (aEvent.iErrorCode==KErrNone)
			{
			iCamera->PowerOn(); // calls back HandleEvent() with KUidECamEventPowerOnComplete
			}
		else
			{
			iEikonEnv->Format128(bb,R_STR_CAMERA_RESERVE_ERROR,aEvent.iErrorCode);
			iEikonEnv->InfoMsg(bb);
			CloseCamera();
			}
		}
	else if (aEvent.iEventType==KUidECamEventPowerOnComplete)
		{ // called back after a PowerOn() completes
		TInt err=aEvent.iErrorCode;

		// whilst the P990 only supports Jpeg other devices may not. Apps may wish to 
		// check which formats are supported and choose between them
		TCameraInfo info;
		info.iImageFormatsSupported=0;

		if (err==KErrNone)
			{
			iState=ECameraStatePoweredOn;
			iCamera->CameraInfo(info);

/*
Code you might use to enumerate supported formats and image sizes...
			TInt i;
			TInt mask=0x01;
			for (i=0;i<17;i++,mask<<=1)
				{
				if (info.iImageFormatsSupported&mask)
					{ // this format is supported...
					CCamera::TFormat format=(CCamera::TFormat)mask;

⌨️ 快捷键说明

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