I've integrated opencv in Swift IOS project using bridging header (to connect Swift to Objective C) and a Objective C wrapper (to connect Objective C to C++). Using this method I can pass single images from the Swift code, analyse them in the C++ files and get them back.
But since my UIViewController are written in Swift I've wondered if this is possible as well?
This is an update to my initial answer after I had a chance to play with this myself. Yes, it is possible to use
CvVideoCamera
with a view controller written in Swift. If you just want to use it to display video from the camera in your app, it's really easy.#import <opencv2/highgui/cap_ios.h>
via the bridging header. Then, in your view controller:The
ViewController
cannot actually conform to theCvVideoCameraDelegate
protocol, butCvVideoCamera
won't work without a delegate, so we work around this problem by declaringViewController
to adopt the protocol without implementing any of its methods. This will trigger a compiler warning, but the video stream from the camera will be displayed in the image view.Of course, you might want to implement the
CvVideoCameraDelegate
's (only)processImage()
method to process video frames before displaying them. The reason you cannot implement it in Swift is because it uses a C++ type,Mat
.So, you will need to write an Objective-C++ class whose instance can be set as camera's delegate. The
processImage()
method in that Objective-C++ class will be called byCvVideoCamera
and will in turn call code in your Swift class. Here are some sample code snippets. InOpenCVWrapper.h
:In the wrapper implementation,
OpenCVWrapper.mm
(it's an Objective-C++ class, hence the .mm extension):Then you put
#import "OpenCVWrapper.h"
in the bridging header, and the Swift view controller might look like this:See https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html about forward declarations and Swift/C++/Objective-C interop. There is plenty of info on the web about
#ifdef __cplusplus
andextern "C"
(if you need it).In the
processImage()
delegate method you will likely need to interact with some OpenCV API, for which you will also have to write wrappers. You can find some info on that elsewhere, for example here: Using OpenCV in Swift iOS