⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cameracaptureengine.cpp

📁 symbian下摄像头拍照示例,symbian c 开发
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*
 * Copyright ?2008 Nokia Corporation.
 */

#include "CameraCaptureEngine.h"
#include "CamTest.hrh"

#include <AknViewAppUi.h>
#include <CamTest.rsg>
#include <centralrepository.h>
#include <ProfileEngineSDKCRKeys.h>
#include <eikenv.h>
#include <barsread.h>
#include <stringloader.h>
#include "filelogger.h"
#include "MyLogger.h"
#include <aknnotewrappers.h> 


// GetImageSizeIndexL() could be used for selecting a specific image
// size. Here, we simply select 2nd largest size (index 1)
const TUint KImageSizeIndex = 1;

/*
-------------------------------------------------------------------------------
C++ default constructor
-------------------------------------------------------------------------------
*/
CCameraCaptureEngine::CCameraCaptureEngine( CCameraAppController* aController)
: CActive( EPriorityStandard ),
  iController( aController ),
  iZoomFactor( 0 ),
  iCapturePrepared( EFalse ),
  iBitmapSave( 0 ),
  iImageExif( 0 ),
  iEncoder( 0 ),
  iCameraReserved( EFalse ),
  iAutoFocus( 0 )
    {
    CActiveScheduler::Add( this );
    iState = EEngineNotReady;
    }

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

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

    return self;
    }


/*
-------------------------------------------------------------------------------
Destructor. Frees allocated resources.
-------------------------------------------------------------------------------
*/
CCameraCaptureEngine::~CCameraCaptureEngine()
    {
    delete iAutoFocus;
    delete iEncoder;
    delete iCamera;
    delete iBitmapPortraitVF;
    delete iBitmapSave;
    delete iImageExif;

    // Cancel any outstanding request
    Cancel();
    }

