richardli79

导航

Symbian60第二版拍照功能实现

前一阵子使用Symbian手机作一个图像识别的程序,从网上找了一个现成的图像抓拍的程序,摘上一部分关键代码。
/*
* ============================================================================
*  Name     : CCameraCapureEngine from CameraCaptureEngine.cpp
*  Part of  : CameraApp
*  Description : Provides all still image releated methods. 
*                Interface to Symbian Onboard Camera API.
*  Created  : 05/06/2006 by Forum Nokia
*  Version  : 2.0
*  Copyright: Nokia Corporation, 2006
* ============================================================================
*/

#include 
"CameraCaptureEngine.h"

#include 
<AknViewAppUi.h>
#include 
<eikenv.h>
#include 
<barsread.h>
#include 
<BitmapTransforms.h>
//#include "SB80a_S602ndFP2SC_barcode_decoder.h"
//typedef void (*EntryPointfuncPtr)(HBufC8* grayDate, TInt width, TInt height, HBufC8* text);
//class CSB80a_S602ndFP2SC_barcode_decoder;
_LIT( KSavingImage, "Saving image" );
_LIT( KRecognizeImage, 
"Recoginzing image");
_LIT( KImageSaved, 
"Image saved" );
_LIT( KImageRecognized, 
"Image Recognized" );
/*
-----------------------------------------------------------------------------

    CCameraCaptureEngine::CCameraCaptureEngine( 
            CBaseAppController& aController,
            const TRect& aRect )
                    :CActive( EPriorityStandard ),
                    iController( aController ), 
                    iRect(aRect),
                    iZoomFactor( NULL ), iCapturePrepared( EFalse ), 
                    iEncoder( NULL ),
                    iFrameImageData( NULL ), iCameraReserved( EFalse )

    Description: C++ default constructor can NOT contain any code that might 
                 leave.
    Comments   :

    Return values: N/A

-----------------------------------------------------------------------------
*/
CCameraCaptureEngine::CCameraCaptureEngine( CBaseAppController
& aController)
                    :CActive( EPriorityStandard ),
                    iController( aController ), 
                    iZoomFactor( NULL ), iCapturePrepared( EFalse ), 
                    iEncoder( NULL ), iCameraReserved( EFalse )
    {
    CActiveScheduler::Add( 
this );
    iState 
= EEngineIdle;
    }

/*
-----------------------------------------------------------------------------

    CCameraCaptureEngine* CCameraCaptureEngine::NewL( CBaseAppController& 
                                                aController )

    Description: Two-phased constructor.

    Comments   :

    Return values: CCameraCaptureEngine object pointer

-----------------------------------------------------------------------------
*/
CCameraCaptureEngine
* CCameraCaptureEngine::NewL( 
    CBaseAppController
& aController, const TRect& aRect )
    {
    CCameraCaptureEngine
* self = 
        
new (ELeave) CCameraCaptureEngine( aController );

    CleanupStack::PushL( self );
    self
->ConstructL(aRect);
    CleanupStack::Pop(self);

    
return self;
    }


/*
-----------------------------------------------------------------------------

    CCameraCaptureEngine::~CCameraCaptureEngine()

    Description: Destructor, free allocated resources
    Comments   :

    Return values: N/A

-----------------------------------------------------------------------------
*/
CCameraCaptureEngine::
~CCameraCaptureEngine()
    {
    
if (iEncoder)
        delete iEncoder;      
    
if (iCamera)
        delete iCamera;
    
if (iBitmapPortraitVF)
        delete iBitmapPortraitVF;
    
if (iBitmapSave)
        delete iBitmapSave;

    
// Cancel any outstanding request
    Cancel();
    }

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::ConstructL(const TRect& aRect)

    Description: Symbian 2nd phase constructor can leave.
    Comments   :

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::ConstructL(const TRect& aRect)
    {
    iEikEnv 
= CEikonEnv::Static();
    
if ( !CCamera::CamerasAvailable() )
        {
        HandleError( KErrHardwareNotAvailable );
        
return;
        }
       
    
// camera index 0 (the main camera)
    iCamera = CCamera::NewL( *this0 );

    iCameraReserveComplete 
= ETrue;//No requests issued yet

    
// Gets information about the camera device. refer to SDK for more info.
    iCamera->CameraInfo(iInfo); 
    iDisplayMode 
= DisplayMode();
    iColorDepth 
= ImageFormat();
    iColorDepthHW 
= ImageFormatMax();
    ClientRectChangedL(aRect);
    }

