1 /*
2 * Copyright (C) 2021-2023 Alibaba Group Holding Limited
3 */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include "ulog/ulog.h"
8 #include "aos/kernel.h"
9 #include "ucamera_service.h"
10 #include "wifi_camera.h"
11
12 #define TAG "UCAMERA_SERVICE"
13
14 /*
15 * Initialize the ucamera service
16 */
ucamera_service_init(const char * dev_name)17 int32_t ucamera_service_init(const char *dev_name)
18 {
19 int32_t ret;
20
21 ucamera_device_t *dev = ucamera_device_init(dev_name);
22 if (!dev) {
23 LOGE(TAG, "ucamera service init fail\n");
24 return -1;
25 }
26
27 ret = dev->camera_connect(dev);
28 if (ret < 0)
29 LOGE(TAG, "ucamera device connect fail\n");
30 return ret;
31 }
32
33 /*
34 * Uninitialize the ucamera service
35 */
ucamera_service_uninit(void)36 int32_t ucamera_service_uninit(void)
37 {
38 int32_t ret;
39
40 ucamera_device_t *dev = ucamera_get_device();
41 if (!dev) {
42 LOGE(TAG, "ucamera get device name fail\n");
43 return -1;
44 }
45 ret = dev->camera_disconnect(dev);
46 if (ret < 0)
47 LOGE(TAG, "ucamera device disconnect fail\n");
48
49 ucamera_device_uninit();
50
51 return ret;
52 }
53
54 /*
55 * Get frame of camera device
56 */
ucamera_service_get_frame(void)57 frame_buffer_t *ucamera_service_get_frame(void)
58 {
59 frame_buffer_t *frame = NULL;
60 ucamera_device_t *dev = ucamera_get_device();
61 if (!dev) {
62 LOGE(TAG, "ucamera get device name fail\n");
63 return NULL;
64 }
65
66 frame = dev->camera_get_frame(dev);
67 return frame;
68 }
69
70 /*
71 * Save frame of camera
72 */
ucamera_service_save_frame(frame_buffer_t * frame,const char * path)73 int ucamera_service_save_frame(frame_buffer_t *frame, const char *path)
74 {
75 FILE *jpeg_file = NULL;
76
77 jpeg_file = fopen(path, "wb");
78 if (jpeg_file == NULL) {
79 LOGE(TAG, "opening output file fail\n");
80 return -1;
81 }
82 if (fwrite(frame->buf, frame->len, 1, jpeg_file) < 1) {
83 LOGE(TAG, "write buf fail\n");
84 fclose(jpeg_file);
85 return -1;
86 }
87
88 fclose(jpeg_file);
89 return 0;
90 }
91