examples/ForumNokia/CameraExample/src/CameraWrapperExampleAppView.cpp

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

Generated by  doxygen 1.6.2