2 * Copyright (c) 2009 Nokia Corporation.
\r
8 #include <aknutils.h>
\r
9 #include <pathinfo.h>
\r
10 #include <f32file.h>
\r
11 #include <BAUTILS.H>
\r
12 #include "ZXingBarcodeReaderAppView.h"
\r
13 #include "ZXingBarcodeReaderAppUi.h"
\r
17 // ============================ MEMBER FUNCTIONS ===============================
\r
19 CZXingBarcodeReaderAppView* CZXingBarcodeReaderAppView::NewL (const TRect& aRect )
\r
21 CZXingBarcodeReaderAppView* self = CZXingBarcodeReaderAppView::NewLC (aRect );
\r
22 CleanupStack::Pop (self );
\r
26 CZXingBarcodeReaderAppView* CZXingBarcodeReaderAppView::NewLC (const TRect& aRect )
\r
28 CZXingBarcodeReaderAppView* self = new (ELeave) CZXingBarcodeReaderAppView;
\r
29 CleanupStack::PushL (self );
\r
30 self->ConstructL (aRect );
\r
34 void CZXingBarcodeReaderAppView::ConstructL (const TRect& aRect )
\r
36 // Create a window for this application view
\r
39 iTitleFont = AknLayoutUtils::FontFromId(EAknLogicalFontPrimarySmallFont);
\r
41 iAppUi = static_cast<CZXingBarcodeReaderAppUi*>(iEikonEnv->EikAppUi());
\r
43 // Set the windows size
\r
46 //Start decoder timer.
\r
49 // Activate the window, which makes it ready to be drawn
\r
53 CZXingBarcodeReaderAppView::CZXingBarcodeReaderAppView () : iPeriodic(NULL)
\r
57 CZXingBarcodeReaderAppView::~CZXingBarcodeReaderAppView ()
\r
61 iCameraWrapper->ReleaseAndPowerOff();
\r
63 delete iCameraWrapper;
\r
66 ReleaseBackBuffer();
\r
69 TKeyResponse CZXingBarcodeReaderAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
\r
71 switch ( aKeyEvent.iCode )
\r
74 case EStdKeyDevice3:
\r
77 iCameraShutterFocusing = EFalse;
\r
79 return EKeyWasConsumed;
\r
83 if (iCameraWrapper->State() == CCameraEngine::EEngineViewFinding)
\r
85 iCameraWrapper->AdjustDigitalZoom(ETrue);
\r
87 return EKeyWasConsumed;
\r
91 if (iCameraWrapper->State() == CCameraEngine::EEngineViewFinding)
\r
93 iCameraWrapper->AdjustDigitalZoom(EFalse);
\r
96 return EKeyWasConsumed;
\r
106 #ifdef ENABLE_CAMERA_SHUTTER
\r
107 // Camera shutter autofocus
\r
108 switch ( aKeyEvent.iScanCode )
\r
110 case KStdKeyCameraFocus:
\r
111 case KStdKeyCameraFocus2:
\r
113 // Camera shutter autofocus
\r
114 if (aType == EEventKeyDown)
\r
116 if (!iAppUi->IsBackCBA())
\r
118 iCameraShutterFocusing = ETrue;
\r
121 return EKeyWasConsumed;
\r
123 else if (aType == EEventKeyUp)
\r
125 // Camera state can be EEngineIdle or EEngineFocusing
\r
126 if (!iAppUi->IsBackCBA() && (iCameraWrapper->State() == CCameraEngine::EEngineFocusing ||
\r
127 iCameraWrapper->State() == CCameraEngine::EEngineIdle))
\r
129 iCameraWrapper->FocusCancel();
\r
130 CancelCapturedPicture();
\r
131 iAppUi->UseOptionsExitCbaL();
\r
133 return EKeyWasConsumed;
\r
143 return EKeyWasNotConsumed;
\r
146 void CZXingBarcodeReaderAppView::CancelCapturedPicture(TBool aCleanTexts)
\r
148 if (iCameraWrapper && iCameraWrapper->State() == CCameraEngine::EEngineIdle)
\r
150 TRAPD(err,iCameraWrapper->StartViewFinderL(iViewFinderSize));
\r
155 SetError(_L("Camera viewfinder error %d"), err);
\r
159 SetTitle(_L("Camera viewfinder"));
\r
165 void CZXingBarcodeReaderAppView::Draw(const TRect& /*aRect*/) const
\r
167 CWindowGc& gc = SystemGc ();
\r
169 // Draw backbuffer that has camera picture
\r
170 gc.BitBlt(TPoint(0, 0), iBackBuffer);
\r
176 if (iCameraWrapper && iCameraWrapper->State() == CCameraEngine::EEngineFocusing)
\r
178 gc.SetPenColor(KRgbWhite);
\r
179 gc.DrawRect(iFocusRect);
\r
183 void CZXingBarcodeReaderAppView::DrawTexts(CWindowGc& gc) const
\r
185 if (iTitle.Length()>0)
\r
187 TRect rect(Rect());
\r
188 gc.SetPenColor(KRgbWhite);
\r
189 gc.UseFont(iTitleFont);
\r
190 gc.DrawText(iTitle, rect, rect.Height()/10, CGraphicsContext::ECenter );
\r
195 void CZXingBarcodeReaderAppView::SizeChanged()
\r
197 // Create camera wrapper class here because
\r
198 // whole camera wrapper and all handles have to reset
\r
199 // while orientatio of the application changes.
\r
200 if (iCameraWrapper)
\r
202 // Power off camera if it is on
\r
203 iCameraWrapper->StopViewFinder();
\r
204 iCameraWrapper->ReleaseAndPowerOff();
\r
205 delete iCameraWrapper; iCameraWrapper = NULL;
\r
207 TInt camErr(KErrNotSupported);
\r
208 if(CCameraEngine::CamerasAvailable() > 0)
\r
210 TRAP(camErr, iCameraWrapper = CCameraEngine::NewL(0,0,this));
\r
213 // iViewFinderSize is picture size for viewfinder.
\r
214 // iCaptureSize is picture size for capturing picture.
\r
215 // We want fill whole screen
\r
216 if (Rect().Size().iWidth > Rect().Size().iHeight)
\r
218 iViewFinderSize = TSize(Rect().Size().iWidth,Rect().Size().iWidth);
\r
219 iCaptureSize = TSize(1280,960); // Captured picture size
\r
223 iViewFinderSize = TSize(Rect().Size().iHeight,Rect().Size().iHeight);
\r
224 iCaptureSize = TSize(1280,960); // Captured picture size
\r
228 iFocusRect = Rect();
\r
229 iFocusRect.Shrink(Rect().Size().iWidth/4, Rect().Size().iHeight/4);
\r
231 // Create back buffer where recieved camera pictures are copied
\r
232 ReleaseBackBuffer();
\r
233 CreateBackBufferL();
\r
235 // Power on camera, start viewfinder when MceoCameraReady() received
\r
236 if(camErr == KErrNone)
\r
238 iCameraWrapper->ReserveAndPowerOn();
\r
239 SetTitle(_L("Camera power on"));
\r
243 SetTitle(_L("no camera found"));
\r
247 void CZXingBarcodeReaderAppView::HandlePointerEventL (
\r
248 const TPointerEvent& aPointerEvent )
\r
250 if (aPointerEvent.iType == TPointerEvent::EButton1Down)
\r
252 // When pointing to screen camera capture picture
\r
253 if (!iAppUi->IsBackCBA() &&
\r
254 iCameraWrapper && iCameraWrapper->State() == CCameraEngine::EEngineViewFinding)
\r
256 iCameraShutterFocusing = EFalse;
\r
259 // After captureing, when pointing again to screen camera
\r
260 // start viewfinder again
\r
261 else if (!iAppUi->IsBackCBA() &&
\r
262 iCameraWrapper && iCameraWrapper->State() == CCameraEngine::EEngineIdle)
\r
264 CancelCapturedPicture();
\r
265 iAppUi->UseOptionsExitCbaL();
\r
270 void CZXingBarcodeReaderAppView::SetTitle(const TDesC& aTitle)
\r
272 iTitle.Copy(aTitle);
\r
276 void CZXingBarcodeReaderAppView::SetError( const TDesC& aMsg, TInt aVal )
\r
278 iTitle.Format(aMsg, aVal);
\r
282 void CZXingBarcodeReaderAppView::SetError( const TDesC& aMsg, TInt aVal1, TInt aVal2 )
\r
284 iTitle.Format(aMsg, aVal1, aVal2);
\r
288 void CZXingBarcodeReaderAppView::CreateBackBufferL()
\r
290 // create back buffer bitmap
\r
291 iBackBuffer = new (ELeave) CFbsBitmap;
\r
293 User::LeaveIfError( iBackBuffer->Create(Size(),EColor16M));
\r
295 // create back buffer graphics context
\r
296 iBackBufferDevice = CFbsBitmapDevice::NewL(iBackBuffer);
\r
297 User::LeaveIfError(iBackBufferDevice->CreateContext(iBackBufferContext));
\r
298 iBackBufferContext->SetPenStyle(CGraphicsContext::ESolidPen);
\r
300 iBackBufferContext->SetBrushColor(KRgbBlack);
\r
301 iBackBufferContext->Clear();
\r
304 void CZXingBarcodeReaderAppView::ReleaseBackBuffer()
\r
306 if (iBackBufferContext)
\r
308 delete iBackBufferContext;
\r
309 iBackBufferContext = NULL;
\r
311 if (iBackBufferDevice)
\r
313 delete iBackBufferDevice;
\r
314 iBackBufferDevice = NULL;
\r
318 delete iBackBuffer;
\r
319 iBackBuffer = NULL;
\r
323 void CZXingBarcodeReaderAppView::MceoCameraReady()
\r
325 iAppUi->UseOptionsExitCbaL();
\r
327 if (iCameraWrapper->State() == CCameraEngine::EEngineIdle)
\r
330 TRAPD(err,iCameraWrapper->PrepareL(iCaptureSize));
\r
333 SetError(_L("Camera prepare error %d"), err);
\r
337 // Start viewfinder. Viewfinder pictures starts coming into MceoViewFinderFrameReady();
\r
338 TRAPD(err2,iCameraWrapper->StartViewFinderL(iViewFinderSize));
\r
341 SetError(_L("Camera start viewfinder error %d"), err2);
\r
345 SetTitle(_L("Camera viewfinder"));
\r
349 void CZXingBarcodeReaderAppView::Capture()
\r
351 // This method is called when picture is focused with camera shutter and pressed whole down
\r
352 // as taking a new picture
\r
353 #ifdef ENABLE_CAMERA_SHUTTER
\r
354 if (iCameraWrapper && !iAppUi->IsBackCBA())
\r
356 // No focus supported
\r
357 SetTitle(_L("Capturing picture"));
\r
358 iCameraWrapper->StopViewFinder();
\r
359 TRAPD(err,iCameraWrapper->CaptureL());
\r
362 SetError(_L("Camera capture error %d"), err);
\r
368 void CZXingBarcodeReaderAppView::StartFocusing()
\r
370 if (iCameraWrapper && iCameraWrapper->State() == CCameraEngine::EEngineViewFinding)
\r
372 if (!iCameraWrapper->IsAutoFocusSupported())
\r
374 // No focus supported
\r
375 SetTitle(_L("Capturing picture"));
\r
376 iCameraWrapper->StopViewFinder();
\r
377 TRAPD(err,iCameraWrapper->CaptureL());
\r
380 SetError(_L("Camera capture error %d"), err);
\r
385 // Focusing supported
\r
386 iCameraWrapper->StartFocusL();
\r
387 SetTitle(_L("Autofocusing..."));
\r
392 void CZXingBarcodeReaderAppView::MceoFocusComplete()
\r
394 // CameraEngine state is EEngineIdle
\r
395 SetTitle(_L("Focused"));
\r
397 if (iCameraShutterFocusing)
\r
399 // Leave as focused. User must press whole camera shutter down for capturing
\r
400 // then CZXingBarcodeReaderAppView::Capture() is called
\r
404 // Capture picture after it has focused
\r
405 iCameraWrapper->StopViewFinder();
\r
406 TRAPD(err,iCameraWrapper->CaptureL());
\r
409 SetError(_L("Camera capture error %d"), err);
\r
414 void CZXingBarcodeReaderAppView::MceoCapturedDataReady( TDesC8* aData )
\r
416 SetTitle(_L("Saving picture..."));
\r
418 delete iData; iData = NULL;
\r
419 iData = aData->Alloc();
\r
421 if (iCameraWrapper)
\r
422 iCameraWrapper->ReleaseImageBuffer();
\r
424 TRAP_IGNORE(iAppUi->UseOptionsBackCbaL());
\r
426 StorePicture(iData);
\r
429 void CZXingBarcodeReaderAppView::StorePicture( TDesC8* aData )
\r
431 // Create path for filename
\r
432 TFileName path = PathInfo::PhoneMemoryRootPath();
\r
433 path.Append(PathInfo::ImagesPath());
\r
435 // Ensure that path exists
\r
436 BaflUtils::EnsurePathExistsL(iEikonEnv->FsSession(),path);
\r
438 // Get next free filename for the image
\r
439 TFileName fileToSave;
\r
440 TBool fileExists = ETrue;
\r
441 for (TInt i=1 ; i<100 ; i++)
\r
443 fileToSave.Copy(path);
\r
444 fileToSave.Append(_L("cw_image_"));
\r
445 fileToSave.AppendNum(i);
\r
446 fileToSave.Append(_L(".jpg"));
\r
447 fileExists = BaflUtils::FileExists(iEikonEnv->FsSession(),fileToSave);
\r
458 TInt err = file.Create(iEikonEnv->FsSession(),fileToSave,EFileWrite);
\r
461 file.Write(*aData);
\r
463 SetTitle(fileToSave);
\r
467 SetError(_L("File saving error %d"),err);
\r
472 SetTitle(_L("File not saved, delete old pictures!"));
\r
477 void CZXingBarcodeReaderAppView::MceoCapturedBitmapReady( CFbsBitmap* aBitmap )
\r
479 if (iBackBufferContext)
\r
481 TSize bmpSizeInPixels = aBitmap->SizeInPixels();
\r
482 TInt xDelta = (Rect().Width() - bmpSizeInPixels.iWidth) / 2;
\r
483 TInt yDelta = (Rect().Height() - bmpSizeInPixels.iHeight) / 2;
\r
484 TPoint pos( xDelta, yDelta );
\r
486 // Copy received viewfinder picture to back buffer
\r
487 iBackBufferContext->BitBlt( pos, aBitmap, TRect( TPoint( 0, 0 ), bmpSizeInPixels ));
\r
489 // Update backbuffer into screen
\r
490 SetTitle(_L("New picture"));
\r
492 if (iCameraWrapper)
\r
493 iCameraWrapper->ReleaseImageBuffer();
\r
496 void CZXingBarcodeReaderAppView::MceoViewFinderFrameReady( CFbsBitmap& aFrame )
\r
498 if (iBackBufferContext)
\r
500 TSize bmpSizeInPixels = aFrame.SizeInPixels();
\r
501 TInt xDelta = (Rect().Width() - bmpSizeInPixels.iWidth) / 2;
\r
502 TInt yDelta = (Rect().Height() - bmpSizeInPixels.iHeight) / 2;
\r
503 TPoint pos( xDelta, yDelta );
\r
505 // Copy received viewfinder picture to back buffer
\r
506 iBackBufferContext->BitBlt( pos, &aFrame, TRect( TPoint( 0, 0 ), bmpSizeInPixels ));
\r
508 // Update backbuffer into screen
\r
511 if (iCameraWrapper)
\r
512 iCameraWrapper->ReleaseViewFinderBuffer();
\r
515 void CZXingBarcodeReaderAppView::MceoHandleError( TCameraEngineError aErrorType, TInt aError )
\r
517 // NOTE: CameraEngine state seems to go into CCameraEngine::EEngineIdle state
\r
519 if (aErrorType == EErrReserve)
\r
521 return; //-18 comes on application startup, but everything works ok
\r
524 switch (aErrorType)
\r
528 SetError(_L("Camera reserved error (%d)"), aError);
\r
533 SetError(_L("Camera power on error (%d)"), aError);
\r
536 case EErrViewFinderReady:
\r
538 SetError(_L("Camera viewfinder error (%d)"), aError);
\r
541 case EErrImageReady:
\r
543 SetError(_L("Camera image ready error (%d)"), aError);
\r
546 case EErrAutoFocusInit:
\r
547 case EErrAutoFocusMode:
\r
548 case EErrAutoFocusArea:
\r
549 case EErrAutoFocusRange:
\r
550 case EErrAutoFocusType:
\r
551 case EErrOptimisedFocusComplete:
\r
553 //SetTitle(_L("Try focusing again"));
\r
558 SetError(_L("Error %d (%d)"), aErrorType, aError);
\r
563 // Try handle error
\r
564 CancelCapturedPicture(EFalse);
\r
565 iAppUi->UseOptionsExitCbaL();
\r
568 void CZXingBarcodeReaderAppView::MceoHandleOtherEvent( const TECAMEvent& /*aEvent*/ )
\r