/*
-------------------------------------------------------------------------------
Symbian OS 2nd phase constructor
-------------------------------------------------------------------------------
*/
void CCameraCaptureEngine::ConstructL(const TRect& aRect)
    {
    iEikEnv = CEikonEnv::Static();  // CSI: 27 # (Store a pointer to CEikonEnv)
    if ( !CCamera::CamerasAvailable() )
        {
        HandleError( KErrHardwareNotAvailable );
        return;
        }

    // camera index 0 (the main camera)
    iCamera = CCamera::NewL( *this, 0 );

    // try to construct autofocus object
    TRAPD(afErr, iAutoFocus = CCamAutoFocus::NewL( iCamera ));
    if(afErr)
        {
        iAutoFocus = 0;
        HandleError( KErrExtensionNotSupported );
        }

    iBitmapSave = new (ELeave) CFbsBitmap;

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

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

/*
-------------------------------------------------------------------------------
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;
        default:
            return CCamera::EFormatFbsBitmapColor4K;
        }
    }

/*
-------------------------------------------------------------------------------
Returns the 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
        {
        return CCamera::EFormatFbsBitmapColor4K;
        }
    }

/*
-------------------------------------------------------------------------------
Stops view finding
-------------------------------------------------------------------------------
*/
void CCameraCaptureEngine::StopViewFinder()
    {
    if ( iCameraReserved )
        {
        __LOGSTR_TOFILE("stop vf");
        iCamera->StopViewFinder();
        }
    }

/*
-------------------------------------------------------------------------------
Starts view finding and prepares image capturing
-------------------------------------------------------------------------------
*/
void CCameraCaptureEngine::DoViewFinderL()
    {
    //Default, always supported by API
    TRAP_IGNORE(iCamera->SetExposureL());
    TRAP_IGNORE(iCamera->SetDigitalZoomFactorL( iZoomFactor ));


    if ( iInfo.iOptionsSupported & TCameraInfo::EViewFinderBitmapsSupported )
        {
        if ( iInfo.iOptionsSupported & TCameraInfo::EImageCaptureSupported
            && !iCapturePrepared ) // first try capturing in EXIF JPEG format
            {
            iFormat = CCamera::EFormatExif;
            TRAPD(exifErr, iCamera->PrepareImageCaptureL(iFormat, KImageSizeIndex));
            if(exifErr) // capturing in EXIF format not supported,
                        // fall back to bitmap format
                {
                iFormat = iColorDepthHW;
                iCamera->PrepareImageCaptureL(iFormat, 1);
                }
            else    // notify controller that we're using EXIF capture mode
                {
                iCamera->EnumerateCaptureSizes( iCaptureSize, KImageSizeIndex,
                                               iFormat );

                iController->SetCaptureModeL( iCaptureSize,
                                             (TInt)iFormat,
                                             (TBool)iAutoFocus );
                }

            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);
        }
    else
        {
        //Images could be taken even without viewfinder, if following is true:
        //( iInfo.iOptionsSupported & TCameraInfo::EImageCaptureSupported )
        User::Leave(KErrNotSupported);
        }
    }

/*
-------------------------------------------------------------------------------
Reserves camera, switches power on, and eventually starts view finding
-------------------------------------------------------------------------------
*/
void CCameraCaptureEngine::StartViewFinderL()
    {
    __LOGSTR_TOFILE("start vf");
    ReserveCameraL();
    }


/*
-------------------------------------------------------------------------------
Returns the index for the requested image size, if supported by camera.
May leave with KErrNotSupported.
-------------------------------------------------------------------------------
*/
TInt CCameraCaptureEngine::GetImageSizeIndexL(const TSize& aRequestedSize,  // CSI: 40 # (Image size index is not an error code)
                                              const CCamera::TFormat& aFormat)
    {
    TCameraInfo camInfo;
    iCamera->CameraInfo(camInfo);

    for (TInt i=0; i< camInfo.iNumImageSizesSupported; i++)
        {
        TSize supportedSize;
        iCamera->EnumerateCaptureSizes(supportedSize, i, aFormat);
        if (supportedSize == aRequestedSize)
            {
            // Found the index
            return(i);
            }
        }
    // Index not found
    User::Leave(KErrNotSupported);
    return(0);
    }

/*
-------------------------------------------------------------------------------
Takes a picture
-------------------------------------------------------------------------------
*/
void CCameraCaptureEngine::SnapL()
    {
    if ( iCameraReserved && !iPowering )
        {
        iState = ESnappingPicture;
        iCamera->StopViewFinder();
        iCamera->CaptureImage();
        HBufC* resSavingImage = StringLoader::LoadLC(R_SAVING_IMAGE);
        iController->ShowConversionStatusL( *resSavingImage );
        CleanupStack::PopAndDestroy(resSavingImage);

        // On completion, MCameraObserver::ImageReady() will be called
        }
    else
        {
        User::Leave( KErrNotReady );
        }
    }


/*
-------------------------------------------------------------------------------
Sets engine's camera orientation
-------------------------------------------------------------------------------
*/
void CCameraCaptureEngine::SetOrientation(TCameraOrientation anOrientation)
    {
    iOrientation = anOrientation;
    }

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

    if (iBitmapSave)
        {
        iBitmapSave->Reset();
        }
    }

/*
-------------------------------------------------------------------------------
Converts and saves a bitmap to JPEG image
-------------------------------------------------------------------------------
*/
void CCameraCaptureEngine::SaveImageL(TJpegQualityFactor /*aQuality*/,
    const TFileName* aNewFilePathAndName, RFs* aFs)
    {
    if( iFormat == CCamera::EFormatExif )
        {
        TInt fErr = iFile.Replace( *aFs, *aNewFilePathAndName, EFileWrite );
        if (fErr == KErrNone)
            {
            iState = EConvertingImage; // no need for actual conversion step
                                       // when capturing to EXIF JPEG
            iFile.Write( *iImageExif, iStatus );
            SetActive();
            return;
            }
        }

    if ( !iEncoder )
        {
        if ( aFs && aNewFilePathAndName )
            {
            iEncoder = CImageEncoder::FileNewL(*aFs, *aNewFilePathAndName,
                KMimeType);
            }
        else
            {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -