在XPC服务中可以使用AVFoundation和OpenCV吗?我有这么简单的代码
#include <opencv2/opencv.hpp>
#import <AVFoundation/AVFoundation.h>
@interface AppDelegate(){
cv::VideoCapture m_vidCap;
}..。
//Hacky way of forcing OpenCV to call AVFoundation for the first time
//before the camera arrives
//OpenCV should add a better way of mapping camera indexes to cameras
//This way we force that devices are enumerated in the same order here
//and in their code
m_vidCap.open(-1);
m_vidCap.release();
[AVCaptureDevice devices]; //Force avfoundation "startup"
@autoreleasepool {
std::vector<std::wstring> devices;
// chosen device.
NSArray *osDevices=[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding ( kCFStringEncodingUTF32LE );
AVCaptureDevice *device;
int did=0;
for(device in osDevices) {
NSString *deviceName=[device localizedName];
NSData* wstrdata = [deviceName dataUsingEncoding : encoding ];
std::wstring cppDeviceName=std::wstring((wchar_t*) [ wstrdata bytes ], [ wstrdata length] / sizeof ( wchar_t ) );
devices.push_back(cppDeviceName);
//NSLog("Device %d: %S at position %ld",did,cppDeviceName.c_str(),device.position);
did++;
}
}当在常规Cocoa应用程序中启动时,它可以根据需要工作。它将枚举两个相机,FaceTime的一个和一个USB的。
如果我在XPC服务中放置相同的代码,它将永远挂在
m_vidCap.open(-1);再也不会执行了。
我假设在XPC服务中可以使用什么是有一些限制的,但是我不能搜索任何有用的东西。任何投入都是非常感谢的。Thx
发布于 2015-02-05 06:05:31
嗯,经过多次的尝试和苹果的样品检查,我已经开始工作了。问题在于NSXPCListener是如何创建的。在我像普通的服务一样
NSXPCListener *listener = [NSXPCListener serviceListener];但没起作用。
在我换到
ToolMonitorDelegate *myDelegate = [[ToolMonitorDelegate alloc] initWithHardwareType:HT_Vision];
NSXPCListener *listener = [[NSXPCListener alloc] initWithMachServiceName:bundleId];
listener.delegate = myDelegate;
[listener resume];
[[NSRunLoop currentRunLoop] run];一切都马上就好了。在我看来,OpenCV需要运行app循环才能正常工作。如果我错了,或者有另一个解释,请纠正我。
发布于 2015-05-15 10:55:19
你就快到了!我认为AVFoundation需要使用NSRunLoop,但是XPC服务默认使用dispatch_main。您需要在XPC服务的XPCService文件中向由XPCService键标识的字典值添加一个键。默认情况下,它应该有一个ServiceType键。添加一个RunLoopType键,并给它一个字符串值NSRunLoop。
有关更多信息,请参见创建XPC服务。
https://stackoverflow.com/questions/28314877
复制相似问题