Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "android_cam_utils.hpp"
- #include <media/NdkImageReader.h>
- #include <QDebug>
- #include <memory>
- namespace ocv {
- ACameraMetadata_const_entry
- get_camera_capabilities(ACameraManager *cam_manager, const char *camera_id, camera_status_t *status)
- {
- camera_status_t ret = ACAMERA_OK;
- auto chars = get_camera_characteristics(cam_manager, camera_id, &ret);
- if(status){
- *status = ret;
- }
- if(ret != ACAMERA_OK){
- qInfo()<<__func__<<": "<<ret;
- return {};
- }
- return get_camera_capabilities(chars.get());
- }
- ACameraMetadata_const_entry get_camera_capabilities(ACameraMetadata const *camera_chars, camera_status_t *status)
- {
- ACameraMetadata_const_entry entry;
- auto ret = ACameraMetadata_getConstEntry(camera_chars, ACAMERA_REQUEST_AVAILABLE_CAPABILITIES, &entry);
- if(status){
- *status = ret;
- }
- if(ret != ACAMERA_OK) {
- qInfo()<<__func__<<": "<<ret;
- return {};
- }
- // Check the entry is actually legit
- if(entry.tag != ACAMERA_REQUEST_AVAILABLE_CAPABILITIES ||
- entry.count == 0 || entry.type != ACAMERA_TYPE_BYTE || entry.data.i32 == nullptr){
- qInfo()<<QString("Bad available capabilities key: tag: %1 (expected %2), count %3 (expect > 0), "
- "type %4 (expected %5), data expected not null").
- arg(QString::number(entry.tag),
- QString::number(ACAMERA_REQUEST_AVAILABLE_CAPABILITIES),
- QString::number(entry.count),
- QString::number(entry.type),
- QString::number(ACAMERA_TYPE_BYTE));
- return {};
- }
- return entry;
- }
- std::tuple<std::string, bool> get_camera_depth_id(ACameraManager *cam_manager, int camera_facing)
- {
- auto camera_ids = get_camera_id_list(cam_manager);
- if(camera_ids){
- qInfo()<<__func__<<": found camera count "<<camera_ids->numCameras;
- for(int i = 0; i < camera_ids->numCameras; ++i){
- const char *id = camera_ids->cameraIds[i];
- camera_status_t ret = ACAMERA_OK;
- auto chars = get_camera_characteristics(cam_manager, id, &ret);
- if(ret != ACAMERA_OK){
- qInfo()<<__func__<<": cannot obtain characteristics of camera id = "<<id;
- continue;
- }
- auto const entry = get_camera_capabilities(chars.get(), &ret);
- if(ret != ACAMERA_OK){
- qInfo()<<__func__<<": cannot obtain capabilities of camera id = "<<id;
- continue;
- }
- ACameraMetadata_const_entry lens_info;
- ACameraMetadata_getConstEntry(chars.get(), ACAMERA_LENS_FACING, &lens_info);
- auto const facing = static_cast<acamera_metadata_enum_android_lens_facing_t>(lens_info.data.u8[i]);
- bool is_right_face = facing == camera_facing;
- bool support_bc = false, support_depth = false;
- for(uint32_t i = 0; i < entry.count; i++) {
- if(entry.data.u8[i] == ACAMERA_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE){
- support_bc = true;
- }
- if(entry.data.u8[i] == ACAMERA_REQUEST_AVAILABLE_CAPABILITIES_DEPTH_OUTPUT){
- support_depth = true;
- }
- }
- qInfo()<<__func__<<" support bc = "<<support_bc<<", support depth = "<<support_depth
- <<", is right face = "<<is_right_face;
- if(is_right_face && support_depth){
- qInfo()<<__func__<<": obtain depth camera id = "<<id;
- return {id, support_bc};
- }
- }
- }else{
- qInfo()<<__func__<<": cannot get depth cam";
- }
- return {};
- }
- std::string get_camera_id(ACameraManager *cam_manager, int camera_facing)
- {
- auto camera_ids = get_camera_id_list(cam_manager);
- if(!camera_ids){
- return {};
- }
- qInfo()<<__func__<<": found camera count "<<camera_ids->numCameras;
- std::string back_id;
- for(int i = 0; i < camera_ids->numCameras; ++i){
- const char *id = camera_ids->cameraIds[i];
- auto chars = get_camera_characteristics(cam_manager, id);
- ACameraMetadata_const_entry lensInfo;
- auto const ret = ACameraMetadata_getConstEntry(chars.get(), ACAMERA_LENS_FACING, &lensInfo);
- if(ret != ACAMERA_OK){
- qInfo()<<__func__<<": cannot get ACAMERA_LENS_FACING, error code = "<<ret;
- continue;
- }
- auto const facing = static_cast<acamera_metadata_enum_android_lens_facing_t>(lensInfo.data.u8[0]);
- // Found a back-facing camera?
- if (facing == camera_facing){
- back_id = id;
- break;
- }
- }
- return back_id;
- }
- void print_camera_props(ACameraManager *cam_manager, const char *id)
- {
- // exposure range
- ACameraMetadata *metadataObj;
- ACameraManager_getCameraCharacteristics(cam_manager, id, &metadataObj);
- ACameraMetadata_const_entry entry;
- ACameraMetadata_getConstEntry(metadataObj,
- ACAMERA_SENSOR_INFO_EXPOSURE_TIME_RANGE, &entry);
- int64_t minExposure = entry.data.i64[0];
- int64_t maxExposure = entry.data.i64[1];
- qInfo()<<__func__<<QString(" camProps: minExposure=%1 vs maxExposure=%2").arg(minExposure).arg(maxExposure);
- ////////////////////////////////////////////////////////////////
- // sensitivity
- ACameraMetadata_getConstEntry(metadataObj,
- ACAMERA_SENSOR_INFO_SENSITIVITY_RANGE, &entry);
- int32_t minSensitivity = entry.data.i32[0];
- int32_t maxSensitivity = entry.data.i32[1];
- qInfo()<<__func__<<QString("camProps: minSensitivity=%1 vs maxSensitivity=%2").
- arg(minSensitivity).arg(maxSensitivity);
- ////////////////////////////////////////////////////////////////
- // cam facing
- ACameraMetadata_getConstEntry(metadataObj,
- ACAMERA_SENSOR_ORIENTATION, &entry);
- int32_t orientation = entry.data.i32[0];
- qInfo()<<__func__<<QString("camProps: %1").arg(orientation);
- }
- std::unique_ptr<ACameraIdList, void(*)(ACameraIdList *)>
- get_camera_id_list(ACameraManager *cam_manager, camera_status_t *status)
- {
- ACameraIdList *camera_ids = nullptr;
- auto const ret = ACameraManager_getCameraIdList(cam_manager, &camera_ids);
- if(status){
- *status = ret;
- }
- if(ret != ACAMERA_OK){
- qInfo()<<__func__<<": cannot get ACameraIdList, error code = "<<ret;
- }
- return {camera_ids, ACameraManager_deleteCameraIdList};
- }
- std::unique_ptr<ACameraMetadata, void (*)(ACameraMetadata *)>
- get_camera_characteristics(ACameraManager *cam_manager, const char *camera_id, camera_status_t *status)
- {
- ACameraMetadata *meta = nullptr;
- auto const ret = ACameraManager_getCameraCharacteristics(cam_manager, camera_id, &meta);
- if(status){
- *status = ret;
- }
- if(ret != ACAMERA_OK) {
- qInfo()<<__func__<<": cannot get camera characteristics, error code = "<<ret;
- }
- return {meta, ACameraMetadata_free};
- }
- std::unique_ptr<AImage, void (*)(AImage *)> get_next_image(AImageReader *reader, int *status)
- {
- AImage *image = nullptr;
- auto const ret = AImageReader_acquireNextImage(reader, &image);
- if(status){
- *status = ret;
- }
- return {image, AImage_delete};
- }
- }
Add Comment
Please, Sign In to add comment