Camera error handling added.
[apps/native/sample/QrCodeRecognizer.git] / project / src / CameraTools.cpp
1 //
2 // Tizen Native SDK
3 // Open Service Platform
4 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
5 // All rights reserved.
6 //
7 // This software contains confidential and proprietary information
8 // of Samsung Electronics Co., Ltd.
9 // The user of this software agrees not to disclose, disseminate or copy such
10 // Confidential Information and shall use the software only in accordance with
11 // the terms of the license agreement the user entered into with Samsung.
12 //
13
14 #include "CameraTools.h"
15 #include <FApp.h>
16
17 using namespace Tizen::Base;
18 using namespace Tizen::Media;
19 using namespace Tizen::Ui;
20 using namespace Tizen::Ui::Controls;
21
22 Camera*  CameraTools::__pCamera(null);
23 Tizen::Graphics::Dimension CameraTools::__choosenResolution;
24 int CameraTools::__width;
25 int CameraTools::__height;
26
27 const char* pTag = "CameraTools";
28
29 bool
30 CameraTools::StartCamera(ICameraEventListener &listener,
31                               const unsigned int width, const unsigned int height)
32 {
33
34     __choosenResolution = Tizen::Graphics::Dimension(width, height);
35
36     if (__pCamera)
37     {
38         return true;
39     }
40
41     __pCamera = new (std::nothrow) Camera();
42     if (null == __pCamera)
43     {
44         return false;
45     }
46
47     if (IsFailed(__pCamera->Construct(listener, CAMERA_PRIMARY)))
48     {
49         AppLogExceptionTag(pTag, "Construct failed with %s", GetErrorMessage(GetLastResult()));
50         delete __pCamera;
51         __pCamera = null;
52         return false;
53     }
54
55     if (IsFailed(__pCamera->PowerOn()))
56     {
57         AppLogExceptionTag(pTag, "PowerOn failed with %s", GetErrorMessage(GetLastResult()));
58         return false;
59     }
60
61     if (IsFailed(__pCamera->SetPreviewFormat(Tizen::Graphics::PIXEL_FORMAT_YCbCr420_PLANAR)))
62     {
63         AppLogExceptionTag(pTag, "SetPreviewFormat failed with %s", GetErrorMessage(GetLastResult()));
64         return false;
65     }
66
67     if (IsFailed(__pCamera->SetPreviewFrameRate(30)))
68     {
69         AppLogExceptionTag(pTag, "SetPreviewFrameRate failed with %s", GetErrorMessage(GetLastResult()));
70         return false;
71     }
72
73     Tizen::Base::Collection::IList* _pList = __pCamera->GetSupportedPreviewResolutionListN();
74
75     if (_pList)
76     {
77         int _count = _pList->GetCount();
78         if (_count)
79         {
80             Tizen::Base::Collection::IEnumerator* _pEnum = _pList->GetEnumeratorN();
81             if (_pEnum)
82             {
83                 result _res = E_SUCCESS;
84                 int _area; //area of camera preview image
85
86                 while (E_SUCCESS == _res)
87                 {
88                     _res = _pEnum->MoveNext();
89                     switch (_res)
90                     {
91                     case E_SUCCESS:
92                     {
93                         Tizen::Graphics::Dimension* _pSupportedDim = (Tizen::Graphics::Dimension*) _pEnum->GetCurrent();
94                         if (_pSupportedDim)
95                         {
96                             if (_pSupportedDim->width <= width && _pSupportedDim->height <= height)
97                             {
98                                 int _supportedArea = _pSupportedDim->width * _pSupportedDim->height;
99                                 if (_supportedArea > _area)
100                                 {
101                                     __choosenResolution = *_pSupportedDim;
102                                     _area = _supportedArea;
103                                 }
104                             }
105                         }
106                         break;
107                     }
108                     case E_OUT_OF_RANGE:
109                         break;
110                     case E_INVALID_OPERATION:
111                     default:
112                         break;
113                     }
114                 }
115
116                 delete _pEnum;
117             }
118
119         }
120         delete _pList;
121     }
122
123     if (IsFailed(__pCamera->SetPreviewResolution(__choosenResolution)))
124     {
125         AppLogExceptionTag(pTag, "SetPreviewResolution failed with %s", GetErrorMessage(GetLastResult()));
126         return false;
127     }
128
129         if (IsFailed(__pCamera->SetFocusMode(Tizen::Media::CAMERA_FOCUS_MODE_NORMAL)))
130         {
131                 AppLogExceptionTag(pTag, "SetFocusMode CAMERA_FOCUS_MODE_NORMAL failed with %s", GetErrorMessage(GetLastResult()));
132         }
133
134     __width = __pCamera->GetPreviewResolution().width;
135     __height = __pCamera->GetPreviewResolution().height;
136
137     return true;
138 }
139
140 void
141 CameraTools::StopCamera(void)
142 {
143     if (__pCamera && __pCamera->IsPoweredOn())
144     {
145         Suspend();
146         if (IsFailed(__pCamera->PowerOff()))
147         {
148                 AppLogExceptionTag(pTag, "PowerOff failed with %s", GetErrorMessage(GetLastResult()));
149         }
150         delete __pCamera;
151         __pCamera = null;
152     }
153     return;
154 }
155
156 void
157 CameraTools::Suspend(void)
158 {
159     if (__pCamera && __pCamera->IsPoweredOn())
160     {
161         if (CAMERA_STATE_PREVIEW == __pCamera->GetState() || CAMERA_STATE_AUTO_FOCUSING == __pCamera->GetState())
162         {
163                 if (IsFailed(__pCamera->StopPreview()))
164                         {
165                                 AppLogExceptionTag(pTag, "StopPreview failed with %s", GetErrorMessage(GetLastResult()));
166                         }
167         }
168         else
169         {
170                         AppLogExceptionTag(pTag, "StopPreview camera state %i", __pCamera->GetState());
171         }
172     }
173 }
174
175 void
176 CameraTools::Restart(void)
177 {
178         if (__pCamera && __pCamera->IsPoweredOn())
179         {
180                 AppLogExceptionTag(pTag, "Restart camera state %i", __pCamera->GetState());
181
182                 bool res = true;
183                 switch(__pCamera->GetState())
184                 {
185                 case CAMERA_STATE_INITIALIZED:
186                         //no break;
187                 case CAMERA_STATE_CAPTURED:
188                         if (IsFailed(__pCamera->StartPreview(null, true)))
189                         {
190                                 AppLogExceptionTag(pTag, "StartPreview failed with %s", GetErrorMessage(GetLastResult()));
191                                 res = false;
192                         }
193                         break;
194                 case CAMERA_STATE_PREVIEW:
195                         break;
196                 case CAMERA_STATE_AUTO_FOCUSING:
197                         break;
198                 case CAMERA_STATE_ERROR:
199                         AppLogExceptionTag(pTag, "Camera is in error state");
200                         res = false;
201                         break;
202                 default:
203                         AppLogExceptionTag(pTag, "Camera is in state %i", __pCamera->GetState());
204                         res = false;
205                         break;
206                 }
207
208                 if (!res)
209                 {
210                         res = true;
211                         MessageBox msgBox;
212                         int r = 0;
213
214                         msgBox.Construct("Warning", "Camera error occurred. Terminating application.", MSGBOX_STYLE_OK, 0);
215                         msgBox.ShowAndWait(r);
216
217                         switch (r)
218                         {
219                                 case MSGBOX_RESULT_OK:
220                                         Tizen::App::Application::GetInstance()->Terminate();
221                                         return;
222                                         break;
223
224                                 default:
225                                         break;
226                         }
227                 }
228         }
229 }
230
231 void
232 CameraTools::ZoomIn(void)
233 {
234     if (__pCamera && __pCamera->IsPoweredOn())
235     {
236         if (IsFailed(__pCamera->ZoomIn()))
237                 {
238                         AppLogExceptionTag(pTag, "ZoomIn failed with %s", GetErrorMessage(GetLastResult()));
239                 }
240     }
241 }
242
243 void
244 CameraTools::ZoomOut(void)
245 {
246     if (__pCamera && __pCamera->IsPoweredOn())
247     {
248         if (IsFailed(__pCamera->ZoomOut()))
249                 {
250                         AppLogExceptionTag(pTag, "ZoomIn failed with %s", GetErrorMessage(GetLastResult()));
251                 }
252     }
253 }
254
255 Camera*
256 CameraTools::GetCamera(void)
257 {
258     return __pCamera;
259 }
260
261 int
262 CameraTools::GetWidth(void)
263 {
264     return __width;
265 }
266
267 int
268 CameraTools::GetHeight(void)
269 {
270     return __height;
271 }