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

📄 camerawrapperexampleappview.cpp

📁 This Symbian C++ code example demonstrates how to easily use the onboard camera with zoom and autofo
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    iBackBuffer = new (ELeave) CFbsBitmap;
    
    User::LeaveIfError( iBackBuffer->Create(Size(),EColor16M));
    
    // create back buffer graphics context
    iBackBufferDevice = CFbsBitmapDevice::NewL(iBackBuffer);
    User::LeaveIfError(iBackBufferDevice->CreateContext(iBackBufferContext));
    iBackBufferContext->SetPenStyle(CGraphicsContext::ESolidPen);
    
    iBackBufferContext->SetBrushColor(KRgbBlack);
    iBackBufferContext->Clear();
    }

void CCameraWrapperExampleAppView::ReleaseBackBuffer()
    {
    if (iBackBufferContext)
        {
        delete iBackBufferContext;
        iBackBufferContext = NULL;
        }
    if (iBackBufferDevice)
        {
        delete iBackBufferDevice;
        iBackBufferDevice = NULL;
        }
    if (iBackBuffer)
        {
        delete iBackBuffer;
        iBackBuffer = NULL;
        }
    }

void CCameraWrapperExampleAppView::MceoCameraReady()
    {
    iAppUi->UseOptionsExitCbaL();
    
    if (iCameraWrapper->State() == CCameraEngine::EEngineIdle)
        {
        // Prepare camera
        TRAPD(err,iCameraWrapper->PrepareL(iCaptureSize));
        if (err)
            {
            SetError(_L("Camera prepare error %d"), err);
            return;
            }
        
        // Start viewfinder. Viewfinder pictures starts coming into MceoViewFinderFrameReady();
        TRAPD(err2,iCameraWrapper->StartViewFinderL(iViewFinderSize));
        if (err2)
            {
            SetError(_L("Camera start viewfinder error %d"), err2);
            return;
            }

        SetTitle(_L("Camera viewfinder"));
        }
    }

void CCameraWrapperExampleAppView::Capture()
    {
    // This method is called when picture is focused with camera shutter and pressed whole down
    // as taking a new picture
    #ifdef ENABLE_CAMERA_SHUTTER
    if (iCameraWrapper && !iAppUi->IsBackCBA())
        {
        // No focus supported
        SetTitle(_L("Capturing picture"));
        iCameraWrapper->StopViewFinder();
        TRAPD(err,iCameraWrapper->CaptureL());
        if (err)
            {
            SetError(_L("Camera capture error %d"), err);                    
            }
        }
    #endif
    }

void CCameraWrapperExampleAppView::StartFocusing()
    {
    if (iCameraWrapper && iCameraWrapper->State() == CCameraEngine::EEngineViewFinding)
        {
        if (!iCameraWrapper->IsAutoFocusSupported())
            {
            // No focus supported
            SetTitle(_L("Capturing picture"));
            iCameraWrapper->StopViewFinder();
            TRAPD(err,iCameraWrapper->CaptureL());
            if (err)
                {
                SetError(_L("Camera capture error %d"), err);                    
                }
            }
        else
            {
            // Focusing supported
            iCameraWrapper->StartFocusL();
            SetTitle(_L("Autofocusing..."));                    
            }
        }
    }

void CCameraWrapperExampleAppView::MceoFocusComplete()
    {
    // CameraEngine state is EEngineIdle
    SetTitle(_L("Focused"));                    

    if (iCameraShutterFocusing)
        {
        // Leave as focused. User must press whole camera shutter down for capturing
        // then CCameraWrapperExampleAppView::Capture() is called
        }
    else
        {
        // Capture picture after it has focused
        iCameraWrapper->StopViewFinder();
        TRAPD(err,iCameraWrapper->CaptureL());
        if (err)
            {
            SetError(_L("Camera capture error %d"), err);                    
            }
        }
    }

void CCameraWrapperExampleAppView::MceoCapturedDataReady( TDesC8* aData )
    {
    SetTitle(_L("Saving picture..."));
    
    delete iData; iData = NULL;
    iData = aData->Alloc();

    if (iCameraWrapper)
        iCameraWrapper->ReleaseImageBuffer();

    TRAP_IGNORE(iAppUi->UseOptionsBackCbaL());
    
    StorePicture(iData);
    }