/*
-----------------------------------------------------------------------------

    TDisplayMode CCameraCaptureEngine::DisplayMode() const

    Description: Returns default display mode
    Comments   :

    Return values: Returns default display mode

-----------------------------------------------------------------------------
*/
TDisplayMode CCameraCaptureEngine::DisplayMode() 
const
    {
    TInt color;
    TInt gray;
    TDisplayMode displayMode 
= EColor4K;
//        iEikEnv->WsSession().GetDefModeMaxNumColors( color, gray );
    return displayMode;
    }

/*
-----------------------------------------------------------------------------

    CCamera::TFormat CCameraCaptureEngine::ImageFormat() const

    Description: Returns camera image format to be used with current display 
                 mode
    Comments   :

    Return values: Returns camera image format to be used with current display 
                   mode

-----------------------------------------------------------------------------
*/
CCamera::TFormat CCameraCaptureEngine::ImageFormat() 
const
    {
    
switch ( iDisplayMode )
        {
        
case EColor16M:
            
return CCamera::EFormatFbsBitmapColor16M;
        
case EColor64K:
            
return CCamera::EFormatFbsBitmapColor64K;
        
case EColor4K:
            
return CCamera::EFormatFbsBitmapColor4K;
        
case EGray256:
            
return CCamera::EFormatMonochrome;
        
default:
            
return CCamera::EFormatFbsBitmapColor4K;
        }
    }

/*
-----------------------------------------------------------------------------

    CCamera::TFormat CCameraCaptureEngine::ImageFormatMax() const

    Description: Returns highest color mode supported by HW
    Comments   :

    Return values: Returns highest color mode supported by HW

-----------------------------------------------------------------------------
*/
CCamera::TFormat CCameraCaptureEngine::ImageFormatMax() 
const
    {
    
if ( iInfo.iImageFormatsSupported & CCamera::EFormatFbsBitmapColor16M )
        {
        
return CCamera::EFormatFbsBitmapColor16M;
        }
    
else if ( iInfo.iImageFormatsSupported & CCamera::EFormatFbsBitmapColor64K)
        {
        
return CCamera::EFormatFbsBitmapColor64K;
        }
    
else if ( iInfo.iImageFormatsSupported & CCamera::EFormatFbsBitmapColor4K)
        {
        
return CCamera::EFormatFbsBitmapColor4K;
        }
    
else if ( iInfo.iImageFormatsSupported & CCamera::EFormatMonochrome)
        {
        
return CCamera::EFormatMonochrome;
        }
    
else return CCamera::EFormatMonochrome;
    }

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::StopViewFinder() 

    Description: Stops view finding
    Comments   :

    Return values: Stops view finding

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::StopViewFinder() 
    {
    
if ( iCameraReserved && iCameraReserveComplete )
        {
        iCamera
->StopViewFinder();
        }
    }

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::DoViewFinderL() 

    Description: Starts view finding and prepares image capturing
    Comments   :

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::DoViewFinderL() 
    {
    
//Default, always supported by API
    iCamera->SetExposureL(); 

    
if ( iZoomFactor && !iCapturePrepared )
        {
        
//Camera released, restore zoom setting
        iCamera->SetDigitalZoomFactorL( iZoomFactor );
        }
    
//Preferred
    if ( iInfo.iOptionsSupported & TCameraInfo::EViewFinderBitmapsSupported ) 
        {
        
if ( iInfo.iOptionsSupported & TCameraInfo::EImageCaptureSupported 
            
&& !iCapturePrepared )
            {
            
//Request largest image 
            
// test for supported colordepth
            iCamera->PrepareImageCaptureL( ImageFormat(), 1);
//            iCamera->PrepareImageCaptureL( iColorDepthHW, 0);
            iCapturePrepared = ETrue;
            }

        
// Start the view finding, and transfer the view finder data.
        
// Bitmaps are returned by MCameraObserver::ViewFinderFrameReady(). 
        iCamera->StartViewFinderBitmapsL( iLandscapeSize );
        }
    
else if (iInfo.iOptionsSupported & TCameraInfo::EViewFinderDirectSupported)
        {
        User::Leave(KErrNotSupported);
//Not verified
        }
    
else
        {
        
//Image can be taken without viewfinding
        if ( iInfo.iOptionsSupported & TCameraInfo::EImageCaptureSupported 
            
&& !iCapturePrepared)
            {
            iCamera
->PrepareImageCaptureL( iColorDepthHW, 0);
            iCapturePrepared 
= ETrue;
            }
        }
    }

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::StartViewFinderL() 

    Description: Reserves camera, switches power on, and starts finally view 
                 finding
    Comments   :

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::StartViewFinderL() 
    {    
    ReserveCameraL();
    }  

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::SnapL()

    Description: take a picture
    Comments   :

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::SnapL()
    {
    
if ( iCameraReserved && iCameraReserveComplete && !iPowering )
        {
        
//If the Engine is ready
        iCamera->StopViewFinder();

        
// According to on-board camera API, MCameraObserver::ImageReady()
        
// will be called back when the following API is called.
        iCamera->CaptureImage();
        }
    
else
        {
        User::Leave( KErrNotReady );
        }
    }


