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 #define SAFE_DELETE(x) if (x) { delete x; x = null; }
23 using namespace Tizen::Base;
24 using namespace Tizen::Base::Collection;
25 using namespace Tizen::System;
26 using namespace Tizen::App;
27 using namespace Tizen::Ui;
28 using namespace Tizen::Ui::Controls;
29 using namespace Tizen::Graphics;
30 using namespace Tizen::Uix::Vision;
31 using namespace Tizen::Media;
33 static const int X_OFFSET_FOR_CANVAS = 30;
34 static const int Y_OFFSET_FOR_CAMVAS = 30;
35 static const int PREVIEW_WIDTH = 320;
36 static const int PREVIEW_HEIGHT = 240;
37 static const int LINE_WIDTH = 4;
38 static const unsigned int FG_COLOR = 0xFF0000FF;
39 static const int FONT_SIZE = 40;
41 FaceTrackerForm::FaceTrackerForm(void)
42 : __pFaceDetector(null)
43 , __pFaceRecognizer(null)
45 , __pFInfoReference(null)
46 , __pOverlayRegion(null)
55 FaceTrackerForm::~FaceTrackerForm(void)
57 __pFaceThread->Stop();
58 __pFaceThread->Join();
59 SAFE_DELETE(__pFaceThread);
60 SAFE_DELETE(__pCamera);
61 SAFE_DELETE(__pOverlayRegion);
62 SAFE_DELETE(__pCanvas);
63 SAFE_DELETE(__pFaceDetector);
64 SAFE_DELETE(__pFaceRecognizer);
65 SAFE_DELETE(__pFInfoReference);
66 SAFE_DELETE(__pFaceData);
70 FaceTrackerForm::Initialize(void)
72 Construct(L"IDF_FORM");
78 FaceTrackerForm::OnInitializing(void)
84 FaceBuffer *pFaceBuffer = null;
86 ByteBuffer *pBuffer = null;
87 Bitmap *pBitmap = null;
90 __rect = GetClientAreaBounds();
91 Rectangle rect(__rect.x + X_OFFSET_FOR_CANVAS, Y_OFFSET_FOR_CAMVAS, PREVIEW_WIDTH, PREVIEW_HEIGHT);
93 __pFaceDetector = new (std::nothrow) FaceDetector();
94 TryReturn(__pFaceDetector != null, E_FAILURE, "Failed to create FaceDetector");
95 r = __pFaceDetector->Construct();
96 TryReturn(r == E_SUCCESS, E_FAILURE, "Failed to construct FaceDetector");
98 __pFaceRecognizer = new (std::nothrow) FaceRecognizer();
99 TryReturn(__pFaceRecognizer != null, E_FAILURE, "Failed to create FaceRecognizer");
100 r = __pFaceRecognizer->Construct();
101 TryReturn(r == E_SUCCESS, E_FAILURE, "Failed to construct FaceRecognizer");
103 __pFaceData = new (std::nothrow) FaceData();
104 TryReturn(__pFaceData != null, E_FAILURE, "Failed to create FaceData");
106 Image *pImage = new (std::nothrow) Image();
107 TryReturn(pImage != null, E_FAILURE, "Failed to create Image");
108 r = pImage->Construct();
109 TryCatch(r == E_SUCCESS, r = E_FAILURE, "Failed to construct Image");
111 pBuffer = pImage->DecodeToBufferN(Tizen::App::App::GetInstance()->GetAppRootPath()+L"res/sample1.bmp", BITMAP_PIXEL_FORMAT_RGB565,width,height);
114 pBitmap = new (std::nothrow) Bitmap();
115 TryReturn(pBitmap != null, E_FAILURE, "Failed to create Bitmap");
116 r = pBitmap->Construct(*pBuffer, Dimension(width, height),BITMAP_PIXEL_FORMAT_RGB565 );
117 TryCatch(r == E_SUCCESS, r = E_FAILURE, "Failed to construct Bitmap");
119 pLabel = new (std::nothrow) Label();
120 TryCatch(pLabel != null, r = E_FAILURE, "Failed to create Label");
121 r = pLabel->Construct(Rectangle(__rect.width - X_OFFSET_FOR_CANVAS - PREVIEW_WIDTH, Y_OFFSET_FOR_CAMVAS, PREVIEW_WIDTH, PREVIEW_HEIGHT), L"");
122 TryCatch(r == E_SUCCESS, r = E_FAILURE, "Failed to construct Label");
124 pLabel->SetBackgroundBitmap(*pBitmap);
126 SAFE_DELETE(pBitmap);
128 pFaceBuffer = __pFaceDetector->PreprocessDataN(*pBuffer, Dimension(width, height), PIXEL_FORMAT_RGB565);
129 if (pFaceBuffer != null)
131 IList *pFaceDetectList = __pFaceDetector->DetectFacesN(*pFaceBuffer, FACE_DETECTION_OPTION_FAST);
132 if (pFaceDetectList != null)
134 if (pFaceDetectList->GetCount() > 0)
136 Rectangle *pFaceRect = (Tizen::Graphics::Rectangle*)pFaceDetectList->GetAt(0);
137 FaceComponentsPosition* pFaceComponents = __pFaceDetector->ExtractFaceComponentsN(*pFaceBuffer, *pFaceRect);
140 ByteBuffer* pFeatureBuffer = __pFaceRecognizer->ExtractFeatureN(*pFaceBuffer,*pFaceComponents);
141 if (pFeatureBuffer != null)
143 __pFInfoReference = new (std::nothrow) FaceRecognitionInfo();
144 if (__pFInfoReference != null)
146 r = __pFInfoReference->Construct(*pFeatureBuffer);
148 SAFE_DELETE(pFeatureBuffer);
150 SAFE_DELETE(pFaceComponents);
153 pFaceDetectList->RemoveAll(true);
154 SAFE_DELETE(pFaceDetectList);
156 SAFE_DELETE(pFaceBuffer);
158 if (__pFInfoReference == null || r != E_SUCCESS)
163 __pPanel = new (std::nothrow) Panel();
164 TryReturn(__pPanel != null, E_FAILURE, "Failed to create Panel");
165 r = __pPanel->Construct(rect);
166 TryReturn(r == E_SUCCESS, E_FAILURE, "Failed to construct Panel");
167 AddControl(*__pPanel);
168 __pCanvas = __pPanel->GetCanvasN();
169 __pCanvas->SetLineWidth(LINE_WIDTH);
170 __pCanvas->SetForegroundColor(FG_COLOR);
171 __pCanvas->SetBackgroundColor(0x0);
173 r = font.Construct(FONT_STYLE_PLAIN, FONT_SIZE);
174 TryReturn(r == E_SUCCESS, E_FAILURE, "Failed to construct Font");
175 __pCanvas->SetFont(font);
178 __pCamera = new (std::nothrow) Camera();
179 TryReturn(__pCamera != null, E_FAILURE, "Failed to create Camera");
180 r = __pCamera->Construct(*this, CAMERA_PRIMARY);
181 TryReturn(r == E_SUCCESS, E_FAILURE, "Failed to construct Camera");
183 __pFaceThread = new (std::nothrow) FaceThread();
184 TryReturn(__pFaceThread != null, E_FAILURE, "Failed to create FaceThread");
185 r = __pFaceThread->Construct();
186 TryReturn(r == E_SUCCESS, E_FAILURE, "Failed to construct FaceThread");
187 __pFaceThread->SetFaceDetectData(__pFaceDetector, __pFaceRecognizer, __pFInfoReference, __pFaceData, this);
188 __pFaceThread->Start();
189 __pFaceData->SetThreadCompleted(true);
191 SetFormBackEventListener(this);
196 SAFE_DELETE(pBitmap);
203 FaceTrackerForm::OnTerminating(void)
210 FaceTrackerForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
212 UiApp* pApp = UiApp::GetInstance();
218 FaceTrackerForm::StartCamera(void)
220 BatteryLevel level = BATTERY_FULL;
222 Tizen::System::Battery::GetCurrentLevel(level);
224 result r = CheckBatteryLevel(level);
230 if (!__pCamera->IsPoweredOn())
232 __pCamera->PowerOn();
235 if (__pCamera->IsPoweredOn())
237 __pCamera->SetPreviewFormat(PIXEL_FORMAT_YCbCr420_PLANAR);
238 __pCamera->SetPreviewResolution(Dimension(PREVIEW_WIDTH, PREVIEW_HEIGHT));
240 __rect = GetClientAreaBounds();
241 Rectangle rect(__rect.x + X_OFFSET_FOR_CANVAS, Y_OFFSET_FOR_CAMVAS, PREVIEW_WIDTH, PREVIEW_HEIGHT);
243 bool modified = false;
244 OverlayRegion::EvaluateBounds(OVERLAY_REGION_EVALUATION_OPTION_LESS_THAN, rect, modified);
245 __pOverlayRegion = GetOverlayRegionN(rect, OVERLAY_REGION_TYPE_PRIMARY_CAMERA);
246 __pOverlayRegion->GetBackgroundBufferInfo(__bufferInfo);
248 r = __pCamera->StartPreview(&__bufferInfo, true);
249 TryCatch(r == E_SUCCESS, , "Start Preview failed");
251 r = Tizen::System::PowerManager::KeepScreenOnState(true, false);
260 r = msgBox.Construct("Warning","Camera is busy. Terminating App.",MSGBOX_STYLE_OK);
261 TryReturn(r == E_SUCCESS, , "Failed to construct MessageBox");
262 msgBox.ShowAndWait(modalResult);
264 if(modalResult == MSGBOX_RESULT_OK)
266 Application::GetInstance()->Terminate();
273 FaceTrackerForm::StopCamera(void)
278 if (__pCamera->IsPoweredOn())
280 __pCamera->StopPreview();
281 __pCamera->PowerOff();
284 __isCameraOn = false;
285 SAFE_DELETE(__pOverlayRegion);
289 FaceTrackerForm::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList *pArgs)
291 UiApp* pApp = UiApp::GetInstance();
292 if(pApp->GetAppUiState() != APP_UI_STATE_FOREGROUND)
297 if (__isCameraOn == true)
299 __pCanvas->SetBackgroundColor(0x0);
301 Rectangle faceRect = __pFaceData->GetRectangle();
302 if(__pFaceData->GetFaceDetected())
304 __pCanvas->DrawRectangle(faceRect);
305 __pCanvas->DrawText(Point(faceRect.x, faceRect.y), Tizen::Base::Integer::ToString(__pFaceData->GetSimilarity()),Tizen::Graphics::Color(0xFF0000FF));
317 FaceTrackerForm::CheckBatteryLevel(BatteryLevel level)
319 bool isCharging = false;
323 Tizen::System::RuntimeInfo::GetValue(String(L"IsCharging"),isCharging);
330 if ((level == BATTERY_EMPTY) || (level == BATTERY_CRITICAL) || (level == BATTERY_LOW))
332 result r = msgBox.Construct("Warning","Battery Low.\nPlease charge to Use.",MSGBOX_STYLE_OK);
333 TryReturn(r == E_SUCCESS, E_FAILURE, "Failed to construct MessageBox");
334 msgBox.ShowAndWait(modalResult);
336 if(modalResult == MSGBOX_RESULT_OK)
338 Application::GetInstance()->Terminate();
348 FaceTrackerForm::OnCameraPreviewed( Tizen::Base::ByteBuffer& previewedData, result r)
350 if(!(__pFaceData->GetThreadCompleted()))
354 ArrayList *pList = null;
355 ByteBuffer* pBuffer = new (std::nothrow) ByteBuffer();
356 TryReturn(pBuffer != null, , "Failed to create ByteBuffer");
357 pBuffer->Construct(previewedData);
358 TryCatch(r == E_SUCCESS, , "Failed to construct ByteBuffer");
360 pList = new (std::nothrow) ArrayList;
361 TryCatch(pList != null, , "Failed to create ArrayList");
363 TryCatch(r == E_SUCCESS, , "Failed to construct ArrayList");
364 pList->InsertAt (*pBuffer, 0);
365 __pFaceThread->SendUserEvent(RUN_MODE_FACE_DETECTION, pList);
369 SAFE_DELETE(pBuffer);
374 FaceTrackerForm::OnCameraAutoFocused(bool completeCondition)
379 FaceTrackerForm::OnCameraCaptured( Tizen::Base::ByteBuffer& capturedData, result r)
384 FaceTrackerForm::OnCameraErrorOccurred(Tizen::Media::CameraErrorReason r )