Skip to content

Commit f6c21ff

Browse files
committed
clean up example camera and use logging instead of cout
1 parent b8dcf7c commit f6c21ff

File tree

1 file changed

+69
-86
lines changed

1 file changed

+69
-86
lines changed

src/viam/examples/camera/example_camera.cpp

Lines changed: 69 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -5,97 +5,80 @@
55

66
#include <viam/sdk/common/instance.hpp>
77
#include <viam/sdk/components/camera.hpp>
8+
#include <viam/sdk/log/logging.hpp>
89
#include <viam/sdk/robot/client.hpp>
910
#include <viam/sdk/rpc/dial.hpp>
1011

11-
int main() {
12-
using std::cerr;
13-
using std::cout;
14-
using std::endl;
12+
int main() try {
1513
namespace vs = ::viam::sdk;
16-
try {
17-
// Every Viam C++ SDK program must have one and only one Instance object which is created
18-
// before
19-
// any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed.
20-
vs::Instance inst;
21-
22-
// If you want to connect to a remote robot, this should be the url of the robot
23-
// Ex: xxx.xxx.viam.cloud
24-
std::string robot_address("localhost:8080");
25-
// If you want to connect to a remote robot, you need some authentication secret
26-
// You can find this on app.viam.com
27-
vs::Credentials credentials("", "");
28-
29-
vs::DialOptions dial_options;
30-
31-
// If you have credentials, use this to pass them to the robot
32-
// dial_options.credentials = credentials;
33-
34-
// This is for an example. Care should be taken before exercising this option in production.
35-
dial_options.set_allow_insecure_downgrade(
36-
(credentials.type().empty() && credentials.payload().empty()));
37-
38-
// Set the refresh interval of the robot (in seconds) (0 = auto refresh) and the dial
39-
// options
40-
vs::Options options = vs::Options(1, dial_options);
41-
42-
std::shared_ptr<vs::RobotClient> robot;
43-
try {
44-
robot = vs::RobotClient::at_address(robot_address, options);
45-
cout << "Successfully connected to the robot" << endl;
46-
} catch (const std::exception& e) {
47-
cerr << "Failed to connect to the robot. Exiting." << endl;
48-
throw;
49-
}
50-
51-
std::vector<vs::Name> resource_names = robot->resource_names();
52-
53-
cout << "Resources of the robot:" << endl;
54-
for (const auto& resource : resource_names) {
55-
cout << " - " << resource.name() << " (" << resource.api().resource_subtype() << ")"
56-
<< endl;
57-
}
58-
59-
std::string camera_name("camera1");
60-
61-
cout << "Getting camera: " << camera_name << endl;
62-
std::shared_ptr<vs::Camera> camera;
63-
try {
64-
camera = robot->resource_by_name<vs::Camera>(camera_name);
65-
} catch (const std::exception& e) {
66-
cerr << "Failed to find " << camera_name << ". Exiting." << endl;
67-
throw;
68-
}
69-
vs::Camera::properties props = camera->get_properties();
70-
vs::Camera::intrinsic_parameters intrinsics = props.intrinsic_parameters;
71-
cout << "Image dimensions: " << intrinsics.width_px << " x " << intrinsics.height_px
72-
<< endl;
73-
74-
std::string output_file("img.png");
75-
std::string image_mime_type("image/png");
76-
77-
cout << "Getting image from camera " << endl;
78-
vs::Camera::raw_image img = camera->get_image(image_mime_type);
79-
cout << "Got image of mime type: " << img.mime_type << endl;
80-
81-
cout << "Getting and saving image to " << output_file << endl;
82-
std::ofstream fout;
83-
fout.open(output_file, std::ios::binary | std::ios::out);
84-
if (fout.fail()) {
85-
throw std::runtime_error("Failed to open output file " + output_file);
86-
}
87-
fout.write(reinterpret_cast<char*>(img.bytes.data()), img.bytes.size());
88-
fout.close();
89-
if (fout.fail()) {
90-
throw std::runtime_error("Failed to write and close output file " + output_file);
91-
}
92-
} catch (const std::exception& ex) {
93-
cerr << "Program failed. Exception: " << std::string(ex.what()) << endl;
94-
return EXIT_FAILURE;
95-
} catch (...) {
96-
cerr << "Program failed without exception message." << endl;
97-
return EXIT_FAILURE;
14+
// Every Viam C++ SDK program must have one and only one Instance object which is created
15+
// before
16+
// any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed.
17+
vs::Instance inst;
18+
19+
// If you want to connect to a remote robot, this should be the url of the robot
20+
// Ex: xxx.xxx.viam.cloud
21+
std::string robot_address("localhost:8080");
22+
// If you want to connect to a remote robot, you need some authentication secret
23+
// You can find this on app.viam.com
24+
vs::Credentials credentials("", "");
25+
26+
vs::DialOptions dial_options;
27+
28+
// If you have credentials, use this to pass them to the robot
29+
// dial_options.credentials = credentials;
30+
31+
// This is for an example. Care should be taken before exercising this option in production.
32+
dial_options.set_allow_insecure_downgrade(
33+
(credentials.type().empty() && credentials.payload().empty()));
34+
35+
// Set the refresh interval of the robot (in seconds) (0 = auto refresh) and the dial
36+
// options
37+
vs::Options options = vs::Options(1, dial_options);
38+
39+
std::shared_ptr<vs::RobotClient> robot = vs::RobotClient::at_address(robot_address, options);
40+
VIAM_SDK_LOG(info) << "Successfully connected to the robot";
41+
42+
std::vector<vs::Name> resource_names = robot->resource_names();
43+
44+
VIAM_SDK_LOG(info) << "Resources of the robot:";
45+
for (const auto& resource : resource_names) {
46+
VIAM_SDK_LOG(info) << " - " << resource.name() << " (" << resource.api().resource_subtype()
47+
<< ")";
9848
}
9949

50+
std::string camera_name("camera1");
51+
52+
VIAM_SDK_LOG(info) << "Getting camera: " << camera_name;
53+
std::shared_ptr<vs::Camera> camera = robot->resource_by_name<vs::Camera>(camera_name);
54+
55+
vs::Camera::properties props = camera->get_properties();
56+
vs::Camera::intrinsic_parameters intrinsics = props.intrinsic_parameters;
57+
VIAM_SDK_LOG(info) << "Image dimensions: " << intrinsics.width_px << " x "
58+
<< intrinsics.height_px;
59+
60+
std::string output_file("img.png");
61+
std::string image_mime_type("image/png");
62+
63+
VIAM_SDK_LOG(info) << "Getting image from camera ";
64+
65+
vs::Camera::raw_image img = camera->get_image(image_mime_type);
66+
67+
VIAM_SDK_LOG(info) << "Got image of mime type: " << img.mime_type;
68+
69+
VIAM_SDK_LOG(info) << "Getting and saving image to " << output_file;
70+
71+
std::ofstream fout;
72+
73+
// Operations on the ofstream will throw on failure.
74+
fout.exceptions(std::ofstream::failbit);
75+
fout.open(output_file, std::ios::binary | std::ios::out);
76+
77+
fout.write(reinterpret_cast<char*>(img.bytes.data()), img.bytes.size());
78+
fout.close();
79+
10080
return EXIT_SUCCESS;
81+
} catch (const std::exception& ex) {
82+
std::cerr << "Program failed. Exception: " << std::string(ex.what()) << "\n";
83+
return EXIT_FAILURE;
10184
}

0 commit comments

Comments
 (0)