/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::SetMode( TCameraState aMode )

    Description: Sets engine's camera mode
    Comments   :

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::SetMode( TCameraState aMode )
    {   
    iMode 
= aMode;
    }

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::DeleteEncoder()

    Description: Destructs JPEG encoder
    Comments   :

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::DeleteEncoder()
    {
    
if (iEncoder)
        {
        delete iEncoder;
        iEncoder 
= NULL;
        }

    
if (iBitmapSave)
        {
        delete iBitmapSave;
        iBitmapSave 
= NULL;
        }
    }

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::SaveImageL(TJpegQualityFactor aQuality, 
                            const TFileName* aNewFilePathAndName, RFs* aFs)

    Description: Converts and saves bitmap to JPEG image
    Comments   :

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::SaveImageL(TJpegQualityFactor /*aQuality*/,
    
const TFileName* aNewFilePathAndName, RFs* aFs)
    {
    
if ( !iEncoder )
        {
        
if ( aFs && aNewFilePathAndName )
            {
            
//            iEncoder = CImageEncoder::DataNewL( iBitmapBuf, KMimeType );
            iEncoder = CImageEncoder::FileNewL(*aFs, *aNewFilePathAndName, 
                KMimeType);
            }
        
else
            {
            User::Leave( KErrArgument );
            }
    }

    
// If it is not busy, start to do the conversion.
    if ( !IsActive() )
        {
//        _LIT(Ktxt,"SymbianDll.DLL");//宏定义指定要调用的dll
//        RLibrary library;//调用库函数类
//        TFileName file;
//        RFs fs;
//        HBufC8* nBuffer = HBufC8::NewL(32);
//        CBitmapScaler* nScaler = CBitmapScaler::NewL();
//        EntryPointfuncPtr decode_barcode = NULL;
//        CFbsBitmap* nDestBitmap = new(ELeave) CFbsBitmap();
//        CleanupStack::PushL(nDestBitmap);
//        User::LeaveIfError(nDestBitmap->Create(TSize(640, 480), EGray256));
    
//        nScaler->Scale(&iStatus, *iBitmapSave, *nDestBitmap);
//        nDestBitmap->Save(*aNewFilePathAndName);
//        User::LeaveIfError(fs.Connect());
//        file=Ktxt;
//        User::LeaveIfError(library.Load(file));//如果失败就退出
//
//        decode_barcode = (EntryPointfuncPtr)library.Lookup ( 1 );
//        decode_barcode(nDestBitmap,640,480,nBuffer);

//        library.Close();//关闭调用
        iEncoder->Convert( &iStatus, *iBitmapSave );
        iState 
= EConvertingImage;

        
// Update the status pane to "Saving image"
        iController.ShowConversionStatusL( KSavingImage );
        SetActive();
        }
}

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::SaveImageL(TJpegQualityFactor aQuality, 
                            const TFileName* aNewFilePathAndName, RFs* aFs)

    Description: Converts and saves bitmap to JPEG image
    Comments   :

    Return values: N/A

