Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Cannot get camera list by android NDK
Forum Updated to NodeBB v4.3 + New Features

Cannot get camera list by android NDK

Scheduled Pinned Locked Moved Solved Mobile and Embedded
3 Posts 1 Posters 605 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tham
    wrote on 9 Dec 2019, 10:02 last edited by tham 12 Oct 2019, 14:18
    #1

    Trying to build an app with Qt5.12.3 and android ndk, but cannot get camera list by "ACameraManager_getCameraIdList".

    Mobile : Sony xperia XA1 Ultra
    Qt: 5.12.3
    NDK : r19c
    minimum target: 24
    maximum target: 28
    Android library link to : libcamera2ndk.so and libmediandk.so, I link to the libs of api 28, not 24(android-28\arch-arm\usr\lib)
    permissions: <uses-permission android:name="android.permission.CAMERA"/>
    arch : armeabi-v7a
    

    Extra so files I add into the project

    contains(ANDROID_TARGET_ARCH, armeabi-v7a) {
        ANDROID_EXTRA_LIBS = \
            C:/Users/yyyy/programs/Qt/android_camera/../../../3rdLibs/Android/android-ndk-r19c-windows-x86_64/android-ndk-r19c/platforms/android-28/arch-arm/usr/lib/libcamera2ndk.so
    }
    

    Source codes:

    #include "mainwindow.hpp"
    #include <QApplication>
    
    #include <QDebug>
    
    #ifdef Q_OS_ANDROID
    
    #include <android/log.h>
    
    #include <camera/NdkCameraManager.h>
    
    #include <QDebug>
    #include <QtAndroid>
    #include <QtAndroidExtras>
    
    void myMessageHandler(
            QtMsgType type,
            const QMessageLogContext& context,
            const QString& msg
            )
    {
        const char*const applicationName="android_camera";
    
        QString report=msg;
        if (context.file && !QString(context.file).isEmpty()) {
            report+=" in file ";
            report+=QString(context.file);
            report+=" line ";
            report+=QString::number(context.line);
        }
        if (context.function && !QString(context.function).isEmpty()) {
            report+=+" function ";
            report+=QString(context.function);
        }
        const char*const local=report.toLocal8Bit().constData();
        switch (type) {
        case QtDebugMsg:
            __android_log_write(ANDROID_LOG_DEBUG,applicationName,local);
            break;
        case QtInfoMsg:
            __android_log_write(ANDROID_LOG_INFO,applicationName,local);
            break;
        case QtWarningMsg:
            __android_log_write(ANDROID_LOG_WARN,applicationName,local);
            break;
        case QtCriticalMsg:
            __android_log_write(ANDROID_LOG_ERROR,applicationName,local);
            break;
        case QtFatalMsg:
            __android_log_write(ANDROID_LOG_FATAL,applicationName,local);
            abort();
        }
    }
    
    void open_android_camera()
    {
        auto result = QtAndroid::checkPermission(QString("android.permission.CAMERA"));
        if(result == QtAndroid::PermissionResult::Denied){
            QtAndroid::PermissionResultMap resultHash =
                    QtAndroid::requestPermissionsSync(QStringList({"android.permission.CAMERA"}));
            if(resultHash["android.permission.CAMERA"] == QtAndroid::PermissionResult::Denied){
                qInfo()<<__func__<<"permission deny";
            }else{
                qInfo()<<__func__<<"permission granted";
            }
        }
    
        qInfo()<<__func__<<"create camera manager";
        ACameraManager *camera_manager_ = ACameraManager_create();
        if(camera_manager_){
            qInfo()<<__func__<<"get camera ids";
            ACameraIdList *camera_ids = nullptr;
            auto const status = ACameraManager_getCameraIdList(camera_manager_, &camera_ids);
            qInfo()<<__func__<<"can get camera ids:"<<(camera_ids != nullptr)<<", status:"<<status;
            switch(status){
            case ACAMERA_OK:
                qInfo()<<__func__<<"ACAMERA_OK";
                break;
            case ACAMERA_ERROR_INVALID_PARAMETER:
                qInfo()<<__func__<<"ACAMERA_ERROR_INVALID_PARAMETER";
                break;
            case ACAMERA_ERROR_CAMERA_DISCONNECTED:
                qInfo()<<__func__<<"ACAMERA_ERROR_CAMERA_DISCONNECTED";
                break;
            case ACAMERA_ERROR_NOT_ENOUGH_MEMORY:
                qInfo()<<__func__<<"ACAMERA_ERROR_NOT_ENOUGH_MEMORY";
                break;
            default:
                qInfo()<<__func__<<"unknown status codes:"<<status;
            }
            if(camera_ids){
                qInfo()<<__func__<<"camera number:"<<camera_ids->numCameras;
            }
        }else{
            qInfo()<<__func__<<"cannot create camera manager";
        }
    }
    
    #endif
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
    #ifdef Q_OS_ANDROID
        qInstallMessageHandler(myMessageHandler);
        open_android_camera();
    #endif
    
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    
    

    Error code returned by ACameraManager_getCameraIdList is -866653748, none of the value of NdkCameraError.h looks like that, weird

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tham
      wrote on 11 Dec 2019, 16:06 last edited by
      #2

      Do I need to create an Activity and initialize the camera in the onResume function?

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tham
        wrote on 20 Dec 2019, 08:43 last edited by
        #3

        I think I know why(guess), although my phone support camera2, but sony disable the api, please check this post for more details.

        1 Reply Last reply
        2

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved