我已经使用桥接标头(将Swift连接到Objective C)和目标C包装器(将Objective C连接到C ++)集成了Swift IOS项目中的opencv。使用这种方法,我可以从Swift代码中传递单个图像,在C ++文件中对其进行分析,然后将其取回。
我已经看到opencv提供了可以与Objective C UIViewController集成的CvVideoCamera对象。
但是由于我的UIViewController是用Swift编写的,所以我想知道是否也可以这样做?
这是我自己有机会玩这个游戏后对我的最初答案的更新。是的,可以与用CvVideoCamera
Swift编写的视图控制器一起使用。如果您只想使用它来显示应用程序中摄像机的视频,那真的很容易。
#import <opencv2/highgui/cap_ios.h>
通过桥接头。然后,在您的视图控制器中:
class ViewController: UIViewController, CvVideoCameraDelegate {
...
var myCamera : CvVideoCamera!
override func viewDidLoad() {
...
myCamera = CvVideoCamera(parentView: imageView)
myCamera.delegate = self
...
}
}
该ViewController
实际上不能符合CvVideoCameraDelegate
协议,但CvVideoCamera
不会没有代理工作,所以我们通过声明变通解决此问题ViewController
通过该协议没有实施任何它的方法。这将触发编译器警告,但是来自摄像机的视频流将显示在图像视图中。
当然,您可能需要实现CvVideoCameraDelegate
的(仅)processImage()
方法来在显示视频帧之前对其进行处理。之所以不能在Swift中实现它,是因为它使用C ++类型Mat
。
因此,您将需要编写一个Objective-C ++类,其实例可以设置为相机的委托。该processImage()
Objective-C ++类中的方法将由调用,CvVideoCamera
并依次调用您的Swift类中的代码。以下是一些示例代码片段。在OpenCVWrapper.h
:
// Need this ifdef, so the C++ header won't confuse Swift
#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#endif
// This is a forward declaration; we cannot include *-Swift.h in a header.
@class ViewController;
@interface CvVideoCameraWrapper : NSObject
...
-(id)initWithController:(ViewController*)c andImageView:(UIImageView*)iv;
...
@end
在包装器实现中OpenCVWrapper.mm
(这是一个Objective-C ++类,因此是.mm扩展名):
#import <opencv2/highgui/cap_ios.h>
using namespace cv;
// Class extension to adopt the delegate protocol
@interface CvVideoCameraWrapper () <CvVideoCameraDelegate>
{
}
@end
@implementation CvVideoCameraWrapper
{
ViewController * viewController;
UIImageView * imageView;
CvVideoCamera * videoCamera;
}
-(id)initWithController:(ViewController*)c andImageView:(UIImageView*)iv
{
viewController = c;
imageView = iv;
videoCamera = [[CvVideoCamera alloc] initWithParentView:imageView];
// ... set up the camera
...
videoCamera.delegate = self;
return self;
}
// This #ifdef ... #endif is not needed except in special situations
#ifdef __cplusplus
- (void)processImage:(Mat&)image
{
// Do some OpenCV stuff with the image
...
}
#endif
...
@end
然后放入#import "OpenCVWrapper.h"
桥接头,Swift视图控制器可能如下所示:
class ViewController: UIViewController {
...
var videoCameraWrapper : CvVideoCameraWrapper!
override func viewDidLoad() {
...
self.videoCameraWrapper = CvVideoCameraWrapper(controller:self, andImageView:imageView)
...
}
有关前向声明和Swift / C ++ / Objective-C互操作的信息,请参见https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html。网上有很多有关#ifdef __cplusplus
和的信息extern "C"
(如果需要)。
在processImage()
委托方法中,您可能需要与某些OpenCV API交互,还必须为其编写包装程序。您可以在其他地方找到有关此信息的一些信息,例如在这里:在Swift iOS中使用OpenCV
更新09/03/2019
应社区要求,请参阅注释,示例代码已放置在GitHub上的https://github.com/aperedera/opencv-swift-examples。
另外,在撰写本文时,当前版本的OpenCV iOS框架不再允许Swift代码使用声明CvVideoCameraDelegate
协议的标头(现在在videoio / cap_ios.h中),因此您不能仅将其包含在桥接标头中并声明视图控制器符合该协议,以仅在您的应用中显示摄像机视频。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句