Add Camera Module
[apps/native/position-finder-server.git] / src / resource / resource_camera.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Contact: Jin Yoon <jinny.yoon@samsung.com>
5  *          Geunsun Lee <gs86.lee@samsung.com>
6  *          Eunyoung Lee <ey928.lee@samsung.com>
7  *          Junkyu Han <junkyu.han@samsung.com>
8  *
9  * Licensed under the Flora License, Version 1.1 (the License);
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://floralicense.org/license/
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an AS IS BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 #include <camera.h>
23 #include <glib.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <tizen.h>
27
28 #include "log.h"
29 #include "resource/resource_camera.h"
30
31 #define RESOLUTION_W 320
32 #define RESOLUTION_H 240
33
34 static int __init(void);
35 static void __completed_cb(void *user_data);
36 static bool __resolution_list_cb(int width, int height, void *user_data);
37 static void __capturing_cb(camera_image_data_s *image, camera_image_data_s *postview,
38                 camera_image_data_s *thumbnail, void *user_data);
39
40 struct __camera_data {
41         camera_h cam_handle;
42         int resolution_w;
43         int resolution_h;
44         void *captured_file;
45         unsigned int image_size;
46         capture_completed_cb completed_cb;
47         void *completed_cb_data;
48 };
49
50 static struct __camera_data *camera_data = NULL;
51
52 int resource_capture_camera(capture_completed_cb capture_completed, void *user_data)
53 {
54         camera_state_e state;
55         int ret = CAMERA_ERROR_NONE;
56
57         if (camera_data == NULL) {
58                 _I("Camera is not initialized");
59                 ret = __init();
60                 if (ret < 0) {
61                         _E("Failed to initialize camera");
62                         return -1;
63                 }
64         }
65
66         ret = camera_get_state(camera_data->cam_handle, &state);
67         if (ret != CAMERA_ERROR_NONE) {
68                 _E("Failed to get camera state");
69                 return -1;
70         }
71
72         if (state >= CAMERA_STATE_CAPTURING) {
73                 _D("Camera is now capturing");
74                 return 0;
75         }
76
77         if (state != CAMERA_STATE_PREVIEW) {
78                 _I("Preview is not started");
79                 ret = camera_start_preview(camera_data->cam_handle);
80                 if (ret != CAMERA_ERROR_NONE) {
81                         _E("Failed to start preview");
82                         return -1;
83                 }
84         }
85
86         ret = camera_start_capture(camera_data->cam_handle, __capturing_cb, __completed_cb, camera_data);
87         if (ret != CAMERA_ERROR_NONE) {
88                 _E("Failed to start capturing");
89                 return -1;
90         }
91
92         camera_data->completed_cb = capture_completed;
93         camera_data->completed_cb_data = user_data;
94
95         return 0;
96 }
97
98 void resource_close_camera(void)
99 {
100         if (camera_data == NULL)
101                 return;
102
103         camera_stop_preview(camera_data->cam_handle);
104
105         camera_destroy(camera_data->cam_handle);
106         camera_data->cam_handle = NULL;
107
108         free(camera_data);
109         camera_data = NULL;
110 }
111
112 static void __capturing_cb(camera_image_data_s *image, camera_image_data_s *postview,
113                 camera_image_data_s *thumbnail, void *user_data)
114 {
115         struct __camera_data *camera_data = user_data;
116         if (image == NULL) {
117                 _E("Image is NULL");
118                 return;
119         }
120
121         camera_data->captured_file = malloc(image->size);
122         if (camera_data->captured_file == NULL)
123                 return;
124
125         _D("Now is on Capturing: Image size[%d x %d]", image->width, image->height);
126
127         memcpy(camera_data->captured_file, image->data, image->size);
128         camera_data->image_size = image->size;
129
130         return;
131 }
132
133 static void __completed_cb(void *user_data)
134 {
135         struct __camera_data *camera_data = user_data;
136         int ret = CAMERA_ERROR_NONE;
137
138         if (camera_data->completed_cb)
139                 camera_data->completed_cb(camera_data->captured_file, camera_data->image_size, camera_data->completed_cb_data);
140
141         free(camera_data->captured_file);
142         camera_data->captured_file = NULL;
143
144         if (!camera_data->cam_handle) {
145                 _E("Camera is NULL");
146                 return;
147         }
148         _D("Capture is completed");
149
150         ret = camera_start_preview(camera_data->cam_handle);
151         if (ret != CAMERA_ERROR_NONE) {
152                 _E("Failed to start preview");
153                 return;
154         }
155
156         ret = camera_stop_preview(camera_data->cam_handle);
157         if (ret != CAMERA_ERROR_NONE) {
158                 _E("Failed to stop preview");
159                 return;
160         }
161
162         return;
163 }
164
165 static bool __resolution_list_cb(int width, int height, void *user_data)
166 {
167         _D("Supported resolution - Width[%d], Height[%d]", width, height);
168
169         if (width > camera_data->resolution_w && width <= RESOLUTION_W &&
170                         height > camera_data->resolution_h && height <= RESOLUTION_H) {
171                 camera_data->resolution_w = width;
172                 camera_data->resolution_h = height;
173         }
174         _D("Fixed Resolution is Width[%d], Height[%d]", camera_data->resolution_w, camera_data->resolution_h);
175
176         return true;
177 }
178
179 static int __init(void)
180 {
181         int ret = CAMERA_ERROR_NONE;
182
183         camera_data = malloc(sizeof(struct __camera_data));
184         if (camera_data == NULL) {
185                 _E("Failed to allocate Camera data");
186                 return -1;
187         }
188         memset(camera_data, 0, sizeof(struct __camera_data));
189
190         ret = camera_create(CAMERA_DEVICE_CAMERA0, &(camera_data->cam_handle));
191         if (ret != CAMERA_ERROR_NONE) {
192                 _E("Failed to create camera");
193                 goto ERROR;
194         }
195
196         ret = camera_foreach_supported_capture_resolution(camera_data->cam_handle, __resolution_list_cb, NULL);
197         if (ret != CAMERA_ERROR_NONE) {
198                 _E("Failed to foreach supported capture resolution");
199                 goto ERROR;
200         }
201
202         ret = camera_set_preview_resolution(camera_data->cam_handle, camera_data->resolution_w, camera_data->resolution_h);
203         if (ret != CAMERA_ERROR_NONE) {
204                 _E("Failed to set preview resolution");
205                 goto ERROR;
206         }
207
208         ret = camera_set_capture_resolution(camera_data->cam_handle, camera_data->resolution_w, camera_data->resolution_h);
209         if (ret != CAMERA_ERROR_NONE) {
210                 _E("Failed to set capture resolution");
211                 goto ERROR;
212         }
213
214         ret = camera_set_capture_format(camera_data->cam_handle, CAMERA_PIXEL_FORMAT_JPEG);
215         if (ret != CAMERA_ERROR_NONE) {
216                 _E("Failed to set capture resolution");
217                 goto ERROR;
218         }
219
220         ret = camera_start_preview(camera_data->cam_handle);
221         if (ret != CAMERA_ERROR_NONE) {
222                 _E("Failed to start preview[%d]", ret);
223                 goto ERROR;
224         }
225
226         return 0;
227
228 ERROR:
229         camera_destroy(camera_data->cam_handle);
230         free(camera_data);
231         return -1;
232 }
233