orb_camera_ui.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "orb_camera_ui_impl.h"
  2. #include "core/image_utility.hpp"
  3. #include "core/imgui_utility.hpp"
  4. #include <boost/asio/io_context.hpp>
  5. #include <boost/asio/post.hpp>
  6. using boost::asio::io_context;
  7. using boost::asio::post;
  8. extern io_context *main_ctx;
  9. orb_camera_ui::impl::impl(create_config conf) {
  10. cam_c_conf.stream = conf.stream;
  11. cam_c_conf.ctx = main_ctx;
  12. cam_s_conf.color.name = conf.cf_name;
  13. cam_s_conf.depth.name = conf.df_name;
  14. refresh_dev_info_list();
  15. }
  16. void orb_camera_ui::impl::refresh_dev_info_list() {
  17. dev_info_list = orb_camera::query_device_info();
  18. }
  19. void orb_camera_ui::impl::open_camera() {
  20. cam_c_conf.sn_str = dev_info_list[dev_index].sn_str.c_str();
  21. cam = std::unique_ptr<orb_camera>(orb_camera::create(cam_c_conf));
  22. assert(cam != nullptr);
  23. c_conf_list.clear();
  24. auto cv_info = cam->query_video_info(orb_camera::V_COLOR);
  25. for (auto v_info: *cv_info) {
  26. if (strcmp(v_info.fmt_name, "rgb") == 0 ||
  27. strcmp(v_info.fmt_name, "mjpg") == 0) {
  28. c_conf_list.emplace_back(v_info.index, std::string(v_info));
  29. }
  30. }
  31. d_conf_list.clear();
  32. auto dv_info = cam->query_video_info(orb_camera::V_DEPTH);
  33. for (auto v_info: *dv_info) {
  34. if (strcmp(v_info.fmt_name, "y16") == 0) {
  35. d_conf_list.emplace_back(v_info.index, std::string(v_info));
  36. }
  37. }
  38. }
  39. void orb_camera_ui::impl::start_camera() {
  40. assert(cam != nullptr);
  41. cam_s_conf.color.config_index = c_conf_list[c_conf_index].index;
  42. cam_s_conf.depth.config_index = d_conf_list[d_conf_index].index;
  43. cam->start(cam_s_conf);
  44. }
  45. void orb_camera_ui::impl::show_config() {
  46. // select device by S/N
  47. if (!dev_info_list.empty()) {
  48. auto guard = imgui_disable_guard(cam != nullptr);
  49. auto dev_sn_preview = dev_info_list[dev_index].sn_str.c_str();
  50. if (ImGui::BeginCombo("Device SN", dev_sn_preview)) {
  51. for (int k = 0; k < dev_info_list.size(); ++k) {
  52. auto is_selected = (dev_index == k);
  53. if (ImGui::Selectable(dev_info_list[k].sn_str.c_str(), is_selected)) {
  54. dev_index = k;
  55. }
  56. if (is_selected) {
  57. ImGui::SetItemDefaultFocus();
  58. }
  59. }
  60. ImGui::EndCombo();
  61. }
  62. } else { // No device found.
  63. auto guard = imgui_disable_guard();
  64. if (ImGui::BeginCombo("Device SN", "No device")) {
  65. ImGui::EndCombo();
  66. }
  67. }
  68. ImGui::SameLine();
  69. if (ImGui::Button("R")) {
  70. post(*main_ctx, [this] { refresh_dev_info_list(); });
  71. }
  72. // select video config
  73. if (cam != nullptr) {
  74. auto guard = imgui_disable_guard(cam->is_capturing());
  75. auto c_conf_preview = c_conf_list[c_conf_index].dis_name.c_str();
  76. if (ImGui::BeginCombo("Color", c_conf_preview)) {
  77. for (int k = 0; k < c_conf_list.size(); ++k) {
  78. auto is_selected = (c_conf_index == k);
  79. if (ImGui::Selectable(c_conf_list[k].dis_name.c_str(), is_selected)) {
  80. c_conf_index = k;
  81. }
  82. if (is_selected) {
  83. ImGui::SetItemDefaultFocus();
  84. }
  85. }
  86. ImGui::EndCombo();
  87. }
  88. auto d_conf_preview = d_conf_list[d_conf_index].dis_name.c_str();
  89. if (ImGui::BeginCombo("Depth", d_conf_preview)) {
  90. for (int k = 0; k < d_conf_list.size(); ++k) {
  91. auto is_selected = (d_conf_index == k);
  92. if (ImGui::Selectable(d_conf_list[k].dis_name.c_str(), is_selected)) {
  93. d_conf_index = k;
  94. }
  95. if (is_selected) {
  96. ImGui::SetItemDefaultFocus();
  97. }
  98. }
  99. ImGui::EndCombo();
  100. }
  101. }
  102. }
  103. void orb_camera_ui::impl::show() {
  104. ImGui::SeparatorText("Actions");
  105. if (cam == nullptr) {
  106. auto guard = imgui_disable_guard(dev_info_list.empty());
  107. if (ImGui::Button("Open")) {
  108. post(*main_ctx, [this] { open_camera(); });
  109. }
  110. } else {
  111. assert(cam != nullptr);
  112. if (ImGui::Button("Close")) {
  113. post(*main_ctx, [this] { cam = nullptr; });
  114. }
  115. ImGui::SameLine();
  116. if (!cam->is_capturing()) {
  117. if (ImGui::Button("Start")) {
  118. post(*main_ctx, [this] { start_camera(); });
  119. }
  120. } else {
  121. if (ImGui::Button("Stop")) {
  122. post(*main_ctx, [this] { cam->stop(); });
  123. }
  124. }
  125. }
  126. ImGui::SeparatorText("Configs");
  127. show_config();
  128. ImGui::SeparatorText("Info");
  129. auto c_img = OBJ_QUERY(image_u8c3, cam_s_conf.color.name);
  130. if (c_img != nullptr) {
  131. auto size = c_img->size();
  132. ImGui::Text("Color Stream: %dx%d", size.width, size.height);
  133. }
  134. auto d_img = OBJ_QUERY(image_f32c1, cam_s_conf.depth.name);
  135. if (d_img != nullptr) {
  136. auto size = d_img->size();
  137. ImGui::Text("Depth Stream: %dx%d", size.width, size.height);
  138. }
  139. }
  140. orb_camera_ui::orb_camera_ui(create_config conf)
  141. : pimpl(std::make_unique<impl>(conf)) {}
  142. orb_camera_ui::~orb_camera_ui() = default;
  143. void orb_camera_ui::show() {
  144. pimpl->show();
  145. }