www.pudn.com > MyP990Camera.zip > MyCameraHandler.cpp


/*****************************************************************************  
COPYRIGHT All rights reserved Sony Ericsson Mobile Communications AB 2006.  
The software is the copyrighted work of Sony Ericsson Mobile Communications AB.  
The use of the software is subject to the terms of use or of the end-user license  
agreement which accompanies or is included with the software. The software is  
provided "as is" and Sony Ericsson specifically disclaim any warranty or  
condition whatsoever regarding merchantability or fitness for a specific  
purpose, title or non-infringement. No warranty of any kind is made in  
relation to the condition, suitability, availability, accuracy, reliability,  
merchantability and/or non-infringement of the software provided herein.  
*****************************************************************************/ 
 
// MyCameraHandler.cpp 
 
#include "MyCameraHandler.h" 
#include "MyFileHandler.h" 
 
#include  
#include  
 
 
#define KVFRect TRect(TPoint(0, 0), TSize(240, 320)) 
 
 
CMyCameraHandler* CMyCameraHandler::NewLC() 
  { 
  CMyCameraHandler* self = new (ELeave) CMyCameraHandler(); 
  CleanupStack::PushL(self); 
  self->ConstructL(); 
  return self; 
  } 
 
CMyCameraHandler::~CMyCameraHandler() 
  { 
  CloseCamera(); 
  } 
 
void CMyCameraHandler::StartCamera(RWindowBase* aWindow) 
  { 
  if(aWindow) 
    iWindow = aWindow; 
 
  CloseCamera(); 
  iCamera = CCamera::NewL(*this, 0, EPriorityNormal); 
  iCamera->CameraInfo(iCameraInfo); 
  // Reserve() returns in HandleEvent with KUidECamEventReserveComplete 
  iCamera->Reserve(); 
  } 
 
void CMyCameraHandler::StopCamera() 
  { 
  CloseCamera(); 
  } 
 
TInt CMyCameraHandler::ShootPicture(CMyFileHandler* aFileHandler) 
  { 
  iFileHandler = aFileHandler; 
 
  if(iReadyToShoot && !iIsShooting) 
    { 
    iIsShooting = ETrue; 
    iCamera->CaptureImage(); 
    User::InfoPrint(_L("Shooting picture")); 
    return KErrNone; 
    } 
  return KErrNotReady; 
  } 
 
// function displays avilable formats and sizes 
void CMyCameraHandler::ListSupportedFormats() 
  { 
  if(!iReadyToShoot) 
    { 
    User::InfoPrint(_L("Camera not ready")); 
    return; 
    } 
 
  TBuf<100> buf; 
  TInt suppNo = iCameraInfo.iNumImageSizesSupported; 
  buf.Num(suppNo-1); 
  CEikonEnv::InfoWinL(_L("No of supported sizes"), buf); 
  buf.Delete(0, buf.Length()); 
 
  CCamera::TFormat imgFormat; 
  TBuf<15> formName; 
  TInt forIter(0); 
  TInt imgWidth(0), imgHeight(0); 
 
  for(; ; ++forIter) 
    { 
    switch(forIter) 
      { 
      case  0: imgFormat = CCamera::EFormatMonochrome;         formName.Append(_L("Mono")); 
        break; 
      case  1: imgFormat = CCamera::EFormat16bitRGB444;        formName.Append(_L("RGB444")); 
        break; 
      case  2: imgFormat = CCamera::EFormat16BitRGB565;        formName.Append(_L("RGB565")); 
        break; 
      case  3: imgFormat = CCamera::EFormat32BitRGB888;        formName.Append(_L("RGB888")); 
        break; 
      case  4: imgFormat = CCamera::EFormatJpeg;               formName.Append(_L("Jpeg")); 
        break; 
      case  5: imgFormat = CCamera::EFormatExif;               formName.Append(_L("Exif")); 
        break; 
      case  6: imgFormat = CCamera::EFormatFbsBitmapColor4K;   formName.Append(_L("Color4K")); 
        break; 
      case  7: imgFormat = CCamera::EFormatFbsBitmapColor64K;  formName.Append(_L("Color64K")); 
        break; 
      case  8: imgFormat = CCamera::EFormatFbsBitmapColor16M;  formName.Append(_L("Color16M")); 
        break; 
      case  9: imgFormat = CCamera::EFormatUserDefined;        formName.Append(_L("UserDef")); 
        break; 
      case  10:imgFormat = CCamera::EFormatYUV420Interleaved;  formName.Append(_L("YUV420Int")); 
        break; 
      case  11:imgFormat = CCamera::EFormatYUV420Planar;       formName.Append(_L("YUV420Pl")); 
        break; 
      case  12:imgFormat = CCamera::EFormatYUV422;             formName.Append(_L("YUV422")); 
        break; 
      case  13:imgFormat = CCamera::EFormatYUV422Reversed;     formName.Append(_L("YUV422Rev")); 
        break; 
      case  14:imgFormat = CCamera::EFormatYUV444;             formName.Append(_L("YUV444")); 
        break; 
      case  15:imgFormat = CCamera::EFormatYUV420SemiPlanar;   formName.Append(_L("YUV420Sem")); 
        break; 
      case  16:imgFormat = CCamera::EFormatFbsBitmapColor16MU; formName.Append(_L("Color16MU")); 
        break; 
      default :CEikonEnv::InfoWinL(_L("Supported formats finish"), KNullDesC); 
        return; 
      }; 
    if( !(iCameraInfo.iImageFormatsSupported & imgFormat)) 
      { 
      formName.Delete(0, formName.Length()); 
      continue; 
      } 
    CEikonEnv::InfoWinL(formName, KNullDesC); 
    TSize imgSize; 
    for( TInt i(0); iEnumerateCaptureSizes(imgSize, i, imgFormat); 
      imgWidth = imgSize.iWidth; 
      if(imgWidth != 0 ) 
        { 
        imgHeight = imgSize.iHeight; 
        buf.Append(formName); 
        buf.Append(_L(" sizeNum:")); 
        buf.AppendNum(i); 
        buf.Append(_L(" W:")); 
        buf.AppendNum(imgWidth); 
        buf.Append(_L(" H:")); 
        buf.AppendNum(imgHeight); 
        if(!CEikonEnv::QueryWinL(_L("Size: (YES to continue)"), buf)) 
          return; 
        buf.Delete(0, buf.Length()); 
        } 
      } 
    formName.Delete(0, formName.Length()); 
    } 
  } 
 
