How to read images to Symbian bitmap

The CImage_Reader implementation illustrates how to use CImageDecoder to open and read different types of images (jpeg, png, depending on the device) and how to convert them to the Symbian bitmap format, which then can be used to draw the image to the screen.

Tip: This is a basic example. There is more thorough example in Nokia Developer, which enables you to also load different types of image files, as well as rotate, scale and save them back in different formats.

S60 Platform: Image Converter Example


The GetFileType function is used to determine the type of the image file, and if the image type is not recognized by the system it will be indicated by the iError variable. The MImageReadyCallBack callback interface is used to notify of the completion of the image loading.

Image_Reader.cpp

#include <APMREC.H>
#include <APGCLI.H>
 
 
CImage_Reader::CImage_Reader(MImageReadyCallBack& aNotify)
:CActive(0),iNotify(aNotify)
{
}
 
CImage_Reader::~CImage_Reader()
{
Cancel();
delete iFrame;
delete iImageDecoder;
}
 
 
void CImage_Reader::ConstructL(const TDesC& aFileName)
{
CActiveScheduler::Add(this);
 
iImageName.Copy(aFileName);
 
TBuf8<255> ImageType;
GetFileType(aFileName, ImageType);
 
if(ImageType.Length() && iImageName.Length())
{
iImageDecoder = CImageDecoder::FileNewL(CCoeEnv::Static()->FsSession(),aFileName,ImageType);
 
delete iFrame;
iFrame = NULL;
iFrame = new(ELeave)CFbsBitmap();
iFrame->Create(iImageDecoder->FrameInfo(0).iOverallSizeInPixels,iImageDecoder->FrameInfo(0).iFrameDisplayMode);
 
iImageDecoder->Convert(&iStatus,*iFrame,0);
SetActive();
}
else
{
TRequestStatus* status=&iStatus;
User::RequestComplete(status, KErrNotSupported);
SetActive();
}
}
 
void CImage_Reader::GetFileType(const TDesC& aFileName, TDes8& aFileType)
{
TEntry FileEntry;
 
if(CCoeEnv::Static()->FsSession().Entry(aFileName,FileEntry) == KErrNone)
{
TBuf8<255> FileBuffer;
 
if(!FileEntry.IsDir())
{
TInt FileSize = FileEntry.iSize;
 
if(FileSize > 255)
{
FileSize = 255;
}
 
if(CCoeEnv::Static()->FsSession().ReadFileSection(aFileName,0,FileBuffer,FileSize) == KErrNone)
{
RApaLsSession RSession;
if(RSession.Connect() == KErrNone)
{
TDataRecognitionResult FileDataType;
 
RSession.RecognizeData(aFileName,FileBuffer,*&FileDataType);
 
aFileType.Copy(FileDataType.iDataType.Des8());
 
RSession.Close();
}
}
}
}
}
 
void CImage_Reader::DoCancel()
{
iImageDecoder->Cancel();
}
 
CFbsBitmap* CImage_Reader::Bitmap()
{
return iFrame;
}
 
 
void CImage_Reader::RunL()
{
iNotify.ImageReadyL(iStatus.Int());
}

Image_Reader.h

#include <e32base.h>
#include <coecntrl.h>
#include <w32std.h>
#include <e32std.h>
#include <cntdef.h>
#include <cntdb.h>
#include <ImageConversion.h>
 
class CFbsBitmap;
 
class MImageReadyCallBack
{
public:
virtual void ImageReadyL(const TInt& aError) = 0;
};
 
class CImage_Reader : public CActive
{
public:
CImage_Reader(MImageReadyCallBack& aNotify);
void ConstructL(const TDesC& aFileName);
~CImage_Reader();
public:
CFbsBitmap* Bitmap();
protected:
void DoCancel();
void RunL();
private:
void GetFileType(const TDesC& aFileName, TDes8& aFileType);
private:
MImageReadyCallBack& iNotify;
CImageDecoder* iImageDecoder;
CFbsBitmap* iFrame;
TFileName iImageName;
};


Related links:

Comments

I have a few comments on this code.

(1) What libraries need to be imported in the MMP for this class?
(2) Can you please provide a working example on how to use this class? (not just the implementation)

I can't make it work

can you provide an working example of this?


=======================

Looks like there's a bug that prevents this compiling since: aResult.iConfidence is not definted anywhere.

It might be better to use a constant for comparison here e.g. CApaDataRecognizerType's EUnlikely or EPossible values.


The CImageDecoder class doesn't seem to be deleted anywhere. This would result in a memory leak.

Furthermore, it seems that the file which has been converted, is locked. So after converting an image, it can't be written and replaced anymore until the app closes.


where is aResult here.

Icant find it anywhere Please correct this.




Since symbian does not provides direct API to load images, this article is very useful to load image. The source code given in this article load images asynchronously using active objects.

Article very clearly explained about how to load images using CImageDecoder. Usually beginners will face difficulties in using CImageDecoder class, so this article is specially useful to beginners.

--savaj 14:54, 4 September 2009 (UTC)


The article describes the way of reading images in Symbian OS. It provides source code to convert a non-bitmap image file to symbian bitmap file and to read them. The code given in the article has used Active objects to perform this asynchronous task.

The CImageReader is the active object which performs the asynchronous task to convert the file into bitmap file and read it. The GetFileType method is used to find the type of the image file. Though the article doesn't illustrates the code algorithm, you will find it easy to understand if you have a bit of knowledge of Symbian C++.

This article can be useful to the beginners who are more interested in graphics field, as i found it more interesting.

--deepikagohil 17:49, 8 September 2009 (UTC)

你可能感兴趣的:(Symbian)