| ID | CS000906 | 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, CImageEncoder, CFbsBitmap, CActive, CCamera::CaptureImage(), MCameraObserver::ImageReady(), CImageEncoder::FileNewL(), CImageEncoder::Convert(), CFbsBitmap::Reset(), CFbsBitmap::Duplicate(), CFbsBitmap::Handle() |
This snippet demonstrates how to convert a bitmap image, which was captured using the Camera API (ecam.lib), to JPEG. The code is greatly simplified for the part of error handling and the actual image capturing. For more information on capturing an image, see the related code snippet [[CS000904 - Capturing an image].
This snippet can be self-signed.
This snippet requires the following libraries:
LIBRARY fbscli.lib
LIBRARY imageconversion.lib
In practice, however, also the following capabilities and libraries are required (to capture an image):
CAPABILITY UserEnvironment
LIBRARY ecam.lib
#include <e32base.h> // CActive
#include <ECam.h> // CCamera, MCameraObserver
#include <ImageConversion.h> // CImageEncoder
enum TTask
{
EConvertingImage,
ESleep
};
class CCameraEngine : public CActive,
public MCameraObserver
{
// Constructors and destructor omitted for brevity
// ...
private: // Methods from base classes
/**
* From CActive
*/
void RunL();
/**
* From CActive
*/
TInt RunError(TInt anError);
/**
* From CActive
*/
void DoCancel();
/**
* From MCameraObserver.
* Gets called when CCamera::CaptureImage() is completed.
*/
virtual void ImageReady(CFbsBitmap* aBitmap,
HBufC8* aData, TInt aError);
// Other functions from MCameraObserver omitted for brevity
// ...
private: // New functions
/**
* Destroys the JPEG encoder.
*/
void DeleteEncoder();
private: // Data
// ...
CImageEncoder* iEncoder;
CFbsBitmap* iBitmapSave;
TTask iTask; // Task for RunL
};
#include <e32base.h> // CActive
#include <EIKENV.H> // CEikonEnv
#include <ImageConversion.h> // CImageEncoder
#include "CCameraEngine.h"
// CCameraEngine::NewL() omitted for brevity
// ...
/**
* C++ default constructor.
*/
CCameraEngine::CCameraEngine() : CActive(EPriorityStandard)
{
CActiveScheduler::Add(this);
iTask = ESleep; // Default task for RunL
}
/**
* Symbian OS 2nd phase constructor.
*/
void CCameraEngine::ConstructL()
{
// ...
// Create the bitmap that will hold the image to be converted
iBitmapSave = new (ELeave) CFbsBitmap;
}
/**
* Destructor. Frees allocated resources.
*/
CCameraEngine::~CCameraEngine()
{
// ...
delete iEncoder;
delete iBitmapSave;
// Cancel any outstanding requests
Cancel();
}
/**
* Symbian Onboard Camera API observer. Gets called after
* CCamera::CaptureImage() is called.
* @param aBitmap a pointer to a bitmap image if this was the format specified
* @param aData a pointer to JPEG image data if this was the format specified
* @param aError KErrNone on success or an error code on failure
*/
void CCameraEngine::ImageReady(CFbsBitmap* aBitmap, HBufC8* aData, TInt aError)
{
// TODO: Error handling (aError)
// It is assumed that the device supports image capturing in bitmap
// format, and that the capturing was set up accordingly
// (CCamera::EFormatFbsBitmapColor4K, CCamera::EFormatFbsBitmapColor64K, or
// CCamera::EFormatFbsBitmapColor16M).
// This means that the image data is contained in aBitmap argument.
// Store the bitmap received as an argument to iBitmapSave for conversion
iBitmapSave->Reset();
TInt err = iBitmapSave->Duplicate(aBitmap->Handle());
// TODO: Error handling (err)
// Connect to the file server session
RFs& fsSession = CEikonEnv::Static()->FsSession();
_LIT(KFilename, "C:\\Data\\Images\\image.jpg");
_LIT8(KMimeType, "image/jpeg");
// Create the image encoder which is responsible for converting the image
TRAPD(encErr,
iEncoder = CImageEncoder::FileNewL(fsSession, KFilename, KMimeType));
// TODO: Error handling (encErr)
// Start to do the conversion.
iEncoder->Convert(&iStatus, *iBitmapSave);
iTask = EConvertingImage;
SetActive();
}
// Other functions from MCameraObserver omitted for brevity
// ...
/**
* From CActive.
*/
void CCameraEngine::RunL()
{
switch (iTask)
{
case EConvertingImage:
{
// TODO: Error handling (iStatus may contain an error code)
DeleteEncoder(); // Release captured image file
// Conversion done. Go to sleep.
iTask = ESleep;
break;
}
case ESleep:
default:
{
break;
}
}
}
/**
* From CActive.
*/
TInt CCameraEngine::RunError(TInt anError)
{
return anError;
}
/**
* From CActive.
*/
void CCameraEngine::DoCancel()
{
// Cancel the appropriate task
switch (iTask)
{
case EConvertingImage:
{
iEncoder->Cancel();
DeleteEncoder();
}
case ESleep:
default:
{
break;
}
}
}
/**
* Destroys the JPEG encoder.
*/
void CCameraEngine::DeleteEncoder()
{
if (iEncoder)
{
delete iEncoder;
iEncoder = 0;
}
if (iBitmapSave)
{
iBitmapSave->Reset();
}
}
The captured bitmap image is converted to JPEG and saved as C:\Data\Images\image.jpg.
No related wiki articles found