c++ - 访问 mac osx 应用程序包中资源文件夹中的文件

标签 c++ macos qt

我想访问应用程序包中资源内的文件。不幸的是我不能使用 QT 资源,因为我使用的是来自 opencv 的 CascadeClassifier。我目前的路径是

const std::string FACE_CLASIFIER_PATH = "/Resources/haarcascade_frontalface_default.xml";
const std::string EYES_CLASIFIER_PATH = "/Resources/haarcascade_mcs_eyepair_big.xml";

我也试过

const std::string FACE_CLASIFIER_PATH = "../Resources/haarcascade_frontalface_default.xml";
const std::string EYES_CLASIFIER_PATH = "../Resources/haarcascade_mcs_eyepair_big.xml";

但它们都不起作用。至于配置,这两个文件都存在于 MyApp.app/Contents/Resources 中,我使用 qmake 包含它们

mac {
APP_XML_FILES.files = ../haarcascade_frontalface_default.xml ../haarcascade_mcs_eyepair_big.xml
APP_XML_FILES.path = Contents/Resources
QMAKE_BUNDLE_DATA += APP_XML_FILES
}

如果能帮助解决这个问题,我将不胜感激

最佳答案

你没有说你想用这些文件做什么,虽然这可能是因为我对 opencv 缺乏了解。但是,您可以使用 Core Foundation 类来获取资源文件夹中文件的路径:-

CFURLRef appUrlRef;
appUrlRef = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("somefile"), NULL, NULL);

// do something with the file
//...

// Ensure you release the reference
CFRelease(appUrlRef);

通过 CFURLRef,您可以使用 Apple's documentation从中得到你需要的东西。

例如,如果你想要一个文件路径:-

CFStringRef filePathRef = CFURLCopyPath(appUrlRef);

//始终释放使用具有 "create or "copy" in its name 的函数检索到的项目 CFRelease(filePathRef);

从文件路径中,我们可以得到一个char*到路径:-

const char* filePath = CFStringGetCStringPtr(filePathRef, kCFStringEncodingUTF8);

因此,将它们放在一起,如果您想获得 haarcascade_mcs_eyepair_big.xml 的 char* 路径:-

CFURLRef appUrlRef = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("haarcascade_mcs_eyepair_big.xml"), NULL, NULL);
CFStringRef filePathRef = CFURLCopyPath(appUrlRef);
const char* filePath = CFStringGetCStringPtr(filePathRef, kCFStringEncodingUTF8);

// Release references
CFRelease(filePathRef);
CFRelease(appUrlRef);

关于c++ - 访问 mac osx 应用程序包中资源文件夹中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24165681/

相关文章:

c++ - 取值后程序停止

C++,RapidXML : function to get total of siblings nodes

ios - Xcode 模拟器卡在 "Launching Simulator"并在 Xcode 项目中卡住加载

objective-c - Cocoa/OSX - 当标题栏被隐藏时,NSTextField 不响应单击以开始文本编辑

c++ - Qt 5.3 和 Win SDK 7.1 : Cannot open include file ammintrin. h

c++ - 经典的生产者消费者线程

macos - 如何找到旧版本的 Node ?

qt - 程序意外结束

c++ - 使用 Qt 线程和信号的缓冲区溢出

c++ - "universal-character-name encountered in source"警告的目的是什么?