Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 07:27, 17 April 2008.

CS000904 - Capturing an image

From Forum Nokia Wiki


ID CS000904 Creation date April 17, 2008
Platform S60 3rd Edition, MR Tested on devices Nokia N95 8GB
Category Symbian C++ Subcategory Imaging


Keywords (APIs, classes, methods, functions): CCamera, MCameraObserver, CCamera::Reserve(), CCamera::PowerOn(), CCamera::CaptureImage(), MCameraObserver::ReserveComplete(), MCameraObserver::PowerOnComplete(), MCameraObserver::ImageReady()

Overview

This snippet demonstrates how to capture an image using the Camera API (ecam.lib). Basically, the flow of operations goes like this:

  1. A camera controller that acts as an interface between the UI and the camera engine is created (here, in CAppUi).
  2. CCameraController::InitializeCameraL() is called. It will initialize the camera engine and reserve the camera.
  3. Reserving the camera is an asynchronous operation. On completion, the MCameraObserver::ReserveComplete() function will be called (or actually the CCameraEngine::ReserveComplete() function, because CCameraEngine implements MCameraObserver interface).
  4. The camera is powered on (CCamera::PowerOn()). This, too, is an asynchronous operation. Eventually, it will call MCameraObserver::PowerOnComplete().
  5. The PowerOnComplete() function prepares the capture and sets the engine to idle state.
  6. When the picture is needed, CCameraController::SnapL() is called. This will delegate the command to the engine (CCameraEngine::SnapL()), which in turn delegates it to the actual camera (CCamera::CaptureImage()).
  7. Capturing an image is an asynchronous operation, which will call MCameraObserver::ImageReady() on completion.

This snippet can be self-signed.

MMP file

The following capabilities and libraries are required:

CAPABILITY  UserEnvironment
LIBRARY  ecam.lib

Header file: Camera.hrh

#ifndef __CAMERA_HRH_
#define __CAMERA_HRH_
 
enum TEngineState
    {
    EEngineNotReady,
    ECameraReserved,
    EEngineIdle,
    ESnappingPicture
    };
 
#endif /*__CAMERA_HRH_*/

Header file: 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_*/

Source file: 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();
    }

Header file: 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_*/

Source file: 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);
        }
    }

Source file: CAppUi.cpp

#include "CCameraController.h"
iCameraController = CCameraController::NewL();
iCameraController->InitializeCameraL();

Postconditions

An image is captured, and CCameraEngine::ImageReady() function is called.

See also

Related Discussions
Thread Thread Starter Forum Replies Last Post
How to store Images in RMS? lightpop Mobile Java General 5 2005-02-07 18:11
Help for a newbie nim_esh General Symbian C++ 9 2007-09-14 14:15
Draw an Image on itself FatalError Mobile Java Media (Graphics & Sounds) 4 2004-05-15 11:08
Sending image file via bluetooth selenatan Mobile Java Media (Graphics & Sounds) 7 2007-08-07 10:39
difference between camera burst, and taking stills consecutively ? bobjandal Symbian Media (Graphics & Sounds) 11 2008-02-22 11:30
 
Powered by MediaWiki