×
Namespaces

Variants
Actions

如何将N97变成罗盘仪

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

代码示例
文章
翻译:
hoolee
最后由 hamishwillee 在 22 Dec 2011 编辑


Contents

简介

下列代码演示了如何将N97变成一个罗盘。这个程序可以用来根据内建的磁北感应器,用来寻找磁北方向。如果你转动设备,那么罗盘就会转向北方。

Compass.png

磁北感应器

N97以及某些设备有一个内建的磁力仪。这个磁力仪可以用来寻找方向。可以通过位图旋转来改变。

	TInt i = 0;
CSensrvChannelFinder* finder = CSensrvChannelFinder::NewLC();
RSensrvChannelInfoList channelList;
CleanupClosePushL( channelList );
TSensrvChannelInfo Channelinfo;
finder->FindChannelsL( channelList, Channelinfo );
 
for (i = 0; i<channelList.Count(); i++)
{
Channelinfo = channelList[i];
switch(Channelinfo.iChannelType)
{
case KSensrvChannelTypeIdMagneticNorthData:
iBoolMagSupported = ETrue;
break;
default:
iBoolMagSupported = EFalse;
break;
}
if(iBoolMagSupported == (TInt)ETrue)
{
break;
}
}
 
if(iBoolMagSupported == (TInt)EFalse)
{
User::Leave(KErrNotSupported);
}
 
if ( channelList.Count() > 0 )
{
Channelinfo = channelList[i];
iMagneticNorthSensor = CSensrvChannel::NewL( channelList[ i ] );
iMagneticNorthSensor->OpenChannelL();
iMagneticNorthSensor->StartDataListeningL( this, 1, 1, 0 );
}
channelList.Close();
CleanupStack::Pop( &channelList );
CleanupStack::PopAndDestroy( finder );

位图旋转

Following code shows how to rotate bitmap.
 
CFbsBitmap *CImageConverterEngine::Bitmap_Rotate(TInt Angle)
{
 
Angle = 360 -Angle;
TReal CosA = 0.0;
TReal SinA = 0.0;
 
if(!iOriginalBitmap)
{
return NULL;
}
 
TBitmapUtil BaseBmpUtil(iOriginalBitmap);
 
TBitmapUtil RotateBmpUtil(iRotatedBitmap);
 
BaseBmpUtil.Begin(TPoint(0,0));
 
RotateBmpUtil.Begin(TPoint(0,0), BaseBmpUtil);
RotateBmpUtil.Begin(TPoint(0,0));
 
TReal aAngle = (KPi/180.0)* (TReal)Angle;
 
TInt ret = Math::Cos(CosA, aAngle);
if(ret != KErrNone)
{
return NULL;
}
 
ret = Math::Sin(SinA, aAngle);
if(ret != KErrNone)
{
return NULL;
}
 
TInt xC = iSize.iWidth/2;
TInt yC = iSize.iHeight/2;
 
for(TInt new_y=0;new_y<iSize.iHeight;new_y++)
{
for(TInt new_x=0;new_x<iSize.iWidth;new_x++)
{
if( (new_x-xC)*(new_x-xC) + (new_y-yC)*(new_y-yC) <= (xC*yC))
{
TInt x = xC + (TInt)(((new_x - xC)*CosA) + ((new_y-yC)*SinA)); //new x pixel bitmap
TInt y = yC + (TInt)(-((new_x-xC)*SinA) + ((new_y-yC)*CosA)); // new y pixel bitmap
 
BaseBmpUtil.SetPos(TPoint(x,y));
RotateBmpUtil.SetPos(TPoint(new_x,new_y));
RotateBmpUtil.SetPixel(BaseBmpUtil);
}
}
}
BaseBmpUtil.End();
RotateBmpUtil.End();
 
return iRotatedBitmap;
}

示例程序

这里需要ReadDeviceData能力 示例程序需要在N97上测试,可以在这里下载File:PhoneCompass.zip

This page was last modified on 22 December 2011, at 08:00.
128 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.

×