问题
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
- jdk : jdk1.8.0_201
- link of the adb logcat : put at google drive since it is long, You can find the crash point by the keywords "can get camera ids:"
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
Edit : looks like mobile of sony do not support camera api 2. Please check this link if you were interested.
来源:https://stackoverflow.com/questions/59246410/cannot-get-camera-list-by-android-ndkcamera2-api