有些三方庫不支持Pod管理,我們只能手動把庫文件拖進項目端壳,這種三方庫想在Flutter里引用并做成Plugin插件需要在.podspec配置文件里添加路徑
先創(chuàng)建一個Flutter Plugin項目州疾,然后新建一個文件夾(比如Framework)圾笨,把iOS三方庫拷貝到Framework文件夾下:
紅框里的5個文件就是騰訊OCR三方庫的所有文件乎芳,箭頭指向的tencent_ocr_plugin.podspec文件就是我們需要配置三方庫文件路徑的地方
在tencent_ocr_plugin.podspec文件中我們需要配置如下三個字段:
s.vendored_frameworks(三方庫的.framework文件路徑)
s.vendored_libraries(三方庫的.a文件路徑)
s.resource(三方庫使用的.bundle文件路徑)
如下:
由于這個騰訊OCR三方庫依賴幾個系統(tǒng)框架,我們在xcode中添加一下(其他三方庫按需添加)
PS:iOS系統(tǒng)框架也有在tencent_ocr_plugin.podspec文件中以s.frameworks字段配置的呼盆,我試了下沒有成功,可能是沒有用Pod管理三方庫的原因吧
下面開始寫插件代碼
首先在lib文件夾下的tencent_ocr_plugin.dart文件中寫好注冊MethodChannel的代碼
tencent_ocr_plugin.dart:
class TencentOcrPlugin {
static const MethodChannel _channel = const MethodChannel('tencent_ocr_plugin');
static Future<String?> startOCR(Map params) async {
final String? ocrResult = await _channel.invokeMethod(
'startOCR',
params,
);
return ocrResult;
}
}
這里通道名為tencent_ocr_plugin蚁廓,invokeMethod第一個參數(shù) 'startOCR' 為方法名访圃,第二個參數(shù) params 用來傳參給原生
接下來是iOS代碼,文件在 ios --> Classes --> TencentOcrPlugin.m相嵌,建議在xcode中編寫代碼腿时,會有錯誤提示
TencentOcrPlugin.m:
import <WBOCRService/WBOCRService.h>
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
FlutterMethodChannel* channel = [FlutterMethodChannel
methodChannelWithName:@"tencent_ocr_plugin"
binaryMessenger:[registrar messenger]];
TencentOcrPlugin* instance = [[TencentOcrPlugin alloc] init];
[registrar addMethodCallDelegate:instance channel:channel];
}
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"startOCR" isEqualToString:call.method]) {
NSLog(@"startOCR入?yún)? %@", call.arguments);
[self OCRConfig:result params:call.arguments];
} else {
result(FlutterMethodNotImplemented);
}
}
- (void)OCRConfig:(FlutterResult)result params:(NSDictionary *)params {
WBOCRConfig *config = [WBOCRConfig sharedConfig];
// 識別身份證人像面和國徽面
config.SDKType = WBOCRSDKTypeIDCardNormal;
// 這個參數(shù)默認是NO,設(shè)置為YES后,需要完成人相面和國徽面識別之后饭宾,才能識別結(jié)束流程
config.needBothSidesRecognized = YES;
// 拉起SDK
[[WBOCRService sharedService] startOCRServiceWithConfig:config version:@"1.0.0" appId:params[@"appId"] nonce:params[@"nonce"] userId:params[@"userId"] sign:params[@"sign"] orderNo:params[@"orderNo"] startSucceed:^{
NSLog(@"身份證識別SDK拉起");
} recognizeSucceed:^(id _Nonnull resultModel, id _Nullable extension) {
WBIDCardInfoModel *resultInfo = (WBIDCardInfoModel *)resultModel;
NSDictionary *resultDic = @{
@"name": resultInfo.name,
@"cardId": resultInfo.idcard,
@"frontImg": [self imageToBase64String:resultInfo.frontFullImg],
@"backImg": [self imageToBase64String:resultInfo.backFullImg],
};
NSLog(@"%@", resultDic);
NSError *error = nil;
NSData *ocrJsonData = [NSJSONSerialization dataWithJSONObject:resultDic options:kNilOptions error:&error];
NSString *ocrJsonString = [[NSString alloc] initWithData:ocrJsonData encoding:NSUTF8StringEncoding];
NSLog(@"OCR JSON: %@", ocrJsonString);
result(ocrJsonString);
} failed:^(NSError * _Nonnull error, id _Nullable extension) {
NSLog(@"%@", error);
}];
}
// 圖片轉(zhuǎn)base64
- (NSString *)imageToBase64String:(UIImage *)image {
NSData *imageData = UIImagePNGRepresentation(image);
NSString *base64String = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
// NSLog(@"%lu", (unsigned long)imageData.length);
return base64String;
}
registerWithRegistrar方法中注冊MethodChannel
handleMethodCall方法中監(jiān)聽通道下的方法
方法名用call.method獲取批糟,傳遞的參數(shù)用call.arguments獲取
監(jiān)聽到你設(shè)置好的方法,執(zhí)行三方庫代碼即可
到這里插件就寫完了捏雌,如果想在其他Flutter項目中使用該插件跃赚,需要把插件先上傳github或其他代碼托管平臺
然后在新項目的pubspec.yaml中引用該plugin
在需要的地方調(diào)用即可:
// 喚起騰訊OCR(iOS)
startOCR(Map params) async {
try {
String? ocrResult = await TencentOcrPlugin.startOCR(params);
print(ocrResult);
} catch (e) {
print(e);
}
}