Tizen 2.1 base
[sdk/ide/native-sample.git] / samples / native / partner / cpp / Sample / Tizen C++ / FaceTracker / FaceTracker / project / src / FaceTrackerForm.cpp
1 //
2 // Tizen C++ SDK
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.tizenopensource.org/license
10 //
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.
16 //
17
18 #include <FApp.h>
19 #include "FaceTrackerForm.h"
20
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;
30
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;
38
39 FaceTrackerForm::FaceTrackerForm(void)
40         : __pFaceDetector(null)
41         , __pFaceRecognizer(null)
42         , __pFaceData(null)
43         , __pFInfoReference(null)
44         , __pOverlayRegion(null)
45         , __pCanvas(null)
46         , __pPanel(null)
47         , __pCamera(null)
48         , __pFaceThread(null)
49 {
50 }
51
52 FaceTrackerForm::~FaceTrackerForm(void)
53 {
54         delete __pCamera;
55         delete __pOverlayRegion;
56         delete __pCanvas;
57         delete __pFaceDetector;
58         delete __pFaceRecognizer;
59         delete __pFInfoReference;
60         delete __pFaceData;
61         delete __pFaceThread;
62 }
63
64 bool
65 FaceTrackerForm::Initialize(void)
66 {
67         Construct(L"IDF_FORM");
68
69         return true;
70 }
71 result
72 FaceTrackerForm::OnInitializing(void)
73 {
74         result r = E_SUCCESS;
75
76         int width = 0;
77         int height = 0;
78
79         __rect = GetClientAreaBounds();
80
81         //FaceDetector create
82         __pFaceDetector = new (std::nothrow) FaceDetector();
83         __pFaceDetector->Construct();
84
85         __pFaceRecognizer = new (std::nothrow) FaceRecognizer();
86         __pFaceRecognizer->Construct();
87
88         __pFaceData = new (std::nothrow) FaceData();
89
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);
94         delete pImage;
95         pImage =null;
96
97         Bitmap* pBitmap = new (std::nothrow) Bitmap();
98         r = pBitmap->Construct(*pBuffer, Dimension(width, height),BITMAP_PIXEL_FORMAT_RGB565 );
99
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);
103         AddControl(*pLabel);
104         delete pBitmap;
105         pBitmap = null;
106
107         //Recognition for Reference Image
108         FaceBuffer *pFaceBuffer = __pFaceDetector->PreprocessDataN(*pBuffer, Dimension(width, height), PIXEL_FORMAT_RGB565);
109         if (pFaceBuffer != null)
110         {
111                 IList *pFaceDetectList = __pFaceDetector->DetectFacesN(*pFaceBuffer, FACE_DETECTION_OPTION_ROBUST);
112                 if (pFaceDetectList != null)
113                 {
114                         if (pFaceDetectList->GetCount() > 0)
115                         {
116                                 Rectangle *pFaceRect = (Osp::Graphics::Rectangle*)pFaceDetectList->GetAt(0);
117                                 FaceComponentsPosition* pFaceComponents = __pFaceDetector->ExtractFaceComponentsN(*pFaceBuffer, *pFaceRect);
118                                 if(pFaceComponents)
119                                 {
120                                         ByteBuffer*  pFeatureBuffer = __pFaceRecognizer->ExtractFeatureN(*pFaceBuffer,*pFaceComponents);
121                                         if (pFeatureBuffer != null)
122                                         {
123                                                 __pFInfoReference = new (std::nothrow) FaceRecognitionInfo();
124                                                 r = __pFInfoReference->Construct(*pFeatureBuffer);
125                                                 delete pFeatureBuffer;
126                                                 pFeatureBuffer = null;
127                                         }
128                                         delete pFaceComponents;
129                                         pFaceComponents = null;
130                                 }
131                         }
132                         pFaceDetectList->RemoveAll(true);
133                         delete pFaceDetectList;
134                         pFaceDetectList = null;
135                 }
136                 delete pFaceBuffer;
137                 pFaceBuffer = null;
138         }
139
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);
147
148         //Canvas setting
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);
155         Font font;
156         font.Construct(FONT_STYLE_PLAIN, FONT_SIZE);
157         __pCanvas->SetFont(font);
158         __pCanvas->Clear();
159
160         //FaceTread setting
161         __pFaceThread = new (std::nothrow) FaceThread();
162         r = __pFaceThread->Construct();
163         __pFaceThread->SetFaceDetectData(__pFaceDetector, __pFaceRecognizer, __pFInfoReference, __pFaceData, this);
164
165         // Setup back event listener
166         SetFormBackEventListener(this);
167
168         return r;
169 }
170
171 result
172 FaceTrackerForm::OnTerminating(void)
173 {
174         return E_SUCCESS;
175 }
176
177 void
178 FaceTrackerForm::OnFormBackRequested(Osp::Ui::Controls::Form& source)
179 {
180         UiApp* pApp = UiApp::GetInstance();
181         AppAssert(pApp);
182         pApp->Terminate();
183 }
184
185 void
186 FaceTrackerForm::StartCamera(void)
187 {
188         BatteryLevel level = BATTERY_FULL;
189
190         Osp::System::Battery::GetCurrentLevel(level);
191
192         result r = CheckBatteryLevel(level);
193         if (r != E_SUCCESS)
194         {
195                 return ;
196         }
197    //Camera setting
198         __pCamera = new (std::nothrow) Camera();
199         r = __pCamera->Construct(*this, CAMERA_PRIMARY);
200
201
202         if (!__pCamera->IsPoweredOn())
203         {
204                 __pCamera->PowerOn();
205         }
206
207         if (__pCamera->IsPoweredOn())
208         {
209                 __pCamera->SetPreviewFormat(PIXEL_FORMAT_YCbCr420_PLANAR);
210                 __pCamera->SetPreviewResolution(Dimension(PREVIEW_WIDTH, PREVIEW_HEIGHT));
211                 __pCamera->StartPreview(&__bufferInfo, true);
212         }
213         else
214         {
215                 MessageBox msgBox;
216                 int modalResult = 0;
217                 //Display message and terminate app
218                 msgBox.Construct("Warning","Camera is busy. Terminating App.",MSGBOX_STYLE_OK);
219                 msgBox.ShowAndWait(modalResult);
220
221                 if(modalResult == MSGBOX_RESULT_OK)
222                 {
223                         Application::GetInstance()->Terminate();
224                 }
225
226                 return;
227         }
228
229         __pFaceThread->Start();
230         __pFaceData->SetThreadCompleted(true);
231
232         r = Osp::System::PowerManager::KeepScreenOnState(true, false);
233 }
234
235 void
236 FaceTrackerForm::StopCamera(void)
237 {
238         __pFaceThread->Stop();
239         __pFaceThread->Join();
240
241         __pCanvas->Clear();
242         __pCanvas->Show();
243
244         if (__pCamera->IsPoweredOn())
245         {
246                 __pCamera->StopPreview();
247                 __pCamera->PowerOff();
248         }
249         delete __pCamera;
250         __pCamera = null;
251 }
252
253 void
254 FaceTrackerForm::OnUserEventReceivedN(RequestId requestId, Osp::Base::Collection::IList *pArgs)
255 {
256         UiApp* pApp = UiApp::GetInstance();
257         if(pApp->GetAppUiState() != APP_UI_STATE_FOREGROUND)
258         {
259                 return;
260         }
261         __pCanvas->Clear();
262         Rectangle faceRect = __pFaceData->GetRectangle();
263         if(__pFaceData->GetFaceDetected())
264         {
265                 __pCanvas->DrawRectangle(faceRect);
266                 __pCanvas->DrawText(Point(faceRect.x, faceRect.y), Osp::Base::Integer::ToString(__pFaceData->GetSimilarity()),Osp::Graphics::Color(0xFF0000FF));
267         }
268         __pCanvas->Show();
269 }
270
271 result
272 FaceTrackerForm::CheckBatteryLevel(BatteryLevel level)
273 {
274         bool isCharging = false;
275         MessageBox msgBox;
276         int modalResult = 0;
277
278         Osp::System::RuntimeInfo::GetValue(String(L"IsCharging"),isCharging);
279
280         if (isCharging)
281         {
282                 return E_SUCCESS;
283         }
284
285         if ((level == BATTERY_EMPTY) || (level == BATTERY_CRITICAL) || (level == BATTERY_LOW))
286         {
287                 //Display message and terminate app
288                 msgBox.Construct("Warning","Battery Low.\nPlease charge to Use.",MSGBOX_STYLE_OK);
289                 msgBox.ShowAndWait(modalResult);
290
291                 if(modalResult == MSGBOX_RESULT_OK)
292                 {
293                         Application::GetInstance()->Terminate();
294                 }
295
296                 return E_FAILURE;
297         }
298
299         return E_SUCCESS;
300 }
301 //Camera EventListner
302 void
303 FaceTrackerForm::OnCameraPreviewed( Osp::Base::ByteBuffer& previewedData, result r)
304 {
305         if(!(__pFaceData->GetThreadCompleted()))
306         {
307                 return;
308         }
309         ByteBuffer* pBuffer = new (std::nothrow) ByteBuffer();
310         pBuffer->Construct(previewedData);
311
312         ArrayList *pList = new (std::nothrow) ArrayList;
313         pList->Construct();
314         pList->InsertAt (*pBuffer, 0);
315         __pFaceThread->SendUserEvent(RUN_MODE_FACE_DETECTION, pList);
316
317 }
318 void
319 FaceTrackerForm::OnCameraAutoFocused(bool completeCondition)
320 {
321 }
322 void
323 FaceTrackerForm::OnCameraCaptured( Osp::Base::ByteBuffer& capturedData, result r)
324 {
325 }
326 void
327 FaceTrackerForm::OnCameraErrorOccurred(Osp::Media::CameraErrorReason r )
328 {
329 }
330