void CCameraWrapperExampleAppView::StorePicture( TDesC8* aData )
    {
    // Create path for filename
    TFileName path = PathInfo::PhoneMemoryRootPath(); 
    path.Append(PathInfo::ImagesPath());

    // Ensure that path exists
    BaflUtils::EnsurePathExistsL(iEikonEnv->FsSession(),path);

    // Get next free filename for the image
    TFileName fileToSave;
    TBool fileExists = ETrue;
    for (TInt i=1 ; i<100 ; i++)
        {
        fileToSave.Copy(path);
        fileToSave.Append(_L("cw_image_"));
        fileToSave.AppendNum(i);
        fileToSave.Append(_L(".jpg"));
        fileExists = BaflUtils::FileExists(iEikonEnv->FsSession(),fileToSave);
        if (!fileExists)
            {
            break;
            }
        }

    // Save file
    if (!fileExists)
        {
        RFile file;
        TInt err = file.Create(iEikonEnv->FsSession(),fileToSave,EFileWrite);
        if (!err)
            {
            file.Write(*aData);
            file.Close();
            SetTitle(fileToSave);
            }
        else
            {
            SetError(_L("File saving error %d"),err);
            }
        }
    else
        {
        SetTitle(_L("File not saved, delete old pictures!"));
        }
    }


void CCameraWrapperExampleAppView::MceoCapturedBitmapReady( CFbsBitmap* aBitmap )
    {
    if (iBackBufferContext)
        {
        TSize bmpSizeInPixels = aBitmap->SizeInPixels();
        TInt xDelta = (Rect().Width() - bmpSizeInPixels.iWidth) / 2;
        TInt yDelta = (Rect().Height() - bmpSizeInPixels.iHeight) / 2;
        TPoint pos( xDelta, yDelta );

        // Copy received viewfinder picture to back buffer
        iBackBufferContext->BitBlt( pos, aBitmap, TRect( TPoint( 0, 0 ), bmpSizeInPixels ));

        // Update backbuffer into screen 
        SetTitle(_L("New picture"));
        }
    if (iCameraWrapper)
        iCameraWrapper->ReleaseImageBuffer();
    }

void CCameraWrapperExampleAppView::MceoViewFinderFrameReady( CFbsBitmap& aFrame )
    {
    if (iBackBufferContext)
        {
        TSize bmpSizeInPixels = aFrame.SizeInPixels();
        TInt xDelta = (Rect().Width() - bmpSizeInPixels.iWidth) / 2;
        TInt yDelta = (Rect().Height() - bmpSizeInPixels.iHeight) / 2;
        TPoint pos( xDelta, yDelta );

        // Copy received viewfinder picture to back buffer
        iBackBufferContext->BitBlt( pos, &aFrame, TRect( TPoint( 0, 0 ), bmpSizeInPixels ));

        // Update backbuffer into screen 
        DrawNow();
        }
    if (iCameraWrapper)
        iCameraWrapper->ReleaseViewFinderBuffer();
    }

void CCameraWrapperExampleAppView::MceoHandleError( TCameraEngineError aErrorType, TInt aError )
    {
    // NOTE: CameraEngine state seems to go into CCameraEngine::EEngineIdle state
    
    if (aErrorType == EErrReserve)
        {
        return; //-18 comes on application startup, but everything works ok
        }

    switch (aErrorType)
        {
        case EErrReserve:
            {
            SetError(_L("Camera reserved error  (%d)"), aError);
            break;
            }
        case EErrPowerOn:
            {
            SetError(_L("Camera power on error  (%d)"), aError);
            break;
            }
        case EErrViewFinderReady:
            {
            SetError(_L("Camera viewfinder error  (%d)"), aError);
            break;
            }
        case EErrImageReady:
            {
            SetError(_L("Camera image ready error  (%d)"), aError);
            break;
            }
        case EErrAutoFocusInit:
        case EErrAutoFocusMode:
        case EErrAutoFocusArea:
        case EErrAutoFocusRange:
        case EErrAutoFocusType:
        case EErrOptimisedFocusComplete:
            {
            SetTitle(_L("Try focusing again"));
            break;
            }
        default:
            {
            SetError(_L("Error %d (%d)"), aErrorType, aError);
            break;
            }
        };
    
    // Try handle error
    CancelCapturedPicture(EFalse);
    iAppUi->UseOptionsExitCbaL();
    }

void CCameraWrapperExampleAppView::MceoHandleOtherEvent( const TECAMEvent& /*aEvent*/ )
    {
    }


// End of File

⌨️ 快捷键说明

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