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

📄 cameracaptureengine_3rd_ed.cpp

📁 Symbian智能手机操作系统源代码值的参考_摄像头
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        if (aEvent.iEventType == KUidECamEventReserveComplete)
            {
            iCameraReserveComplete = ETrue;
            if ( iStart )
                {
                iPowering = ETrue;
                iCamera->PowerOn();
                }
            else
                {
                ReleaseCamera();
                }
            }
        else if (aEvent.iEventType == KUidECamEventPowerOnComplete)
            {
            HandleError( aEvent.iErrorCode );
            iPowering = EFalse;
            if ( iStart )//The Operation is not cancelled
                {
                TRAPD( err, DoViewFinderL() );
                HandleError( err );
                }
            else
                {
                ReleaseCamera();
                }
            }
        else if (aEvent.iEventType == KUidECamEventCameraNoLongerReserved)
            {
            // We lost control of the camera
            }
        else
            {
            // Handle unknown event
            }
        }
    else if(aEvent.iErrorCode == KErrNotReady)
        {
        // A request was made out of the correct sequence
        }
    else if(aEvent.iErrorCode == KErrAccessDenied)
        {
        // Reserve() failed, because a higher priority client already controls 
        // the camera
        iCameraReserved = EFalse;
        HandleError( aEvent.iErrorCode );
        }
    else
        {
        // Handle unknown error
        }
    }

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

	void CCameraCaptureEngine::ViewFinderReady(MCameraBuffer &aCameraBuffer, 
          TInt aError)

	Description: From MCameraObserver2
	Comments   : 

    Return values: N/A

-----------------------------------------------------------------------------
*/
void CCameraCaptureEngine::ViewFinderReady(MCameraBuffer &aCameraBuffer, 
    TInt aError)
    {
    if (!aError)
    	{
        if ( iController.CameraMode() == ECameraPortraitMode )
            {
            // Get the bitmap of the still image    
    	    TRAPD(ignore, ClipL( aCameraBuffer.BitmapL(0) ));
        	iController.ViewFinding( *iBitmapPortraitVF );
        	}
        else
            {
            // Pass the Bitmap frame to the controller
            TRAPD(ignore, iController.ViewFinding( aCameraBuffer.BitmapL(0) ));
            }		
        }
    else
        {
        HandleError( aError );
        }
    }

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

	void CCameraCaptureEngine::ImageBufferReady(
          MCameraBuffer &aCameraBuffer, TInt aError)

	Description: From MCameraObserver2
	Comments   : 

    Return values: N/A

-----------------------------------------------------------------------------
*/	
void CCameraCaptureEngine::ImageBufferReady(MCameraBuffer &aCameraBuffer, 
    TInt aError)
    {
    if ( !aError )
   	    {
        TRAPD(ignore, iBitmapSave = &(aCameraBuffer.BitmapL(0))); // Get the still image
   	    TRAPD(err, DrawL());
       	HandleError( err );
        }
    else
   	    {
       	HandleError( aError );  
        }
    }

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

	void CCameraCaptureEngine::VideoBufferReady(
          MCameraBuffer& aCameraBuffer, TInt aError)

	Description: From MCameraObserver2
	Comments   : 

    Return values: N/A

-----------------------------------------------------------------------------
*/	
void CCameraCaptureEngine::VideoBufferReady(MCameraBuffer& /*aCameraBuffer*/, 
    TInt /*aError*/)
    {
    // We are not using video capture.
    }

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

	void CCameraCaptureEngine::ClipL(const CFbsBitmap& aFrame)

	Description: Cli[s 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);
        }
    else
        {
        fbsBitGc->DrawBitmap( TRect(iLandscapeSize), iBitmapSave );
        delete bmpDevice;
        }

    CleanupStack::PopAndDestroy(fbsBitGc);

    // 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 EConvertingImage:
                {
                DeleteEncoder(); //Release captured image file
                // Shows the status to "Image saved"
                iState = EConverted;
                iController.ShowConversionStatusL( KImageSaved );
                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);
    }


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

	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 );
    }

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

	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 );
    }
    

⌨️ 快捷键说明

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