Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Converting a bitmap image captured from ECam to JPEG

From Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Nokia N95 8GB
CompatibilityPlatform Security
Signing Required: Self Signed
Capabilities: UserEnvironment
Article
Keywords: CCamera, MCameraObserver, CImageEncoder, CFbsBitmap, CActive, CCamera::CaptureImage(), MCameraObserver::ImageReady(), CImageEncoder::FileNewL(), CImageEncoder::Convert(), CFbsBitmap::Reset(), CFbsBitmap::Duplicate(), CFbsBitmap::Handle()
Created: tapiolaitinen (09 Apr 2008)
Last edited: pavarang (20 Aug 2012)

Contents

Overview

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].

MMP file

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

Header file

#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
};

Source file

#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)
 
// Release aBitmap handle or a memory leak sooner or later will occur; image won't be deleted because of previous iBitmapSave->Duplicate()
aBitmap->Reset();
 
// 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();
}
}

Postconditions

The captured bitmap image is converted to JPEG and saved as C:\Data\Images\image.jpg.

See also

Version Hint

Windows Phone: [[Category:Windows Phone]]
[[Category:Windows Phone 7.5]]
[[Category:Windows Phone 8]]

Nokia Asha: [[Category:Nokia Asha]]
[[Category:Nokia Asha Platform 1.0]]

Series 40: [[Category:Series 40]]
[[Category:Series 40 1st Edition]] [[Category:Series 40 2nd Edition]]
[[Category:Series 40 3rd Edition (initial release)]] [[Category:Series 40 3rd Edition FP1]] [[Category:Series 40 3rd Edition FP2]]
[[Category:Series 40 5th Edition (initial release)]] [[Category:Series 40 5th Edition FP1]]
[[Category:Series 40 6th Edition (initial release)]] [[Category:Series 40 6th Edition FP1]] [[Category:Series 40 Developer Platform 1.0]] [[Category:Series 40 Developer Platform 1.1]] [[Category:Series 40 Developer Platform 2.0]]

Symbian: [[Category:Symbian]]
[[Category:S60 1st Edition]] [[Category:S60 2nd Edition (initial release)]] [[Category:S60 2nd Edition FP1]] [[Category:S60 2nd Edition FP2]] [[Category:S60 2nd Edition FP3]]
[[Category:S60 3rd Edition (initial release)]] [[Category:S60 3rd Edition FP1]] [[Category:S60 3rd Edition FP2]]
[[Category:S60 5th Edition]]
[[Category:Symbian^3]] [[Category:Symbian Anna]] [[Category:Nokia Belle]]

This page was last modified on 20 August 2012, at 12:31.
115 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.

×