3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.tizenopensource.org/license
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 #include "FaceTrackerForm.h"
21 using namespace Osp::Base;
22 using namespace Osp::Base::Collection;
23 using namespace Osp::System;
24 using namespace Osp::App;
25 using namespace Osp::Ui;
26 using namespace Osp::Ui::Controls;
27 using namespace Osp::Graphics;
28 using namespace Osp::Uix;
29 using namespace Osp::Media;
31 static const int X_OFFSET_FOR_CANVAS = 30;
32 static const int Y_OFFSET_FOR_CAMVAS = 30;
33 static const int PREVIEW_WIDTH = 320;
34 static const int PREVIEW_HEIGHT = 240;
35 static const int LINE_WIDTH = 4;
36 static const unsigned int FG_COLOR = 0xFF0000FF;
37 static const int FONT_SIZE = 40;
39 FaceTrackerForm::FaceTrackerForm(void)
40 : __pFaceDetector(null)
41 , __pFaceRecognizer(null)
43 , __pFInfoReference(null)
44 , __pOverlayRegion(null)
52 FaceTrackerForm::~FaceTrackerForm(void)
55 delete __pOverlayRegion;
57 delete __pFaceDetector;
58 delete __pFaceRecognizer;
59 delete __pFInfoReference;
65 FaceTrackerForm::Initialize(void)
67 Construct(L"IDF_FORM");
72 FaceTrackerForm::OnInitializing(void)
79 __rect = GetClientAreaBounds();
82 __pFaceDetector = new (std::nothrow) FaceDetector();
83 __pFaceDetector->Construct();
85 __pFaceRecognizer = new (std::nothrow) FaceRecognizer();
86 __pFaceRecognizer->Construct();
88 __pFaceData = new (std::nothrow) FaceData();
90 //Reference image setting
91 Image *pImage = new (std::nothrow) Image();
92 r = pImage->Construct();
93 ByteBuffer *pBuffer = pImage->DecodeToBufferN(Osp::App::App::GetInstance()->GetAppRootPath()+L"res/sample1.bmp", BITMAP_PIXEL_FORMAT_RGB565,width,height);
97 Bitmap* pBitmap = new (std::nothrow) Bitmap();
98 r = pBitmap->Construct(*pBuffer, Dimension(width, height),BITMAP_PIXEL_FORMAT_RGB565 );
100 Label *pLabel = new (std::nothrow) Label();
101 r = pLabel->Construct(Rectangle(__rect.width - X_OFFSET_FOR_CANVAS - PREVIEW_WIDTH, Y_OFFSET_FOR_CAMVAS, PREVIEW_WIDTH, PREVIEW_HEIGHT), L"");
102 pLabel->SetBackgroundBitmap(*pBitmap);
107 //Recognition for Reference Image
108 FaceBuffer *pFaceBuffer = __pFaceDetector->PreprocessDataN(*pBuffer, Dimension(width, height), PIXEL_FORMAT_RGB565);
109 if (pFaceBuffer != null)
111 IList *pFaceDetectList = __pFaceDetector->DetectFacesN(*pFaceBuffer, FACE_DETECTION_OPTION_ROBUST);
112 if (pFaceDetectList != null)
114 if (pFaceDetectList->GetCount() > 0)
116 Rectangle *pFaceRect = (Osp::Graphics::Rectangle*)pFaceDetectList->GetAt(0);
117 FaceComponentsPosition* pFaceComponents = __pFaceDetector->ExtractFaceComponentsN(*pFaceBuffer, *pFaceRect);
120 ByteBuffer* pFeatureBuffer = __pFaceRecognizer->ExtractFeatureN(*pFaceBuffer,*pFaceComponents);
121 if (pFeatureBuffer != null)
123 __pFInfoReference = new (std::nothrow) FaceRecognitionInfo();
124 r = __pFInfoReference->Construct(*pFeatureBuffer);
125 delete pFeatureBuffer;
126 pFeatureBuffer = null;
128 delete pFaceComponents;
129 pFaceComponents = null;
132 pFaceDetectList->RemoveAll(true);
133 delete pFaceDetectList;
134 pFaceDetectList = null;
140 //Overlayregion setting
141 bool modified = false;
142 bool isValidRect = false;
143 Rectangle rect(__rect.x + X_OFFSET_FOR_CANVAS, Y_OFFSET_FOR_CAMVAS, PREVIEW_WIDTH, PREVIEW_HEIGHT);
144 isValidRect = OverlayRegion::EvaluateBounds(OVERLAY_REGION_EVALUATION_OPTION_LESS_THAN, rect, modified);
145 __pOverlayRegion = GetOverlayRegionN(rect, OVERLAY_REGION_TYPE_PRIMARY_CAMERA);
146 __pOverlayRegion->GetBackgroundBufferInfo(__bufferInfo);
149 __pPanel = new (std::nothrow) Panel();
150 __pPanel->Construct(rect);
151 AddControl(*__pPanel);
152 __pCanvas = __pPanel->GetCanvasN();
153 __pCanvas->SetLineWidth(LINE_WIDTH);
154 __pCanvas->SetForegroundColor(FG_COLOR);
156 font.Construct(FONT_STYLE_PLAIN, FONT_SIZE);
157 __pCanvas->SetFont(font);
161 __pFaceThread = new (std::nothrow) FaceThread();
162 r = __pFaceThread->Construct();
163 __pFaceThread->SetFaceDetectData(__pFaceDetector, __pFaceRecognizer, __pFInfoReference, __pFaceData, this);
165 // Setup back event listener
166 SetFormBackEventListener(this);
172 FaceTrackerForm::OnTerminating(void)
178 FaceTrackerForm::OnFormBackRequested(Osp::Ui::Controls::Form& source)
180 UiApp* pApp = UiApp::GetInstance();
186 FaceTrackerForm::StartCamera(void)
188 BatteryLevel level = BATTERY_FULL;
190 Osp::System::Battery::GetCurrentLevel(level);
192 result r = CheckBatteryLevel(level);
198 __pCamera = new (std::nothrow) Camera();
199 r = __pCamera->Construct(*this, CAMERA_PRIMARY);
202 if (!__pCamera->IsPoweredOn())
204 __pCamera->PowerOn();
207 if (__pCamera->IsPoweredOn())
209 __pCamera->SetPreviewFormat(PIXEL_FORMAT_YCbCr420_PLANAR);
210 __pCamera->SetPreviewResolution(Dimension(PREVIEW_WIDTH, PREVIEW_HEIGHT));
211 __pCamera->StartPreview(&__bufferInfo, true);
217 //Display message and terminate app
218 msgBox.Construct("Warning","Camera is busy. Terminating App.",MSGBOX_STYLE_OK);
219 msgBox.ShowAndWait(modalResult);
221 if(modalResult == MSGBOX_RESULT_OK)
223 Application::GetInstance()->Terminate();
229 __pFaceThread->Start();
230 __pFaceData->SetThreadCompleted(true);
232 r = Osp::System::PowerManager::KeepScreenOnState(true, false);
236 FaceTrackerForm::StopCamera(void)
238 __pFaceThread->Stop();
239 __pFaceThread->Join();
244 if (__pCamera->IsPoweredOn())
246 __pCamera->StopPreview();
247 __pCamera->PowerOff();
254 FaceTrackerForm::OnUserEventReceivedN(RequestId requestId, Osp::Base::Collection::IList *pArgs)
256 UiApp* pApp = UiApp::GetInstance();
257 if(pApp->GetAppUiState() != APP_UI_STATE_FOREGROUND)
262 Rectangle faceRect = __pFaceData->GetRectangle();
263 if(__pFaceData->GetFaceDetected())
265 __pCanvas->DrawRectangle(faceRect);
266 __pCanvas->DrawText(Point(faceRect.x, faceRect.y), Osp::Base::Integer::ToString(__pFaceData->GetSimilarity()),Osp::Graphics::Color(0xFF0000FF));
272 FaceTrackerForm::CheckBatteryLevel(BatteryLevel level)
274 bool isCharging = false;
278 Osp::System::RuntimeInfo::GetValue(String(L"IsCharging"),isCharging);
285 if ((level == BATTERY_EMPTY) || (level == BATTERY_CRITICAL) || (level == BATTERY_LOW))
287 //Display message and terminate app
288 msgBox.Construct("Warning","Battery Low.\nPlease charge to Use.",MSGBOX_STYLE_OK);
289 msgBox.ShowAndWait(modalResult);
291 if(modalResult == MSGBOX_RESULT_OK)
293 Application::GetInstance()->Terminate();
301 //Camera EventListner
303 FaceTrackerForm::OnCameraPreviewed( Osp::Base::ByteBuffer& previewedData, result r)
305 if(!(__pFaceData->GetThreadCompleted()))
309 ByteBuffer* pBuffer = new (std::nothrow) ByteBuffer();
310 pBuffer->Construct(previewedData);
312 ArrayList *pList = new (std::nothrow) ArrayList;
314 pList->InsertAt (*pBuffer, 0);
315 __pFaceThread->SendUserEvent(RUN_MODE_FACE_DETECTION, pList);
319 FaceTrackerForm::OnCameraAutoFocused(bool completeCondition)
323 FaceTrackerForm::OnCameraCaptured( Osp::Base::ByteBuffer& capturedData, result r)
327 FaceTrackerForm::OnCameraErrorOccurred(Osp::Media::CameraErrorReason r )