You Are Here:

Community: Wiki

This page was last modified on 9 October 2008, at 20:26.

CS000954 - Implementing autofocus functionality (S60 3rd Edition, pre-FP2)

From Forum Nokia Wiki



ID CS000954 Creation date May 14, 2008
Platform S60 3rd Edition, FP1 Tested on devices Nokia N95 8GB
Category Symbian C++ Subcategory Imaging


Keywords (APIs, classes, methods, functions): CCamAutoFocus, MCamAutoFocusObserver, CCamera, CCamAutoFocus::InitL(), MCamAutoFocusObserver::InitComplete(), CCamAutoFocus::SetFocusRangeL(), CCamAutoFocus::AttemptOptimisedFocusL(), MCamAutoFocusObserver::OptimisedFocusComplete(), CCamAutoFocus::Cancel()

Overview

This snippet demonstrates how to implement autofocus (AF) functionality in S60 3rd Edition devices prior to S60 3rd Edition, FP2.

Note: In order to use the code, you need to install the AF extension for your SDK. You can obtain it from the S60 Platform: Camera Example with AutoFocus Support. Read the release notes of the example for more information.

This snippet can be self-signed.

MMP file

This snippet requires the following capabilities and libraries:

CAPABILITY  UserEnvironment
LIBRARY  ecam.lib
 
#ifdef WINSCW
LIBRARY CamAutoFocus.lib
#else
STATICLIBRARY CamAutoFocus_s.lib
#endif

Header file: Camera.hrh

enum TEngineState
{
EIdle,
EFocusing
};

Header file: CCameraEngine.h

#include <CCamAutoFocus.h>  // CCamAutoFocus, MCamAutoFocusObserver
#include <ECam.h> // CCamera, MCameraObserver
 
class CCameraEngine : public MCameraObserver,
public MCamAutoFocusObserver
{
// Constructors and destructors omitted for brevity
// ...
 
private: // Methods from base classes
/**
* From MCamAutoFocusObserver.
* Gets called when CCamAutoFocus::InitL() is completed.
*/

virtual void InitComplete(TInt aError);
 
/**
* From MCamAutoFocusObserver.
* Gets called when CCamAutoFocus::AttemptOptimisedFocusL() is
* completed.
*/

virtual void OptimisedFocusComplete(TInt aError);
 
// Methods from MCameraObserver omitted for brevity
// ...
 
private: // New functions
/**
* Starts the optimised autofocus operation.
* Does nothing if AF is not supported.
*/

void StartFocusL();
 
/**
* Cancels an ongoing autofocus operation.
*/

void CancelFocus();
 
private: // Data
CCamera* iCamera;
CCamAutoFocus* iAutoFocus;
TEngineState iState;
};

Source file: CCameraEngine.cpp

#include "CCameraEngine.h"
 
CCameraEngine::CCameraEngine(/* Parameters omitted */) :
iAutoFocus(0)
{
}
 
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);
 
// Try to create the autofocus object
TRAPD(afErr, iAutoFocus = CCamAutoFocus::NewL(iCamera));
if (afErr)
{
// TODO: Error handling: KErrExtensionNotSupported
iAutoFocus = 0;
}
}
 
// Other constructors omitted for brevity
// ...
 
CCameraEngine::~CCameraEngine()
{
delete iAutoFocus;
delete iCamera;
}
 
// From MCameraObserver. Gets called when CCamera::PowerOn() is completed.
void CCameraEngine::PowerOnComplete(TInt aError)
{
// TODO: Error handling (aError)
 
// Try to init the AF control - not fatal if fails
if (iAutoFocus)
{
TRAPD(afErr, iAutoFocus->InitL(*this));
if (afErr)
{
// TODO: Error handling: KErrExtensionNotSupported
delete iAutoFocus;
iAutoFocus = 0;
}
}
}
 
void CCameraEngine::InitComplete(TInt aError)
{
// TODO: Error handling (aError)
}
 
void CCameraEngine::StartFocusL()
{
// Do nothing if autofocus is not supported or focusing is already in
// progress
if (!iAutoFocus || iState == EFocusing)
return;
 
// Set AF range to normal before first focus attempt
iAutoFocus->SetFocusRangeL(CCamAutoFocus::ERangeNormal);
 
// Attempt focusing. Calls OptimisedFocusComplete when ready.
iState = EFocusing;
iAutoFocus->AttemptOptimisedFocusL();
}
 
void CCameraEngine::OptimisedFocusComplete(TInt aError)
{
iState = EIdle;
 
if (!aError)
{
// Play a sound etc.
}
}
 
void CCameraEngine::CancelFocus()
{
// Do nothing if autofocus is not supported or focusing is not in
// progress
if (!iAutoFocus || iState != EFocusing)
return;
 
iState = EIdle;
iAutoFocus->Cancel();
}

Header file: CCameraController.h

#include <e32base.h>
 
// Forward declarations
class CCameraEngine;
 
class CCameraController : public CBase
{
// ...
 
public: // New functions
/**
* Starts the optimized autofocus operation.
*/

void StartFocusL();
 
/**
* Cancels an ongoing autofocus operation.
*/

void CancelFocus();
 
private: // Data
CCameraEngine* iCameraEngine;
};

Source file: CCameraController.cpp

#include "CCameraEngine.h"
 
// ...
 
void CCameraController::StartFocusL()
{
iCameraEngine->StartFocusL();
}
 
void CCameraController::CancelFocus()
{
iCameraEngine->CancelFocus();
}

Header file: CCameraAppUi.h

#include <aknappui.h>
 
const TInt KStdKeyCameraFocus = 0xE2;
 
class CCameraExampleAppUi : public CAknAppUi
{
// ...
 
private: // Functions from base classes
/**
* From CEikAppUi.
* Handles key events.
*
* @param aKeyEvent Event to be handled.
* @param aType Type of the key event.
* @return Response code (EKeyWasConsumed, EKeyWasNotConsumed).
*/

virtual TKeyResponse HandleKeyEventL(const TKeyEvent& aKeyEvent,
TEventCode aType);
};

Source file: CCameraAppUi.cpp

TKeyResponse CCameraAppUi::HandleKeyEventL(const TKeyEvent& aKeyEvent,
TEventCode aType)
{
// Handle autofocus request
if (aKeyEvent.iScanCode == KStdKeyCameraFocus)
{
// Obtain the engine state from CCameraController
// TEngineState state = ...
 
if (state != EIdle && state != EFocusing)
{
return EKeyWasConsumed;
}
 
// When the focus key is pressed down, start the focus operation.
// When it is released, cancel the ongoing focus operation.
switch (aType)
{
case EEventKeyDown:
iCameraController->StartFocusL();
break;
case EEventKeyUp:
iCameraController->CancelFocus();
break;
default:
break;
}
return EKeyWasConsumed;
}
 
return EKeyWasNotConsumed;
}

Postconditions

The camera focuses when the focus key is pressed.

See also

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fJ2ME45E5fE52SSE5fParserE5fwithE5fKE58mlX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxJ2ME45E20E52SSE20ParserE20withE20KE58mlE20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZupdatedQDx2008E2d10E2d02X qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