void CMyCameraHandler::DoAutoFocus() 
  { 
  if(iCamera && iReadyToShoot) 
    { 
    CCamera::CCameraAdvancedSettings* camAS; 
    camAS = CCamera::CCameraAdvancedSettings::NewL(*iCamera); 
    camAS->SetFocusMode(CCamera::CCameraAdvancedSettings::EFocusModeAuto);	 
    camAS->SetAutoFocusType(CCamera::CCameraAdvancedSettings::EAutoFocusTypeSingle); 
    camAS->SetAutoFocusLockOn(ETrue); 
    } 
  } 
 
CMyCameraHandler::CMyCameraHandler() 
 : iCamera(0), iReadyToShoot(EFalse), iIsShooting(EFalse), 
  iWindow(0), iFileHandler(0), iVFRect(KVFRect) 
  { 
  } 
 
void CMyCameraHandler::ConstructL() 
  { 
  } 
 
void CMyCameraHandler::HandleEvent(const TECAMEvent& aEvent) 
  { 
  if(aEvent.iEventType == KUidECamEventReserveComplete) 
    { 
    iCamera->SetFlashL(CCamera::EFlashForced); 
    iCamera->PowerOn(); 
    } 
  else if(aEvent.iEventType == KUidECamEventPowerOnComplete) 
    { 
    if(iWindow == NULL) 
      { 
      if(!CEikonEnv::QueryWinL(_L("PowerOnComplete"), 
                               _L("iWindow NULL, continue?"))) 
        { 
        CloseCamera(); 
        return; 
        } 
      } 
    TRect rect(TPoint(10, 55), TSize(220, 240)); 
    TRAPD(err, iCamera->StartViewFinderDirectL(CEikonEnv::Static()->WsSession(), 
                                               *CEikonEnv::Static()->ScreenDevice(), 
                                               *iWindow, 
                                               iVFRect)); 
    if(err != KErrNone) 
      { 
      CloseCamera(); 
      return; 
      } 
    iCamera->PrepareImageCaptureL(CCamera::EFormatJpeg, 0); 
    iReadyToShoot = ETrue; 
    } 
  else if(aEvent.iEventType == KUidECamEventCameraNoLongerReserved) 
    { 
    //CEikonEnv::InfoWinL(_L("HandleEvent"), _L("CameraNoLongerReserved")); 
    } 
  else 
    { 
    //CEikonEnv::InfoWinL(_L("HandleEvent"), _L("Other event")); 
    } 
  } 
 
void CMyCameraHandler::ViewFinderReady(MCameraBuffer& /*aCameraBuffer*/, 
                                       TInt aError) 
  { 
  if(aError != KErrNone) 
    ShowError(_L("ImageBufferReady"), aError); 
  } 
 
void CMyCameraHandler::ImageBufferReady(MCameraBuffer& aCameraBuffer, 
                                        TInt aError) 
  { 
  if(aError != KErrNone) 
    { 
    ShowError(_L("ImageBufferReady"), aError); 
    aCameraBuffer.Release(); 
    CloseCamera(); 
    return; 
    } 
  // camera picture is taken and avilable - save it to file if possible 
  if(iFileHandler->SaveDescToFileL(aCameraBuffer) == KErrNotReady) 
    { 
    User::InfoPrint(_L("FileHandler NotReady")); 
    aCameraBuffer.Release(); 
    } 
  else 
    User::InfoPrint(_L("Picture saved")); 
  iIsShooting = EFalse; 
  } 
 
void CMyCameraHandler::VideoBufferReady(MCameraBuffer& /*aCameraBuffer*/, 
                                        TInt /*aError*/) 
  { 
  CEikonEnv::InfoWinL(_L("VideoBufferReady"), KNullDesC); 
  } 
 
void CMyCameraHandler::CloseCamera() 
  { 
  if(iCamera) 
    { 
    iCamera->PowerOff(); 
    iCamera->Release(); 
    delete iCamera; 
    iCamera = NULL; 
    } 
  iReadyToShoot = EFalse; 
  iIsShooting = EFalse; 
  } 
 
_LIT(KErrInfo, "Err: %d"); 
void CMyCameraHandler::ShowError(const TDesC& aInfo, TInt aErr) 
  { 
  TBuf<20> buf; 
  buf.Format(KErrInfo, aErr); 
  CEikonEnv::InfoWinL(aInfo, buf); 
  }