-----------------------------------------------------------------------------

void CCameraCaptureEngine::RecBarcodeImageL()
{
    TInt nSum = 0;
    RLibrary library;//调用库函数类
    TFileName file;
//    HBufC8* nBuffer = HBufC8::NewL(32);
//    CBitmapScaler* nScaler = CBitmapScaler::NewL();
    TLibraryFunction functionWinsMain = NULL;
//    CFbsBitmap* nDestBitmap = new(ELeave) CFbsBitmap();

//    CleanupStack::PushL(nDestBitmap);
//    User::LeaveIfError(nDestBitmap->Create(TSize(640, 480), EGray256));
//    nScaler->Scale(&iStatus, *iBitmapSave, *nDestBitmap);
//    delete nScaler;
    file = KDLLFILE;
    User::LeaveIfError(library.Load(file));//如果失败就退出

    functionWinsMain = library.Lookup( 1 );//查找入口程序 functionWinsMain 为获得程序的第一个导出函数

    CImpCalculator* calculator = (CImpCalculator*)functionWinsMain();//实例化类
    CleanupStack::PushL(calculator);
    User::LeaveIfError( nSum = calculator->Sum(1,2) );
    CleanupStack::PopAndDestroy();
    library.Close();//关闭调用
//        iEncoder->Convert( &iStatus, *iBitmapSave );
    iState = ERecognized;
    iController.ShowConversionStatusL( KImageRecognized );
//    SetActive();
}
*/

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::ReserveCameraL()

    Description: Reserves camera
    Comments   : "ResereComplete" will get called after completion

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::ReserveCameraL()
    {
    iStart 
= ETrue;
    
//Ongoing, no need to issue again
    if ( iCameraReserved && !iCameraReserveComplete )
        {
        User::Leave(
0);
        }

    
if ( !iCameraReserved && iCameraReserveComplete )
        {
        iCameraReserved 
= ETrue;
        iCameraReserveComplete 
= EFalse;
        iCamera
->Reserve(); //Async
        }

    
if ( iCameraReserved && iCameraReserveComplete )
        {
        
if ( !iPowering )
            {
            iPowering 
= ETrue;
            iCamera
->PowerOn();
            }
        
else
            {
            User::Leave(
0);
            }
        }
    }

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::ReleaseCamera()

    Description: Releases camera
    Comments   : 

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::ReleaseCamera()
    {
    iStart 
= EFalse;  
    
if ( iCameraReserved && iCameraReserveComplete )
        {
        iCamera
->Release();
        iCameraReserved 
= EFalse; 
        }
    
    iCapturePrepared 
= EFalse;
    }

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::PowerOff()

    Description: Switches off camera power 
    Comments   : 

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::PowerOff()
    {
    
if ( !iPowering && iCameraReserved )
        {
        iCamera
->PowerOff();
        ReleaseCamera();
        }
    }

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::ReserveComplete(TInt aError)

    Description: reserve completes
    Comments   : Symbian Onboard Camera API observer, This happens after 
                 CCamera::Reserve() is called

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::ReserveComplete(TInt aError)
    {
    iCameraReserveComplete 
= ETrue;
    
if ( aError )
        {
        iCameraReserved 
= EFalse;
        HandleError( aError );
        }
    
    
if ( iStart )
        {
        iPowering 
= ETrue;
        iCamera
->PowerOn();
        }
    
else
        {
        ReleaseCamera();
        }
    }

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::PowerOnComplete(TInt aError)

    Description: poweron completes
    Comments   : Symbian Onboard Camera API observer, This happens after 
                 CCamera::PowerOn() is called

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::PowerOnComplete(TInt aError)
    {
    HandleError( aError );
    iPowering 
= EFalse;
    
if ( iStart ) //The Operation is not cancelled
        {
        TRAPD( err, DoViewFinderL() );
        HandleError( err );
        }
    
else
        {
        ReleaseCamera();
        }
    }

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::ViewFinderFrameReady(CFbsBitmap& aFrame)

    Description: Switches off camera power 
    Comments   : Symbian Onboard Camera API observer,  This is called once 
                "StartViewFinderBitmapsL" is called

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::ViewFinderFrameReady(CFbsBitmap& aFrame)
    {
    
if ( iController.CameraMode() == ECameraPortraitMode )
        {
        TRAPD(ignore, ClipL(aFrame));
        iController.ViewFinding( 
*iBitmapPortraitVF );
        }
    
else
        {
        
// Pass the Bitmap frame to the controller
        iController.ViewFinding( aFrame );
        }
    }

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::ImageReady(CFbsBitmap* aBitmap,HBufC8* aData,
                               TInt aError)

    Description: called when an image is ready 
    Comments   : Symbian Onboard Camera API observer,  This is called once 
                "CCamera::CaptureImage()" is called

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::ImageReady(CFbsBitmap* aBitmap,HBufC8*/*aData*/,
                                      TInt aError)
    {
    TInt err(KErrNone);
    
if ( !aError )
        {
        iBitmapSave 
= aBitmap;
        TRAP(err, DrawL());
        HandleError( err );
        }
    
else
        {
        HandleError( aError );  
        }
    }

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::FrameBufferReady(MFrameBuffer* aFrameBuffer, 
                               TInt aError )

    Description: called when a framebuffer is ready. 
    Comments   : Symbian Onboard Camera API observer,  This is called once 
                "CCamera::StartVideoCapture()" is called

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::FrameBufferReady(MFrameBuffer*  /*aFrameBuffer*/,
                                            TInt 
/*aError*/)
    {
    
// We are not using video capture
    }

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::ClipL(const CFbsBitmap& aFrame)

    Description: Clips the viewfinding iamges according to portrait mode size
    Comments   : 

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::ClipL(const CFbsBitmap& aFrame)
    {
    TSize size 
= aFrame.SizeInPixels();
    TInt x1 
= (size.iWidth-iPortraitSize.iWidth)/2;
    TInt x2 
= x1+iPortraitSize.iWidth;
    TInt y1 
= (size.iHeight-iPortraitSize.iHeight)/2;
    TInt y2 
= y1+iPortraitSize.iHeight;

    CFbsBitGc
* fbsBitGc = CFbsBitGc::NewL(); //graphic context
    CleanupStack::PushL( fbsBitGc );
    CFbsBitmapDevice
* portraitImageDevice = 
        CFbsBitmapDevice::NewL( iBitmapPortraitVF );
    fbsBitGc
->Activate( portraitImageDevice );

    fbsBitGc
->BitBlt( TPoint(0,0), &aFrame, TRect(x1,y1,x2,y2) );

    delete portraitImageDevice;
    CleanupStack::PopAndDestroy(fbsBitGc);
        
    }

