-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcamera.cpp
More file actions
178 lines (144 loc) · 5.88 KB
/
camera.cpp
File metadata and controls
178 lines (144 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Copyright 2020 Konstantinos Tzevanidis
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <devices/camera.hpp>
#include <utilities/log.hpp>
#include <stdexcept>
#include <sstream>
#include <string>
using namespace ::std;
using namespace ::utilities;
namespace devices
{
camera::camera(ANativeWindow* a_window)
: m_manager{nullptr, ACameraManager_delete},
m_ids{nullptr, ACameraManager_deleteCameraIdList},
m_device{nullptr, ACameraDevice_close},
m_outputs{nullptr, ACaptureSessionOutputContainer_free},
m_img_reader_output{nullptr, ACaptureSessionOutput_free},
m_session{nullptr, ACameraCaptureSession_close},
m_capture_req{nullptr, ACaptureRequest_free},
m_target{nullptr, ACameraOutputTarget_free}
{
if(!a_window)
throw runtime_error("Invalid window handle.");
m_manager.reset(ACameraManager_create());
if(!m_manager.get())
throw runtime_error("Cannot create camera manager.");
auto pt = m_ids.release();
auto result = ACameraManager_getCameraIdList(m_manager.get(), &pt);
m_ids.reset(pt);
if(result != ACAMERA_OK)
{
stringstream sstr;
sstr << "Failed to acquire camera list. (code: " << result << ").";
throw runtime_error(sstr.str().c_str());
}
if(m_ids->numCameras < 1)
throw runtime_error("No cameras found.");
string selected_camera;
for(uint32_t i = 0; i < m_ids->numCameras; ++i)
{
auto cam_id = m_ids->cameraIds[i];
ACameraMetadata* metadata = nullptr;
result = ACameraManager_getCameraCharacteristics(m_manager.get(), cam_id, &metadata);
if(result != ACAMERA_OK || !metadata)
continue;
ACameraMetadata_const_entry entry;
if(ACameraMetadata_getConstEntry(metadata, ACAMERA_LENS_FACING, &entry) == ACAMERA_OK)
{
auto facing = static_cast<acamera_metadata_enum_android_lens_facing_t>(entry.data.u8[0]);
// Select which camera to use (front or back)
if(facing == ACAMERA_LENS_FACING_BACK)
selected_camera = string(cam_id);
}
ACameraMetadata_free(metadata);
}
if(selected_camera.size() < 1)
throw runtime_error("Cannot find appropriate device.");
{
auto pt = m_device.release();
result = ACameraManager_openCamera(m_manager.get(), selected_camera.c_str(),
&m_dev_state_cbs, &pt);
m_device.reset(pt);
}
if(result != ACAMERA_OK || !m_device.get())
{
stringstream sstr;
sstr << "Couldn't open camera. (code: " << result << ").";
throw runtime_error(sstr.str());
}
{
auto pt = m_outputs.release();
result = ACaptureSessionOutputContainer_create(&pt);
m_outputs.reset(pt);
}
if(result != ACAMERA_OK)
throw runtime_error("Capture session output container creation failed.");
{
auto pt = m_img_reader_output.release();
result = ACaptureSessionOutput_create(a_window, &pt);
m_img_reader_output.reset(pt);
}
if(result != ACAMERA_OK)
throw runtime_error("Capture session image reader output creation failed.");
result = ACaptureSessionOutputContainer_add(m_outputs.get(), m_img_reader_output.get());
if(result != ACAMERA_OK)
throw runtime_error("Couldn't add image reader output session to container.");
{
auto pt = m_session.release();
result = ACameraDevice_createCaptureSession(m_device.get(), m_outputs.get(),
&m_cap_state_cbs, &pt);
m_session.reset(pt);
}
if(result != ACAMERA_OK)
throw runtime_error("Couldn't create capture session.");
{
auto pt = m_capture_req.release();
result = ACameraDevice_createCaptureRequest(m_device.get(), TEMPLATE_PREVIEW, &pt);
m_capture_req.reset(pt);
}
if(result != ACAMERA_OK)
throw runtime_error("Couldn't create capture request.");
{
auto pt = m_target.release();
result = ACameraOutputTarget_create(a_window, &pt);
m_target.reset(pt);
}
if(result != ACAMERA_OK)
throw runtime_error("Couldn't create camera output target.");
result = ACaptureRequest_addTarget(m_capture_req.get(), m_target.get());
if(result != ACAMERA_OK)
throw runtime_error("Couldn't add capture request to camera output target.");
if constexpr(__ncv_logging_enabled)
_log_android(log_level::info) << "Camera logical device created.";
}
camera::~camera()
{
if constexpr(__ncv_logging_enabled)
_log_android(log_level::info) << "Destroying camera device...";
}
void camera::start_capturing()
{
auto pt = m_capture_req.release();
ACameraCaptureSession_setRepeatingRequest(m_session.get(), nullptr, 1, &pt, nullptr);
m_capture_req.reset(pt);
}
void camera::stop_capturing()
{
ACameraCaptureSession_stopRepeating(m_session.get());
}
}