2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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>
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
13 * http://floralicense.org/license/
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.
29 #include "resource/resource_camera.h"
31 #define RESOLUTION_W 320
32 #define RESOLUTION_H 240
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);
40 struct __camera_data {
45 unsigned int image_size;
46 capture_completed_cb completed_cb;
47 void *completed_cb_data;
50 static struct __camera_data *camera_data = NULL;
52 int resource_capture_camera(capture_completed_cb capture_completed, void *user_data)
55 int ret = CAMERA_ERROR_NONE;
57 if (camera_data == NULL) {
58 _I("Camera is not initialized");
61 _E("Failed to initialize camera");
66 ret = camera_get_state(camera_data->cam_handle, &state);
67 if (ret != CAMERA_ERROR_NONE) {
68 _E("Failed to get camera state");
72 if (state >= CAMERA_STATE_CAPTURING) {
73 _D("Camera is now capturing");
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");
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");
92 camera_data->completed_cb = capture_completed;
93 camera_data->completed_cb_data = user_data;
98 void resource_close_camera(void)
100 if (camera_data == NULL)
103 camera_stop_preview(camera_data->cam_handle);
105 camera_destroy(camera_data->cam_handle);
106 camera_data->cam_handle = NULL;
112 static void __capturing_cb(camera_image_data_s *image, camera_image_data_s *postview,
113 camera_image_data_s *thumbnail, void *user_data)
115 struct __camera_data *camera_data = user_data;
121 camera_data->captured_file = malloc(image->size);
122 if (camera_data->captured_file == NULL)
125 _D("Now is on Capturing: Image size[%d x %d]", image->width, image->height);
127 memcpy(camera_data->captured_file, image->data, image->size);
128 camera_data->image_size = image->size;
133 static void __completed_cb(void *user_data)
135 struct __camera_data *camera_data = user_data;
136 int ret = CAMERA_ERROR_NONE;
138 if (camera_data->completed_cb)
139 camera_data->completed_cb(camera_data->captured_file, camera_data->image_size, camera_data->completed_cb_data);
141 free(camera_data->captured_file);
142 camera_data->captured_file = NULL;
144 if (!camera_data->cam_handle) {
145 _E("Camera is NULL");
148 _D("Capture is completed");
150 ret = camera_start_preview(camera_data->cam_handle);
151 if (ret != CAMERA_ERROR_NONE) {
152 _E("Failed to start preview");
156 ret = camera_stop_preview(camera_data->cam_handle);
157 if (ret != CAMERA_ERROR_NONE) {
158 _E("Failed to stop preview");
165 static bool __resolution_list_cb(int width, int height, void *user_data)
167 _D("Supported resolution - Width[%d], Height[%d]", width, height);
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;
174 _D("Fixed Resolution is Width[%d], Height[%d]", camera_data->resolution_w, camera_data->resolution_h);
179 static int __init(void)
181 int ret = CAMERA_ERROR_NONE;
183 camera_data = malloc(sizeof(struct __camera_data));
184 if (camera_data == NULL) {
185 _E("Failed to allocate Camera data");
188 memset(camera_data, 0, sizeof(struct __camera_data));
190 ret = camera_create(CAMERA_DEVICE_CAMERA0, &(camera_data->cam_handle));
191 if (ret != CAMERA_ERROR_NONE) {
192 _E("Failed to create camera");
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");
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");
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");
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");
220 ret = camera_start_preview(camera_data->cam_handle);
221 if (ret != CAMERA_ERROR_NONE) {
222 _E("Failed to start preview[%d]", ret);
229 camera_destroy(camera_data->cam_handle);