×
Namespaces

Variants
Actions

如何将图片读入Symbian位图

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

文章
翻译:
hoolee
最后由 hamishwillee 在 22 Dec 2011 编辑
  • 详细描述

这里的CImage_Reader完成了如何使用CImageDecoder打开不同类型图片的功能,并将它们转换为Symbian位图格式,它可以用来绘制图片到屏幕上。

这里GetFileType函数用来判断图片类型,如果图片类型无法识别,那么就会由iError指明错误。MImageReadyCallBack回调接口用来通知图片加载的完成情况。

  • Image_Reader.cpp
#include <cntfield.h>
#include <cntdef.h>
#include <cntitem.h>
#include <cntfldst.h>
#include <cntdb.h>
#include <COEMAIN.H>
#include <APMREC.H>
#include <APGCLI.H>
 
 
CImage_Reader::CImage_Reader(MImageReadyCallBack& aNotify)
:CActive(0),iNotify(aNotify)
{
}
 
CImage_Reader::~CImage_Reader()
{
Cancel();
delete iFrame;
}
 
 
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);
 
if(FileDataType.iConfidence > aResult.iConfidence)
{
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:

This page was last modified on 22 December 2011, at 07:59.
78 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.

×