-
iOS) Swift에 opencv 빌드하기Programing Language/iOS(Swift) 2020. 11. 22. 23:29728x90반응형
참고 자료
https://www.timpoulsen.com/2019/using-opencv-in-an-ios-app.html
1. https://opencv.org/releases.html 에서 3.4.4 다운로드 받기
2. 프로젝트 폴더에 opencv2.framework 복사하기
- (복사 할때 유의 사항)
- Make sure Copy items if needed is checked
- Make sure Create folder references is checked
- Make sure Add to targets: your_project is checked
3. Cocoa Touch Class 파일 생성
- OpenCVWrapper란 이름으로 생성
- It should subclass NSOBject
- It should be an Objective-C file
- When prompted, click the Create Bridging Header button
4. Open the YourApp-Bridging-Header.h file and add this line:
- 파일안에 #import "OpenCVWrapper.h" 적기
5. OpenCVWrapper.m 파일 OpenCVWrapper.mm으로 바꾸기
- 코드 넣기
- #import <opencv2/opencv.hpp>
- #import "OpenCVWrapper.h"
6. Finally, we need to set up the prefix header. Again, there's not much that you'll need to do with this file. It will simply tell Xcode to not recompile the already-compiled OpenCV framework to speed up your builds.
- Choose File > New > File
- Scroll to near the bottom and choose PCH File
- Make sure your app is checked in the Targets list
- Click Create
Inside the prefix header, before the closing #endif statement, add:
#ifdef __cplusplus #include <opencv2/opencv.hpp> #endif
설치완료후 잘 빌드 되었는지 확인하기
Implementing an OpenCV function
The basic steps you'll follow to implement an OpenCV function and use it in your app are:
- Add the function signature to the OpenCVWrapper.h file
- Add the function's implementation code in the OpenCVWrapper.mm file
- Call that function in your Swift file
For example, your .h header declares the function and its signature, which might look like this:
---------------------------------------------------------------------------
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface OpenCVWrapper : NSObject + (NSString *)openCVVersionString;
@end
NS_ASSUME_NONNULL_END
---------------------------------------------------------------------------
Your .mm file would have the corresponding function implementation:
------------------------------------------------------------------------------------------
#import <opencv2/opencv.hpp>
#import "OpenCVWrapper.h"
@implementation OpenCVWrapper
+ (NSString *)openCVVersionString {
return [NSString stringWithFormat:@"OpenCV Version %s", CV_VERSION];
}
@end
------------------------------------------------------------------------------------------
Per Objective-C syntax, the + indicates a class method; use - to indicate an instance method. The difference is probably inconsequential since there will only ever be one instance of your OpenCVWrapper class. So, you can probably use + to prefix all of your functions as shown above.
OK, so let's use this powerful new OpenCV capability. In your main ViewController.swift file, update viewDidLoad to call that new function:
------------------------------------------------------------------------------------------
override func viewDidLoad() {
super.viewDidLoad()
print("\(OpenCVWrapper.openVersionString())")
}
------------------------------------------------------------------------------------------
Build to a simulator and watch the Xcode debug console and you should see OpenCV Version 3.4.4 there. Congratulations, you've integrated OpenCV into your iOS app.
opencv함수를 swift로 빌드하기 위해 가장 중요한것은
OpenCVWrapper.h 에 함수명을 적고
OpenCVWrapper.mm 파일에 OpenCVWrapper에 적은 함수명을 그대로 적은뒤 c++ 코드를 입력해주는 것 이다.
맛있는 간식을 먹으면 집중력이 2배!
728x90반응형'Programing Language > iOS(Swift)' 카테고리의 다른 글
iOS) 테스트중인 기기의 모델명 알아내는 방법 (0) 2020.12.06 iOS)인앱결제 추가 (참고자료) (0) 2020.12.05 아이폰 기기별 해상도와 화면 크기 (0) 2020.08.27 아이폰 기기별 해상도 (0) 2020.08.27 iOS) Apple 로그인 연동 (0) 2020.07.26