examples/ForumNokia/CameraExample/src/CameraWrapperExampleAppView.cpp

00001 /*
00002  * Copyright (c) 2009 Nokia Corporation.
00003  */
00004 
00005 // INCLUDE FILES
00006 #include <coemain.h>
00007 #include <eikon.hrh>
00008 #include <aknutils.h>
00009 #include <pathinfo.h>
00010 #include <f32file.h>
00011 #include <BAUTILS.H>
00012 #include "CameraWrapperExampleAppView.h"
00013 #include "CameraWrapperExampleAppUi.h"
00014 
00015 
00016 // ============================ MEMBER FUNCTIONS ===============================
00017 
00018 CCameraWrapperExampleAppView* CCameraWrapperExampleAppView::NewL (const TRect& aRect )
00019     {
00020     CCameraWrapperExampleAppView* self = CCameraWrapperExampleAppView::NewLC (aRect );
00021     CleanupStack::Pop (self );
00022     return self;
00023     }
00024 
00025 CCameraWrapperExampleAppView* CCameraWrapperExampleAppView::NewLC (const TRect& aRect )
00026     {
00027     CCameraWrapperExampleAppView* self = new (ELeave) CCameraWrapperExampleAppView;
00028     CleanupStack::PushL (self );
00029     self->ConstructL (aRect );
00030     return self;
00031     }
00032 
00033 void CCameraWrapperExampleAppView::ConstructL (const TRect& aRect )
00034     {
00035     // Create a window for this application view
00036     CreateWindowL ();
00037 
00038     iTitleFont = AknLayoutUtils::FontFromId(EAknLogicalFontPrimarySmallFont);
00039     
00040     iAppUi = static_cast<CCameraWrapperExampleAppUi*>(iEikonEnv->EikAppUi());
00041     
00042     // Set the windows size
00043     SetRect (aRect );
00044     
00045     // Activate the window, which makes it ready to be drawn
00046     ActivateL ();
00047     }
00048 
00049 CCameraWrapperExampleAppView::CCameraWrapperExampleAppView ()
00050     {
00051     }
00052 
00053 CCameraWrapperExampleAppView::~CCameraWrapperExampleAppView ()
00054     {
00055     if (iCameraWrapper)
00056         {
00057         iCameraWrapper->ReleaseAndPowerOff();
00058         }
00059     delete iCameraWrapper;
00060     delete iData;
00061     
00062     ReleaseBackBuffer();
00063     }
00064 
00065 TKeyResponse CCameraWrapperExampleAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
00066     {
00067     switch ( aKeyEvent.iCode )
00068         {
00069         case EKeyOK:
00070         case EStdKeyDevice3:
00071             {
00072             // Capture picture
00073             iCameraShutterFocusing = EFalse;
00074             StartFocusing();
00075             return EKeyWasConsumed;
00076             }
00077         case EKeyUpArrow:
00078             {
00079             if (iCameraWrapper->State() == CCameraEngine::EEngineViewFinding)
00080                 {
00081                 iCameraWrapper->AdjustDigitalZoom(ETrue);
00082                 }
00083             return EKeyWasConsumed;
00084             }
00085         case EKeyDownArrow:
00086             {
00087             if (iCameraWrapper->State() == CCameraEngine::EEngineViewFinding)
00088                 {
00089                 iCameraWrapper->AdjustDigitalZoom(EFalse);
00090                 }
00091             return EKeyWasConsumed;
00092             }
00093         default:
00094             {
00095             break;
00096             }
00097         };
00098     
00099     
00100     
00101     #ifdef ENABLE_CAMERA_SHUTTER
00102     // Camera shutter autofocus
00103     switch ( aKeyEvent.iScanCode )
00104         {
00105         case KStdKeyCameraFocus:
00106         case KStdKeyCameraFocus2:
00107             {
00108             // Camera shutter autofocus
00109             if (aType == EEventKeyDown)
00110                 {
00111                 if (!iAppUi->IsBackCBA())
00112                     {
00113                     iCameraShutterFocusing = ETrue;
00114                     StartFocusing();
00115                     }
00116                 return EKeyWasConsumed;
00117                 }
00118             else if (aType == EEventKeyUp)
00119                 {
00120                 // Camera state can be EEngineIdle or EEngineFocusing
00121                 if (!iAppUi->IsBackCBA() && (iCameraWrapper->State() == CCameraEngine::EEngineFocusing ||
00122                         iCameraWrapper->State() == CCameraEngine::EEngineIdle))
00123                     {
00124                     iCameraWrapper->FocusCancel();
00125                     CancelCapturedPicture();
00126                     iAppUi->UseOptionsExitCbaL();
00127                     }
00128                 return EKeyWasConsumed;
00129                 }
00130             }
00131         default:
00132             {
00133             break;
00134             }
00135         };
00136     #endif
00137     
00138     return EKeyWasNotConsumed;
00139     }
00140 
00141 void CCameraWrapperExampleAppView::CancelCapturedPicture(TBool aCleanTexts)
00142     {
00143      if (iCameraWrapper && iCameraWrapper->State() == CCameraEngine::EEngineIdle)
00144          {
00145          TRAPD(err,iCameraWrapper->StartViewFinderL(iViewFinderSize));
00146          if (aCleanTexts)
00147              {
00148              if (err)
00149                  {
00150                  SetError(_L("Camera viewfinder error %d"), err);                    
00151                  }
00152              else
00153                  {
00154                  SetTitle(_L("Camera viewfinder"));
00155                  }            
00156              }
00157         }
00158     }
00159 
00160 void CCameraWrapperExampleAppView::Draw(const TRect& /*aRect*/) const
00161     {
00162     CWindowGc& gc = SystemGc ();
00163     
00164     // Draw backbuffer that has camera picture
00165     gc.BitBlt(TPoint(0, 0), iBackBuffer);
00166     
00167     // Draw texts
00168     DrawTexts(gc);
00169     
00170     // Focus rect
00171     if (iCameraWrapper && iCameraWrapper->State() == CCameraEngine::EEngineFocusing)
00172         {
00173         gc.SetPenColor(KRgbWhite);
00174         gc.DrawRect(iFocusRect);
00175         }
00176     }
00177 
00178 void CCameraWrapperExampleAppView::DrawTexts(CWindowGc& gc) const
00179     {
00180     if (iTitle.Length()>0)
00181         {
00182         TRect rect(Rect());
00183         gc.SetPenColor(KRgbWhite);
00184         gc.UseFont(iTitleFont);
00185         gc.DrawText(iTitle, rect, rect.Height()/10, CGraphicsContext::ECenter );
00186         gc.DiscardFont();
00187         }
00188     }
00189 
00190 void CCameraWrapperExampleAppView::SizeChanged()
00191     {
00192     // Create camera wrapper class here because
00193     // whole camera wrapper and all handles have to reset
00194     // while orientatio of the application changes.
00195     if (iCameraWrapper)
00196         {
00197         // Power off camera if it is on
00198         iCameraWrapper->StopViewFinder();
00199         iCameraWrapper->ReleaseAndPowerOff();
00200         delete iCameraWrapper; iCameraWrapper = NULL;
00201         }
00202     TInt camErr(KErrNotSupported);
00203     if(CCameraEngine::CamerasAvailable() > 0)
00204       {
00205       TRAP(camErr, iCameraWrapper = CCameraEngine::NewL(0,0,this));
00206       }
00207 
00208     // iViewFinderSize is picture size for viewfinder.
00209     // iCaptureSize is picture size for capturing picture.
00210     // We want fill whole screen
00211     if (Rect().Size().iWidth > Rect().Size().iHeight)
00212         {
00213         iViewFinderSize = TSize(Rect().Size().iWidth,Rect().Size().iWidth);
00214         iCaptureSize = TSize(1280,960); // Captured picture size
00215         }
00216     else
00217         {
00218         iViewFinderSize = TSize(Rect().Size().iHeight,Rect().Size().iHeight);
00219         iCaptureSize = TSize(1280,960); // Captured picture size
00220         }
00221 
00222     // Focus rectangle
00223     iFocusRect = Rect();
00224     iFocusRect.Shrink(Rect().Size().iWidth/4, Rect().Size().iHeight/4);
00225     
00226     // Create back buffer where recieved camera pictures are copied
00227     ReleaseBackBuffer();
00228     CreateBackBufferL();
00229     
00230     // Power on camera, start viewfinder when MceoCameraReady() received
00231     if(camErr == KErrNone)
00232       {
00233       iCameraWrapper->ReserveAndPowerOn();    
00234       SetTitle(_L("Camera power on"));
00235       }
00236     else
00237       {
00238       SetTitle(_L("no camera found"));
00239       }
00240     }
00241 
00242 void CCameraWrapperExampleAppView::HandlePointerEventL (
00243         const TPointerEvent& aPointerEvent )
00244     {
00245     if (aPointerEvent.iType == TPointerEvent::EButton1Down)
00246         {
00247         // When pointing to screen camera capture picture
00248         if (!iAppUi->IsBackCBA() && 
00249                 iCameraWrapper && iCameraWrapper->State() == CCameraEngine::EEngineViewFinding)
00250             {
00251             iCameraShutterFocusing = EFalse;
00252             StartFocusing();
00253             }
00254         // After captureing, when pointing again to screen camera
00255         // start viewfinder again
00256         else if (!iAppUi->IsBackCBA() &&
00257                 iCameraWrapper && iCameraWrapper->State() == CCameraEngine::EEngineIdle)
00258             {
00259             CancelCapturedPicture();
00260             iAppUi->UseOptionsExitCbaL();
00261             }
00262         }
00263     }
00264 
00265 void CCameraWrapperExampleAppView::SetTitle(const TDesC& aTitle)
00266     {
00267     iTitle.Copy(aTitle);
00268     DrawNow();
00269     }
00270 
00271 void CCameraWrapperExampleAppView::SetError( const TDesC& aMsg, TInt aVal )
00272     {
00273     iTitle.Format(aMsg, aVal);
00274     DrawNow();
00275     }
00276 
00277 void CCameraWrapperExampleAppView::SetError( const TDesC& aMsg, TInt aVal1, TInt aVal2 )
00278     {
00279     iTitle.Format(aMsg, aVal1, aVal2);
00280     DrawNow();
00281     }
00282 
00283 void CCameraWrapperExampleAppView::CreateBackBufferL()
00284     {
00285     // create back buffer bitmap
00286     iBackBuffer = new (ELeave) CFbsBitmap;
00287     
00288     User::LeaveIfError( iBackBuffer->Create(Size(),EColor16M));
00289     
00290     // create back buffer graphics context
00291     iBackBufferDevice = CFbsBitmapDevice::NewL(iBackBuffer);
00292     User::LeaveIfError(iBackBufferDevice->CreateContext(iBackBufferContext));
00293     iBackBufferContext->SetPenStyle(CGraphicsContext::ESolidPen);
00294     
00295     iBackBufferContext->SetBrushColor(KRgbBlack);
00296     iBackBufferContext->Clear();
00297     }
00298 
00299 void CCameraWrapperExampleAppView::ReleaseBackBuffer()
00300     {
00301     if (iBackBufferContext)
00302         {
00303         delete iBackBufferContext;
00304         iBackBufferContext = NULL;
00305         }
00306     if (iBackBufferDevice)
00307         {
00308         delete iBackBufferDevice;
00309         iBackBufferDevice = NULL;
00310         }
00311     if (iBackBuffer)
00312         {
00313         delete iBackBuffer;
00314         iBackBuffer = NULL;
00315         }
00316     }
00317 
00318 void CCameraWrapperExampleAppView::MceoCameraReady()
00319     {
00320     iAppUi->UseOptionsExitCbaL();
00321     
00322     if (iCameraWrapper->State() == CCameraEngine::EEngineIdle)
00323         {
00324         // Prepare camera
00325         TRAPD(err,iCameraWrapper->PrepareL(iCaptureSize));
00326         if (err)
00327             {
00328             SetError(_L("Camera prepare error %d"), err);
00329             return;
00330             }
00331         
00332         // Start viewfinder. Viewfinder pictures starts coming into MceoViewFinderFrameReady();
00333         TRAPD(err2,iCameraWrapper->StartViewFinderL(iViewFinderSize));
00334         if (err2)
00335             {
00336             SetError(_L("Camera start viewfinder error %d"), err2);
00337             return;
00338             }
00339 
00340         SetTitle(_L("Camera viewfinder"));
00341         }
00342     }
00343 
00344 void CCameraWrapperExampleAppView::Capture()
00345     {
00346     // This method is called when picture is focused with camera shutter and pressed whole down
00347     // as taking a new picture
00348     #ifdef ENABLE_CAMERA_SHUTTER
00349     if (iCameraWrapper && !iAppUi->IsBackCBA())
00350         {
00351         // No focus supported
00352         SetTitle(_L("Capturing picture"));
00353         iCameraWrapper->StopViewFinder();
00354         TRAPD(err,iCameraWrapper->CaptureL());
00355         if (err)
00356             {
00357             SetError(_L("Camera capture error %d"), err);                    
00358             }
00359         }
00360     #endif
00361     }
00362 
00363 void CCameraWrapperExampleAppView::StartFocusing()
00364     {
00365     if (iCameraWrapper && iCameraWrapper->State() == CCameraEngine::EEngineViewFinding)
00366         {
00367         if (!iCameraWrapper->IsAutoFocusSupported())
00368             {
00369             // No focus supported
00370             SetTitle(_L("Capturing picture"));
00371             iCameraWrapper->StopViewFinder();
00372             TRAPD(err,iCameraWrapper->CaptureL());
00373             if (err)
00374                 {
00375                 SetError(_L("Camera capture error %d"), err);                    
00376                 }
00377             }
00378         else
00379             {
00380             // Focusing supported
00381             iCameraWrapper->StartFocusL();
00382             SetTitle(_L("Autofocusing..."));                    
00383             }
00384         }
00385     }
00386 
00387 void CCameraWrapperExampleAppView::MceoFocusComplete()
00388     {
00389     // CameraEngine state is EEngineIdle
00390     SetTitle(_L("Focused"));                    
00391 
00392     if (iCameraShutterFocusing)
00393         {
00394         // Leave as focused. User must press whole camera shutter down for capturing
00395         // then CCameraWrapperExampleAppView::Capture() is called
00396         }
00397     else
00398         {
00399         // Capture picture after it has focused
00400         iCameraWrapper->StopViewFinder();
00401         TRAPD(err,iCameraWrapper->CaptureL());
00402         if (err)
00403             {
00404             SetError(_L("Camera capture error %d"), err);                    
00405             }
00406         }
00407     }
00408 
00409 void CCameraWrapperExampleAppView::MceoCapturedDataReady( TDesC8* aData )
00410     {
00411     SetTitle(_L("Saving picture..."));
00412     
00413     delete iData; iData = NULL;
00414     iData = aData->Alloc();
00415 
00416     if (iCameraWrapper)
00417         iCameraWrapper->ReleaseImageBuffer();
00418 
00419     TRAP_IGNORE(iAppUi->UseOptionsBackCbaL());
00420     
00421     StorePicture(iData);
00422     }
00423 
00424 void CCameraWrapperExampleAppView::StorePicture( TDesC8* aData )
00425     {
00426     // Create path for filename
00427     TFileName path = PathInfo::PhoneMemoryRootPath(); 
00428     path.Append(PathInfo::ImagesPath());
00429 
00430     // Ensure that path exists
00431     BaflUtils::EnsurePathExistsL(iEikonEnv->FsSession(),path);
00432 
00433     // Get next free filename for the image
00434     TFileName fileToSave;
00435     TBool fileExists = ETrue;
00436     for (TInt i=1 ; i<100 ; i++)
00437         {
00438         fileToSave.Copy(path);
00439         fileToSave.Append(_L("cw_image_"));
00440         fileToSave.AppendNum(i);
00441         fileToSave.Append(_L(".jpg"));
00442         fileExists = BaflUtils::FileExists(iEikonEnv->FsSession(),fileToSave);
00443         if (!fileExists)
00444             {
00445             break;
00446             }
00447         }
00448 
00449     // Save file
00450     if (!fileExists)
00451         {
00452         RFile file;
00453         TInt err = file.Create(iEikonEnv->FsSession(),fileToSave,EFileWrite);
00454         if (!err)
00455             {
00456             file.Write(*aData);
00457             file.Close();
00458             SetTitle(fileToSave);
00459             }
00460         else
00461             {
00462             SetError(_L("File saving error %d"),err);
00463             }
00464         }
00465     else
00466         {
00467         SetTitle(_L("File not saved, delete old pictures!"));
00468         }
00469     }
00470 
00471 
00472 void CCameraWrapperExampleAppView::MceoCapturedBitmapReady( CFbsBitmap* aBitmap )
00473     {
00474     if (iBackBufferContext)
00475         {
00476         TSize bmpSizeInPixels = aBitmap->SizeInPixels();
00477         TInt xDelta = (Rect().Width() - bmpSizeInPixels.iWidth) / 2;
00478         TInt yDelta = (Rect().Height() - bmpSizeInPixels.iHeight) / 2;
00479         TPoint pos( xDelta, yDelta );
00480 
00481         // Copy received viewfinder picture to back buffer
00482         iBackBufferContext->BitBlt( pos, aBitmap, TRect( TPoint( 0, 0 ), bmpSizeInPixels ));
00483 
00484         // Update backbuffer into screen 
00485         SetTitle(_L("New picture"));
00486         }
00487     if (iCameraWrapper)
00488         iCameraWrapper->ReleaseImageBuffer();
00489     }
00490 
00491 void CCameraWrapperExampleAppView::MceoViewFinderFrameReady( CFbsBitmap& aFrame )
00492     {
00493     if (iBackBufferContext)
00494         {
00495         TSize bmpSizeInPixels = aFrame.SizeInPixels();
00496         TInt xDelta = (Rect().Width() - bmpSizeInPixels.iWidth) / 2;
00497         TInt yDelta = (Rect().Height() - bmpSizeInPixels.iHeight) / 2;
00498         TPoint pos( xDelta, yDelta );
00499 
00500         // Copy received viewfinder picture to back buffer
00501         iBackBufferContext->BitBlt( pos, &aFrame, TRect( TPoint( 0, 0 ), bmpSizeInPixels ));
00502 
00503         // Update backbuffer into screen 
00504         DrawNow();
00505         }
00506     if (iCameraWrapper)
00507         iCameraWrapper->ReleaseViewFinderBuffer();
00508     }
00509 
00510 void CCameraWrapperExampleAppView::MceoHandleError( TCameraEngineError aErrorType, TInt aError )
00511     {
00512     // NOTE: CameraEngine state seems to go into CCameraEngine::EEngineIdle state
00513     
00514     if (aErrorType == EErrReserve)
00515         {
00516         return; //-18 comes on application startup, but everything works ok
00517         }
00518 
00519     switch (aErrorType)
00520         {
00521         case EErrReserve:
00522             {
00523             SetError(_L("Camera reserved error  (%d)"), aError);
00524             break;
00525             }
00526         case EErrPowerOn:
00527             {
00528             SetError(_L("Camera power on error  (%d)"), aError);
00529             break;
00530             }
00531         case EErrViewFinderReady:
00532             {
00533             SetError(_L("Camera viewfinder error  (%d)"), aError);
00534             break;
00535             }
00536         case EErrImageReady:
00537             {
00538             SetError(_L("Camera image ready error  (%d)"), aError);
00539             break;
00540             }
00541         case EErrAutoFocusInit:
00542         case EErrAutoFocusMode:
00543         case EErrAutoFocusArea:
00544         case EErrAutoFocusRange:
00545         case EErrAutoFocusType:
00546         case EErrOptimisedFocusComplete:
00547             {
00548             SetTitle(_L("Try focusing again"));
00549             break;
00550             }
00551         default:
00552             {
00553             SetError(_L("Error %d (%d)"), aErrorType, aError);
00554             break;
00555             }
00556         };
00557     
00558     // Try handle error
00559     CancelCapturedPicture(EFalse);
00560     iAppUi->UseOptionsExitCbaL();
00561     }
00562 
00563 void CCameraWrapperExampleAppView::MceoHandleOtherEvent( const TECAMEvent& /*aEvent*/ )
00564     {
00565     }
00566 
00567 
00568 // End of File

Generated by  doxygen 1.6.2