tham7777

Untitled

Jun 20th, 2020
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.40 KB | None | 0 0
  1. #include "android_cam_utils.hpp"
  2.  
  3. #include <media/NdkImageReader.h>
  4.  
  5. #include <QDebug>
  6.  
  7. #include <memory>
  8.  
  9. namespace ocv {
  10.  
  11. ACameraMetadata_const_entry
  12. get_camera_capabilities(ACameraManager *cam_manager, const char *camera_id, camera_status_t *status)
  13. {
  14.     camera_status_t ret = ACAMERA_OK;
  15.     auto chars = get_camera_characteristics(cam_manager, camera_id, &ret);
  16.     if(status){
  17.         *status = ret;
  18.     }
  19.     if(ret != ACAMERA_OK){
  20.         qInfo()<<__func__<<": "<<ret;
  21.         return {};
  22.     }
  23.  
  24.     return get_camera_capabilities(chars.get());
  25. }
  26.  
  27. ACameraMetadata_const_entry get_camera_capabilities(ACameraMetadata const *camera_chars, camera_status_t *status)
  28. {
  29.     ACameraMetadata_const_entry entry;
  30.     auto ret = ACameraMetadata_getConstEntry(camera_chars, ACAMERA_REQUEST_AVAILABLE_CAPABILITIES, &entry);
  31.     if(status){
  32.         *status = ret;
  33.     }
  34.     if(ret != ACAMERA_OK) {
  35.         qInfo()<<__func__<<": "<<ret;        
  36.         return {};
  37.     }
  38.     // Check the entry is actually legit
  39.     if(entry.tag != ACAMERA_REQUEST_AVAILABLE_CAPABILITIES ||
  40.             entry.count == 0 || entry.type != ACAMERA_TYPE_BYTE || entry.data.i32 == nullptr){
  41.         qInfo()<<QString("Bad available capabilities key: tag: %1 (expected %2), count %3 (expect > 0), "
  42.                          "type %4 (expected %5), data expected not null").
  43.                  arg(QString::number(entry.tag),
  44.                      QString::number(ACAMERA_REQUEST_AVAILABLE_CAPABILITIES),
  45.                      QString::number(entry.count),
  46.                      QString::number(entry.type),
  47.                      QString::number(ACAMERA_TYPE_BYTE));        
  48.         return {};
  49.     }
  50.  
  51.     return entry;
  52. }
  53.  
  54. std::tuple<std::string, bool> get_camera_depth_id(ACameraManager *cam_manager, int camera_facing)
  55. {
  56.     auto camera_ids = get_camera_id_list(cam_manager);
  57.     if(camera_ids){
  58.         qInfo()<<__func__<<": found camera count "<<camera_ids->numCameras;
  59.         for(int i = 0; i < camera_ids->numCameras; ++i){
  60.             const char *id = camera_ids->cameraIds[i];
  61.             camera_status_t ret = ACAMERA_OK;
  62.             auto chars = get_camera_characteristics(cam_manager, id, &ret);
  63.             if(ret != ACAMERA_OK){
  64.                 qInfo()<<__func__<<": cannot obtain characteristics of camera id = "<<id;
  65.                 continue;
  66.             }
  67.             auto const entry = get_camera_capabilities(chars.get(), &ret);
  68.             if(ret != ACAMERA_OK){
  69.                 qInfo()<<__func__<<": cannot obtain capabilities of camera id = "<<id;
  70.                 continue;
  71.             }
  72.  
  73.             ACameraMetadata_const_entry lens_info;
  74.             ACameraMetadata_getConstEntry(chars.get(), ACAMERA_LENS_FACING, &lens_info);
  75.             auto const facing = static_cast<acamera_metadata_enum_android_lens_facing_t>(lens_info.data.u8[i]);
  76.             bool is_right_face = facing == camera_facing;
  77.             bool support_bc = false, support_depth = false;
  78.             for(uint32_t i = 0; i < entry.count; i++) {
  79.                 if(entry.data.u8[i] == ACAMERA_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE){
  80.                     support_bc = true;
  81.                 }
  82.                 if(entry.data.u8[i] == ACAMERA_REQUEST_AVAILABLE_CAPABILITIES_DEPTH_OUTPUT){
  83.                     support_depth = true;
  84.                 }
  85.             }
  86.             qInfo()<<__func__<<" support bc = "<<support_bc<<", support depth = "<<support_depth
  87.                   <<", is right face = "<<is_right_face;
  88.             if(is_right_face && support_depth){
  89.                 qInfo()<<__func__<<": obtain depth camera id = "<<id;
  90.                 return {id, support_bc};
  91.             }
  92.         }
  93.     }else{
  94.         qInfo()<<__func__<<": cannot get depth cam";
  95.     }
  96.  
  97.     return {};
  98. }
  99.  
  100. std::string get_camera_id(ACameraManager *cam_manager, int camera_facing)
  101. {
  102.     auto camera_ids = get_camera_id_list(cam_manager);
  103.     if(!camera_ids){
  104.         return {};
  105.     }
  106.  
  107.     qInfo()<<__func__<<": found camera count "<<camera_ids->numCameras;
  108.  
  109.     std::string back_id;
  110.     for(int i = 0; i < camera_ids->numCameras; ++i){
  111.         const char *id = camera_ids->cameraIds[i];
  112.  
  113.         auto chars = get_camera_characteristics(cam_manager, id);
  114.         ACameraMetadata_const_entry lensInfo;
  115.         auto const ret = ACameraMetadata_getConstEntry(chars.get(), ACAMERA_LENS_FACING, &lensInfo);
  116.         if(ret != ACAMERA_OK){
  117.             qInfo()<<__func__<<": cannot get ACAMERA_LENS_FACING, error code = "<<ret;
  118.             continue;
  119.         }
  120.  
  121.         auto const facing = static_cast<acamera_metadata_enum_android_lens_facing_t>(lensInfo.data.u8[0]);
  122.         // Found a back-facing camera?
  123.         if (facing == camera_facing){
  124.             back_id = id;
  125.             break;
  126.         }
  127.     }
  128.  
  129.     return back_id;
  130. }
  131.  
  132. void print_camera_props(ACameraManager *cam_manager, const char *id)
  133. {
  134.     // exposure range
  135.     ACameraMetadata *metadataObj;
  136.     ACameraManager_getCameraCharacteristics(cam_manager, id, &metadataObj);
  137.  
  138.     ACameraMetadata_const_entry entry;
  139.     ACameraMetadata_getConstEntry(metadataObj,
  140.                                   ACAMERA_SENSOR_INFO_EXPOSURE_TIME_RANGE, &entry);
  141.  
  142.     int64_t minExposure = entry.data.i64[0];
  143.     int64_t maxExposure = entry.data.i64[1];
  144.     qInfo()<<__func__<<QString(" camProps: minExposure=%1 vs maxExposure=%2").arg(minExposure).arg(maxExposure);
  145.     ////////////////////////////////////////////////////////////////
  146.  
  147.     // sensitivity
  148.     ACameraMetadata_getConstEntry(metadataObj,
  149.                                   ACAMERA_SENSOR_INFO_SENSITIVITY_RANGE, &entry);
  150.  
  151.     int32_t minSensitivity = entry.data.i32[0];
  152.     int32_t maxSensitivity = entry.data.i32[1];
  153.  
  154.     qInfo()<<__func__<<QString("camProps: minSensitivity=%1 vs maxSensitivity=%2").
  155.              arg(minSensitivity).arg(maxSensitivity);
  156.     ////////////////////////////////////////////////////////////////
  157.  
  158.     // cam facing
  159.     ACameraMetadata_getConstEntry(metadataObj,
  160.                                   ACAMERA_SENSOR_ORIENTATION, &entry);
  161.  
  162.     int32_t orientation = entry.data.i32[0];
  163.     qInfo()<<__func__<<QString("camProps: %1").arg(orientation);
  164. }
  165.  
  166. std::unique_ptr<ACameraIdList, void(*)(ACameraIdList *)>
  167. get_camera_id_list(ACameraManager *cam_manager, camera_status_t *status)
  168. {
  169.     ACameraIdList *camera_ids = nullptr;
  170.     auto const ret = ACameraManager_getCameraIdList(cam_manager, &camera_ids);
  171.     if(status){
  172.         *status = ret;
  173.     }
  174.     if(ret != ACAMERA_OK){
  175.         qInfo()<<__func__<<": cannot get ACameraIdList, error code = "<<ret;
  176.     }
  177.  
  178.     return {camera_ids, ACameraManager_deleteCameraIdList};
  179. }
  180.  
  181. std::unique_ptr<ACameraMetadata, void (*)(ACameraMetadata *)>
  182. get_camera_characteristics(ACameraManager *cam_manager, const char *camera_id, camera_status_t *status)
  183. {
  184.     ACameraMetadata *meta = nullptr;
  185.     auto const ret = ACameraManager_getCameraCharacteristics(cam_manager, camera_id, &meta);
  186.     if(status){
  187.         *status = ret;
  188.     }
  189.     if(ret != ACAMERA_OK) {
  190.         qInfo()<<__func__<<": cannot get camera characteristics, error code = "<<ret;
  191.     }
  192.  
  193.     return {meta, ACameraMetadata_free};
  194. }
  195.  
  196. std::unique_ptr<AImage, void (*)(AImage *)> get_next_image(AImageReader *reader, int *status)
  197. {
  198.     AImage *image = nullptr;
  199.     auto const ret = AImageReader_acquireNextImage(reader, &image);
  200.     if(status){
  201.         *status = ret;
  202.     }
  203.  
  204.     return {image, AImage_delete};
  205. }
  206.  
  207. }
Add Comment
Please, Sign In to add comment