/*
-----------------------------------------------------------------------------

    TRect CCameraCaptureEngine::Portrait( const CFbsBitmap* aBitmap)
    
    Description: Calculates Portrait image size from bigger snapped image 
                 remaining the aspect
    Comments   : 

    Return values: portrait size

-----------------------------------------------------------------------------
*/
TRect CCameraCaptureEngine::Portrait( 
const CFbsBitmap* aBitmap)
    {
    TRect portrait 
= TRect();
    
if ( aBitmap )
        {
        TSize size 
= aBitmap->SizeInPixels();
        TInt portx 
= 
            iPortraitSize.iWidth 
* size.iWidth / iLandscapeSize.iWidth;
        TInt porty 
= 
            iPortraitSize.iHeight 
* size.iHeight / iLandscapeSize.iHeight;
        TInt x1 
= (size.iWidth-portx)/2;
        TInt x2 
= x1+portx;
        TInt y1 
= (size.iHeight-porty)/2;
        TInt y2 
= y1+porty;
        portrait.SetRect(x1,y1,x2,y2);
        }
    
return portrait;
    }

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::DrawL()
    
    Description: Draws captured image on the screen, modifies if needed
    Comments   : 

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::DrawL()
    {
    CFbsBitGc
* fbsBitGc = CFbsBitGc::NewL(); //graphic context
    CleanupStack::PushL( fbsBitGc );
    
if ( iController.CameraMode() == ECameraPortraitMode )
        {        
        User::LeaveIfError( iController.GetSnappedImage().
            Resize( iPortraitSize ));
        }
    
else
        {
        User::LeaveIfError( iController.GetSnappedImage().
            Resize( iLandscapeSize ));
        }

    CFbsBitmapDevice
* bmpDevice = 
        CFbsBitmapDevice::NewL( 
&iController.GetSnappedImage() );    
    fbsBitGc
->Activate( bmpDevice );

    
if ( iController.CameraMode() == ECameraPortraitMode )
        {
        TRect portraitRect 
= Portrait( iBitmapSave );
        
//Shrink to snap image size
        fbsBitGc->DrawBitmap( TRect(iPortraitSize), iBitmapSave, 
            portraitRect );
        delete bmpDevice;
                
        
//Full color image
        CFbsBitmapDevice* bmpDeviceSave = 
            CFbsBitmapDevice::NewL( iBitmapSave );
        CleanupStack::PushL( bmpDeviceSave );
        fbsBitGc
->Activate( bmpDeviceSave );
        fbsBitGc
->DrawBitmap( TRect(iPortraitSize), iBitmapSave, 
            portraitRect );
        
//To be saved
        User::LeaveIfError( iBitmapSave->Resize( iPortraitSize ));  
        CleanupStack::PopAndDestroy(bmpDeviceSave);
//bmpDeviceSave 
        }
    
else
        {
        fbsBitGc
->DrawBitmap( TRect(iLandscapeSize), iBitmapSave );
        delete bmpDevice;
        }

    CleanupStack::PopAndDestroy(fbsBitGc);
//fbsBitGc;

//    StartToRecImage();
    
// Start to save the image.
    StartToSaveImage();
    }

