如何捕捉图片
文章信息
测试基于
设备:: Nokia N95 8GB
兼容于
平台: S60 3rd Edition, MR
平台安全性
能力: UserEnvironment
文章
关键词: CCamera, CCameraController
翻译:
由 hoolee
最后由 hamishwillee
在 06 Mar 2012 编辑
- 详细描述
下列代码演示了如何使用Camera API(ecam.lib)捕捉一张图片。一般来说,操作流程如下:
- 生成一个camera controller作为UI和camera引擎的接口(这里放在CAppUi中)
- 调用CCameraController::InitializeCameraL(),它将会初始化相机引擎,并暂时掌控相机以便下面的操作。
- 掌控相机是一个异步的操作,完成后将会调用MCameraObserver::ReserveComplete()方法(或者就是CCameraEngine::ReserveComplete()方法的调用,因此CCameraEngine完成了MCameraObserver接口)
- 相机开始启动(CCamera::PowerOn()),同样这也是异步的操作,最终它将调用MCameraObserver::PowerOnComplete()方法。
- PowerOnComplete()方法准备开始捕捉,并设置引擎为空闲状态
- 当需要图片时,我们调用CCameraController::SnapL(),它将执行引擎的CCameraEngine::SnapL()命令,这才依次执行到实际的相机操作(CCamera::CaptureImage()方法)
- 捕捉图片也是一个异步的过程,完成后将调用MCameraObserver::ImageReady()
Contents |
MMP文件
需要下列链接库和能力
CAPABILITY UserEnvironment
LIBRARY ecam.lib
头文件: Camera.hrh
#ifndef __CAMERA_HRH_
#define __CAMERA_HRH_
enum TEngineState
{
EEngineNotReady,
ECameraReserved,
EEngineIdle,
ESnappingPicture
};
#endif /*__CAMERA_HRH_*/
头文件: CCameraController.h
#ifndef __CCAMERACONTROLLER_H_
#define __CCAMERACONTROLLER_H_
#include <e32base.h> // CBase
#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;
};
#endif /*__CCAMERACONTROLLER_H_*/
源文件 CCameraController.cpp
#include "Camera.hrh"
#include "CCameraController.h"
#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
#ifndef __CCAMERAENGINE_H_
#define __CCAMERAENGINE_H_
#include <ECam.h> // CCamera, MCameraObserver
#include "Camera.hrh"
class CCameraEngine : public MCameraObserver
{
public: // Constructors and destructor
/**
* Two-phased constructor.
*/
static CCameraEngine* NewL();
/**
* Destructor.
*/
virtual ~CCameraEngine();
private: // Constructors and destructor
/**
* Symbian OS default constructor.
*/
CCameraEngine();
/**
* Symbian OS constructor.
*/
void ConstructL();
private: // Methods from base classes
/**
* From MCameraObserver.
* Gets called when CCamera::Reserve() is completed.
*/
virtual void ReserveComplete(TInt aError);
/**
* From MCameraObserver.
* Gets called when CCamera::PowerOn() is completed.
*/
virtual void PowerOnComplete(TInt aError);
/**
* From MCameraObserver.
* Gets called when CCamera::StartViewFinderBitmapsL() is completed.
*/
virtual void ViewFinderFrameReady(CFbsBitmap& aFrame);
/**
* From MCameraObserver.
* Gets called when CCamera::CaptureImage() is completed.
*/
virtual void ImageReady(CFbsBitmap* aBitmap,
HBufC8* aData, TInt aError);
/**
* From MCameraObserver.
* Gets called when CCamera::StartVideoCapture() is completed.
*/
virtual void FrameBufferReady(MFrameBuffer* aFrameBuffer,
TInt aError);
public: // New Functions
/**
* Takes a picture.
*/
void SnapL();
/**
* Reserves the camera.
*/
void ReserveCameraL();
private: // Data
CCamera* iCamera;
TEngineState iState;
};
#endif /*__CCAMERAENGINE_H_*/
源文件 CCameraEngine.cpp
#include "Camera.hrh"
#include "CCameraEngine.h"
/*
-------------------------------------------------------------------------------
Two-phased constructor
-------------------------------------------------------------------------------
*/
CCameraEngine* CCameraEngine::NewL()
{
CCameraEngine* self = new (ELeave) CCameraEngine();
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
/*
-------------------------------------------------------------------------------
Destructor. Frees allocated resources.
-------------------------------------------------------------------------------
*/
CCameraEngine::~CCameraEngine()
{
delete iCamera;
}
/*
-------------------------------------------------------------------------------
C++ default constructor
-------------------------------------------------------------------------------
*/
CCameraEngine::CCameraEngine()
{
iState = EEngineNotReady;
}
/*
-------------------------------------------------------------------------------
Symbian OS 2nd phase constructor
-------------------------------------------------------------------------------
*/
void CCameraEngine::ConstructL()
{
// TODO: It is assumed here that the device has a camera.
// Add error handling, if this may not be the case.
// Camera index 0 is the main camera
iCamera = CCamera::NewL(*this, 0);
}
/*
-------------------------------------------------------------------------------
Reserves the camera.
-------------------------------------------------------------------------------
*/
void CCameraEngine::ReserveCameraL()
{
iCamera->Reserve();
// On completion, MCameraObserver::ReserveComplete() will be called
}
/*
-------------------------------------------------------------------------------
Symbian Onboard Camera API observer. Gets called after CCamera::Reserve() is
called.
-------------------------------------------------------------------------------
*/
void CCameraEngine::ReserveComplete(TInt aError)
{
// TODO: Error handling
iState = ECameraReserved;
iCamera->PowerOn();
}
/*
-------------------------------------------------------------------------------
Symbian Onboard Camera API observer. Gets called after CCamera::PowerOn() is
called.
-------------------------------------------------------------------------------
*/
void CCameraEngine::PowerOnComplete(TInt aError)
{
// TODO: Error handling
// Prepare the capture. It is assumed here that the device supports
// capturing in EXIF JPEG format.
CCamera::TFormat format = CCamera::EFormatExif;
const TInt KImageSizeIndex = 1; // 2nd largest image size
iCamera->PrepareImageCaptureL(format, KImageSizeIndex);
// Everything is ready. Set the engine to idle state.
iState = EEngineIdle;
}
/*
-------------------------------------------------------------------------------
Symbian Onboard Camera API observer. Gets called after
CCamera::StartViewFinderBitmapsL() is called.
-------------------------------------------------------------------------------
*/
void CCameraEngine::ViewFinderFrameReady(CFbsBitmap& aFrame)
{
// Not important in this snippet
}
/*
-------------------------------------------------------------------------------
Symbian Onboard Camera API observer. Gets called after CCamera::CaptureImage()
is called.
-------------------------------------------------------------------------------
*/
void CCameraEngine::ImageReady(CFbsBitmap* aBitmap, HBufC8* aData, TInt aError)
{
// TODO: Error handling
// Image saving is not demonstrated in this snippet
// ...
// The engine is ready for another go
iState = EEngineIdle;
}
/*
-------------------------------------------------------------------------------
Symbian Onboard Camera API observer. Gets called once
CCamera::StartVideoCapture() is called.
-------------------------------------------------------------------------------
*/
void CCameraEngine::FrameBufferReady(MFrameBuffer* aFrameBuffer, TInt aError)
{
// TODO: Error handling
// Capturing video is not demonstrated in this snippet
}
/*
-------------------------------------------------------------------------------
Takes a picture
-------------------------------------------------------------------------------
*/
void CCameraEngine::SnapL()
{
// Snapping a picture is only possible if the engine is in idle state
if (iState == EEngineIdle)
{
iState = ESnappingPicture;
iCamera->CaptureImage();
// On completion, MCameraObserver::ImageReady() will be called
}
else
{
User::Leave(KErrNotReady);
}
}
源文件:CAppUi.cpp
#include "CCameraController.h"
iCameraController = CCameraController::NewL();
iCameraController->InitializeCameraL();


(no comments yet)