×
Namespaces

Variants
Actions

如何捕捉图片功能

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

文章
User:Jiangbaolan 在 01 Apr 2009 创建
最后由 hamishwillee 在 15 Dec 2011 编辑

详细描述

下列代码演示了如何使用Camera API(ecam.lib)捕捉一张图片。一般来说,操作流程如下:

  • 1、生成一个camera controller作为UI和camera引擎的接口(这里放在CAppUi中)
  • 2、调用CCameraController::InitializeCameraL(),它将会初始化相机引擎,并暂时掌控相机以便下面的操作。
  • 3、掌控相机是一个异步的操作,完成后将会调用MCameraObserver::ReserveComplete()方法(或者就是CCameraEngine::ReserveComplete()方法的调用,因此CCameraEngine完成了MCameraObserver接口)
  • 4、相机开始启动(CCamera::PowerOn()),同样这也是异步的操作,最终它将调用MCameraObserver::PowerOnComplete()方法。
  • 5、PowerOnComplete()方法准备开始捕捉,并设置引擎为空闲状态
  • 6、当需要图片时,我们调用CCameraController::SnapL(),它将执行引擎的CCameraEngine::SnapL()命令,这才依次执行到实际的相机操作(CCamera::CaptureImage()方法)
  • 7、捕捉图片也是一个异步的过程,完成后将调用MCameraObserver::ImageReady()

MMP文件

需要下列链接库和能力

Code: CAPABILITY UserEnvironment LIBRARY ecam.lib 头文件: Camera.hrh

Code:

  1. ifndef __CAMERA_HRH_
  2. define __CAMERA_HRH_

enum TEngineState

   {
   EEngineNotReady,
   ECameraReserved,
   EEngineIdle,
   ESnappingPicture
   };

  1. endif /*__CAMERA_HRH_*/头文件: CCameraController.h

Code:

  1. ifndef __CCAMERACONTROLLER_H_
  2. define __CCAMERACONTROLLER_H_
  1. include <e32base.h> // CBase
  1. include "Camera.hrh"

// Forward declarations class CCameraEngine;

/**

* Interface between the UI and the camera engine.
*/

class CCameraController : public CBase

   {
   public:  // Constructors and destructor
       /**
        * Two-phased constructor.
        */
       static CCameraController* NewL();
       
       /**
        * Destructor.
        */
       virtual ~CCameraController();

   private:  // Constructors and destructor
       /**
        * Symbian OS default constructor.
        */
       CCameraController();

       /**
        * Symbian OS constructor.
        */
       void ConstructL();
   
   public:  // New functions
       /**
        * Initializes the camera.
        */
       void InitializeCameraL();
       
       /**
        * Snaps an image through the Camera API
        */
       void SnapL();
       
   private:  // Data
       CCameraEngine* iCameraEngine;
   };

  1. endif /*__CCAMERACONTROLLER_H_*/源文件 CCameraController.cpp

Code:

  1. include "Camera.hrh"
  2. include "CCameraController.h"
  3. include "CCameraEngine.h"

/*


Two-phased constructor


  • /

CCameraController* CCameraController::NewL()

   {
   CCameraController* self = new (ELeave) CCameraController();
   CleanupStack::PushL(self);
   self->ConstructL();
   CleanupStack::Pop(self);
   return self;
   }

/*


Destructor. Frees allocated resources.


  • /

CCameraController::~CCameraController()

   {
   delete iCameraEngine;
   }

/*


C++ default constructor


  • /

CCameraController::CCameraController()

   {
   }

/*


Symbian OS 2nd phase constructor


  • /

void CCameraController::ConstructL()

   {
   }

/*


Initializes the still image capture engine


  • /

void CCameraController::InitializeCameraL()

   {
   if (!iCameraEngine)
       {
       iCameraEngine = CCameraEngine::NewL();
       
       iCameraEngine->ReserveCameraL();
       }
   }

/*


Takes an image


  • /

void CCameraController::SnapL()

   {
   iCameraEngine->SnapL();
   }

头文件:CCameraEngine.h Last edited by huwell : 2009-03-12 at 05:57.

This page was last modified on 15 December 2011, at 09:34.
58 page views in the last 30 days.