/*
-----------------------------------------------------------------------------

    TInt CCameraCaptureEngine::SetZoomFactorL(TBool aEnable) 
    
    Description: Sets zoom on/off
    Comments   : 

    Return values: N/A

-----------------------------------------------------------------------------
*/
TInt CCameraCaptureEngine::SetZoomFactorL(TBool aEnable) 
    {   
    TInt bitmapCount 
= ECameraZoomLimit - ECameraZoom2Uid;  
    
//both 0 and 1 indicate that zoom functionality is not supported      
    if ( iInfo.iMaxZoomFactor != 0 && iInfo.iMaxZoomFactor != 1 )
        {
        
if ( aEnable && iZoomFactor < iInfo.iMaxZoom )
            {
            iZoomFactor
++;
            }
        
if ( !aEnable && iZoomFactor > iInfo.iMinZoom )
            {
            iZoomFactor
--;
            }
        iCamera
->SetZoomFactorL( iZoomFactor );
        
//Zoom ind. bitmap offset 
        return ( iInfo.iMaxZoom > bitmapCount )?KErrNotFound:iZoomFactor-1;
        }
    
if ( iInfo.iMaxDigitalZoomFactor != 0 && iInfo.iMaxDigitalZoomFactor != 1 )
        {
        
if ( aEnable && iZoomFactor < iInfo.iMaxDigitalZoom )
            {
            iZoomFactor
++;
            }
        
if ( !aEnable && iZoomFactor > 0 )
            {
            iZoomFactor
--;
            }
        iCamera
->SetDigitalZoomFactorL(iZoomFactor);
        iCapturePrepared 
= EFalse;
        
//Zoom ind. bitmap offset 
        return (iInfo.iMaxDigitalZoom>bitmapCount)?KErrNotFound:iZoomFactor-1;
        }
    
return KErrNotFound; 
    }

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::HandleError(TInt aError )
    
    Description: Displays error message
    Comments   : 

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::HandleError(TInt aError )
    {
    TInt reason(KErrNone);
    
switch( aError )
        {
        
case KErrNone:
            reason 
= KErrNone;
            
break;
        
case KErrNoMemory:
            iEikEnv
->HandleError( aError );
            reason 
= ECameraOverflow;
            
break;
        
case KErrInUse:
            reason 
= ECameraInUse;
            iController.HandleError( aError );
            
break;
        
case KErrHardwareNotAvailable:
            reason 
= ECameraHwFailure;
            
break;
        
case KErrTimedOut:
            reason 
= ECameraOverflow;
            
break;
        
default:
            iEikEnv
->HandleError( aError );
            reason 
= ECameraOverflow;
        }

    
if ( reason )
        {  
        delete iBitmapSave;
        iBitmapSave 
= NULL;        
        }
    }

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::DoCancel()    
    
    Description: Cancels the Active object
    Comments   : 

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::DoCancel()    
    {
    
if ( iState == EConvertingImage )
        {
        iEncoder
->Cancel();
        DeleteEncoder();
        }
    }

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::RunL()
    
    Description: called when an asynchronous request is completed
    Comments   : 

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::RunL()
{   
    
if ( iStatus == KErrNone )
    {
        
switch ( iState )
        {
            
case EStartToSaveImage:
                {
                iController.SaveImageL();
                
break;
                }
//            case EStartToRecImage:
//                {
//                    iController.RecogizeBarCodeImageL();
//                    break;
//                }

            
case EConvertingImage:
                {
                DeleteEncoder(); 
//Release captured image file
                
// Shows the status to "Image saved"
                iState = EConverted;
                iController.ShowConversionStatusL( KImageSaved );
                
break;
                }

            
case ERecognizingImage:
                {
                
// Shows the status to "Image saved"
                iState = ERecognized;
                iController.ShowConversionStatusL( KImageRecognized );
                
break;
                }

            
default:
                
break;
        }
    }
}

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::StartToSaveImage()
    
    Description: initiates a request to start to save an image
    Comments   : 

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::StartToSaveImage()
    {  
    TRequestStatus
* status=(&iStatus); 
    iState 
= EStartToSaveImage;    
    SetActive();

    User::RequestComplete(status, KErrNone);
    }

