×
Namespaces

Variants
Actions

Controlling the camera light using CCamera

From Nokia Developer Wiki
Jump to: navigation, search

This code example shows how to use the Symbian Onboard Camera API (CCamera) to turn the video recording light on/off and use the phone as a flashlight.

Article Metadata
Code ExampleTested with
Devices(s): Tested on: Nokia N86, Nokia 5800 XpressMusic
Compatibility
Platform(s): S60 3rd Edition
S60 5th Edition
Symbian^3
S60 3rd Edition FP2
Platform Security
Capabilities: UserEnvironment
Article
Keywords: CCamera, MCameraObserver2
Created: User:Kbwiki (23 Sep 2010)
Last edited: hamishwillee (14 Jun 2013)

Solution

Turning the video recording light on requires that the flash mode (CCamera::SetFlashL()) is set to CCamera::EFlashForced in case of 3.1 device and CCamera::EFlashVideoLight in case of 3.2 devices on wards, and video capturing is started. This can be achieved by preparing video capture with some default settings, using CCamera::PrepareVideoCaptureL(), and calling CCamera::StartVideoCapture() to start the capture. The resulting video frames can be released immediately as the objective is to simply control the flash light.

Implementation for a class that allows turning the light on and off can be downloaded Media:Cameraflashlight.zip.

Usage

Add the following definitions to your project's MMP file:

SOURCE cameraflashlight.cpp
LIBRARY ecam.lib
CAPABILITY UserEnvironment

Then, controlling the flash is straightforward:

cameraflashlight.h

 #include <ecam.h> // link against ecam.lib
 
//Derive Engine class from MCameraObserver in case of 3.1
//Derive Engine class from MCameraObserver2 in case of 3.2 onwards
#ifdef __SERIES60_31__
class CCameraFlashLight : public CBase, public MCameraObserver
#else
class CCameraFlashLight : public CBase, public MCameraObserver2
#endif
{
public:
static CCameraFlashLight* NewL();
~CCameraFlashLight();
void SetVideoLightL( TBool aOn );
TCameraState State() { return iState; }
private:
CCameraFlashLight() : iState(ECameraIdle) {};
void ConstructL();
void PrepareAndStartCaptureL();
void StopCapture();
private:
 
//For 3.1 device, the pure virtual functions from MCameraObserver
#ifdef __SERIES60_31__
void ReserveComplete(TInt aError);
void PowerOnComplete(TInt aError);
void FrameBufferReady(MFrameBuffer* aFrameBuffer,TInt aError){}
void StopVideoCaptureL();
void HandleEvent(const TECAMEvent &aEvent){}
void ViewFinderReady(MCameraBuffer& aCameraBuffer,TInt aError){}
void ImageBufferReady(MCameraBuffer& aCameraBuffer,TInt aError){}
void VideoBufferReady(MCameraBuffer& aCameraBuffer,TInt aError){}
void ImageReady(CFbsBitmap *aBitmap, HBufC8 *aData, TInt aError){}
void ViewFinderFrameReady(CFbsBitmap &aFrame){}
#else
 
//For 3.2 device onward, the pure virtual functions from MCameraObserver2
void HandleEvent( const TECAMEvent &aEvent );
void ViewFinderReady( MCameraBuffer&, TInt ) {}
void ImageBufferReady( MCameraBuffer&, TInt ) {}
void VideoBufferReady( MCameraBuffer& aBuf, TInt aError )
{
// simply release the buffer
if(aError == KErrNone)
aBuf.Release();
}
#endif
 
private:
CCamera* iCamera;
TCameraInfo iCameraInfo;
TCameraState iState;
};

cameraflashlight.cpp

 //Constructor
void CCameraFlashLight::ConstructL()
{
#ifdef __SERIES60_31__
iCamera = CCamera::NewL( *this, 0); //Initialize CCamera in v3.1
#else
iCamera = CCamera::New2L( *this, 0, 0 ); //Initialize CCamera in v3.2
#endif
}
//Destructor
CCameraFlashLight::~CCameraFlashLight()
{
StopCapture();
delete iCamera;
}
void CCameraFlashLight::SetVideoLightL( TBool aOn )
{
iCamera->CameraInfo(iCameraInfo);
#ifdef __SERIES60_31__
if( !(iCameraInfo.iFlashModesSupported & CCamera::EFlashForced) )
#else
if( !(iCameraInfo.iFlashModesSupported & CCamera::EFlashVideoLight) )
#endif
User::Leave( KErrNotSupported );
if( aOn )
{
iCamera->Reserve();
}
else
StopCapture();
}
// Callbacks from MCameraObserver class in case of 3.1
#ifdef __SERIES60_31__
void CCameraFlashLight::ReserveComplete(TInt aError)
{
if(!aError)
{
}
else
{
}
iCamera->PowerOn();
}
void CCameraFlashLight::PowerOnComplete(TInt aError)
{
iCamera->SetFlashL(iCamera->EFlashForced);
TCameraInfo iCameraInfo;
iCamera->CameraInfo(iCameraInfo);
if((iCameraInfo.iVideoFrameFormatsSupported & CCamera::EFormatYUV420Planar)== 0)
{
// log : video format not supported
}
else
{
PrepareAndStartCaptureL();
}
}
#else
// Callbacks from MCameraObserver2 class in case of 3.2
void CCameraFlashLight::HandleEvent( const TECAMEvent &aEvent )
{
if( aEvent.iErrorCode != KErrNone )
{
iState = ECameraErr;
return;
}
if( aEvent.iEventType == KUidECamEventReserveComplete )
iCamera->PowerOn();
if( aEvent.iEventType == KUidECamEventPowerOnComplete )
{
PrepareAndStartCaptureL();
}
}
#endif
void CCameraFlashLight::PrepareAndStartCaptureL()
{
// Setting flash in 3.1 and 3.2 respectively
#ifdef __SERIES60_31__
iCamera->SetFlashL( CCamera::EFlashForced );
#else
iCamera->SetFlashL( CCamera::EFlashVideoLight );
#endif
iCamera->PrepareVideoCaptureL( CCamera::EFormatYUV420Planar,
iCameraInfo.iNumVideoFrameSizesSupported - 1,0,2,1);
iCamera->StartVideoCapture(); // Video camera started to keep the flash on
}
void CCameraFlashLight::StopCapture()
{
TRAP_IGNORE( iCamera->SetFlashL(CCamera::EFlashNone) );
iCamera->StopVideoCapture();
iCamera->PowerOff();
iCamera->Release();
}
// CCameraFlashLight is initialized in AppUi and SetVideoLightL() is called:
CCameraFlashLight* iFlashlight = CCameraFlashLight::NewL();
iFlashlight->SetVideoLightL( ETrue );
This page was last modified on 14 June 2013, at 08:00.
74 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×