/*
-----------------------------------------------------------------------------
    void CCameraCaptureEngine::StartToRecImage()
    Description: initiates a request to start to Recognize an image to barcode
    Comments   : 
    Return values: N/A
-----------------------------------------------------------------------------

void CCameraCaptureEngine::StartToRecImage()
{
    TRequestStatus* status = (&iStatus);
    iState = EStartToRecImage;
    SetActive();
    User::RequestComplete(status, KErrNone);
}
*/
/*
-----------------------------------------------------------------------------

    TBool CCameraCaptureEngine::IsImageConversionInProgress()
    
    Description: returns whether the image conversion is in progress or not
    Comments   : 

    Return values: true if it is in progress

-----------------------------------------------------------------------------
*/
TBool CCameraCaptureEngine::IsImageConversionInProgress()
    {
    
return ( iState == EConvertingImage || iState == EStartToSaveImage || iState == EStartToRecImage );
    }

/*
-----------------------------------------------------------------------------

    TEngineState CCameraCaptureEngine::GetEngineState()
    
    Description: get the engine state
    Comments   : 

    Return values: return engine state

-----------------------------------------------------------------------------
*/
TEngineState CCameraCaptureEngine::GetEngineState()
    {
    
return iState;
    }

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::SetEngineState( TEngineState aState )
    
    Description: set the engine state
    Comments   : 

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::SetEngineState( TEngineState aState )
    {
    iState 
= aState;
    }

/*
-----------------------------------------------------------------------------

    TBool CCameraCaptureEngine::IsCameraUsedByAnotherApp()
    
    Description: return whether the camera is being used another app.
    Comments   : 

    Return values: true if it is used by another app.

-----------------------------------------------------------------------------
*/
TBool CCameraCaptureEngine::IsCameraUsedByAnotherApp()
    {
    
return (!iCameraReserved);
    }

/*
-----------------------------------------------------------------------------

    void CCameraCaptureEngine::ClientRectChangedL(TRect& aRect)
    
    Description: Notify the engine if the client rect size changes
    Comments   : 

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::ClientRectChangedL(const TRect& aRect)
    {
    
// The given client rect size is the same as the landscape picture size
    iLandscapeSize = aRect.Size();
    
// In portrait mode the height is the same, but the width needs to be
    
// calculated according to the aspect ratio
    iPortraitSize = TSize(
        (aRect.Size().iHeight 
* aRect.Size().iHeight / aRect.Size().iWidth), 
        aRect.Size().iHeight);

    iBitmapPortraitVF 
= 
        
new (ELeave) CWsBitmap( iEikEnv->WsSession());
    TInt createError 
= iBitmapPortraitVF->Create( iPortraitSize, iDisplayMode );
    User::LeaveIfError( createError );
    }

    

posted on 2007-01-08 13:28  Richard  阅读(1655)  评论(0编辑  收藏  举报