![1594482-f95ab4e1f4e4b49c.png](https://i-blog.csdnimg.cn/blog_migrate/078a59cae13029953e1a09973be8efdb.webp?x-image-process=image/format,png)
897C5481-C48E-4CC2-BF3D-313857B9BCC5.png
截止2017-5-8 号,weex框架没有提供扫描二维码的模块,所以就有了本节的内容
本节学习目标
- 自定义一个扫描二维码模块
先看一下如何使用这个模块
- 第一步引入这个模块
var camera = weex.requireModule('camera')
- 调用打开照相机模块方法
APIscanCode(callback)
参数
callback 回调函数,参数res是一个对象,包含下面两个键名
res.result 值为success,fail
res.data 扫码识别出来的二维码 值为string 类型
示例如下
camera.scanCode(res=>{
if(res.result){
this.show(res.data,1)
}
})
介绍完了模块的使用方法,下面就开始自定这个模块
大体步骤分为两步
- 第一步 定义模块
- 第二步 注册模块
下面我们正式开始
第一步 定义一个模块(以iOS 为例)
1.创建一个类名为WXCameraModule
![1594482-2d9d5b479abd42e1.png](https://i-blog.csdnimg.cn/blog_migrate/f18abe7e1548f1f91a0b251b38480429.webp?x-image-process=image/format,png)
75D547FF-93F3-41EB-9B69-7A97234527A0.png
![1594482-3a52972cd74cb013.png](https://i-blog.csdnimg.cn/blog_migrate/886e76af7bf1c75888f9c1cc1637e4dd.webp?x-image-process=image/format,png)
78D002E9-7708-41A0-B582-EFA67E3657E3.png
2.导入框架 <WeexSDK/WeexSDK.h>,然后让WXCameraModule 实现代理WXModuleProtocol
#import "WXEventModule.h"
#import <WeexSDK/WeexSDK.h>
#import "WXCustomModule.h"
typedef void (^CallBlock)(NSDictionary*); // 定义回调函数
@interface WXCameraModule :NSObject<WXModuleProtocol>
@end
WXCameraModule.m文件中实现代理
#import "WXCameraModule.h"
#import "XJScanViewController.h"
@implementation WXCameraModule
@synthesize weexInstance; // 这个必须实现
// 把scanCode 方法暴露给weex,这部一定要做,否则weex将无法解析到js文件中的 camera 模块的scanCode 方法
WX_EXPORT_METHOD(@selector(scanCode:))
-(void)scanCode:(CallBlock)callback{
// 下面这个是我定义的扫描二维码的原生类
XJScanViewController *scanVC= [[XJScanViewController alloc]init];
// 将回调函数传递给scanVC 视图控制器,如果扫描到二维码将值传递给此闭包函数
scanVC.callback = callback;
[self.weexInstance.viewController.navigationController presentViewController:scanVC animated:true completion:nil];
}
@end
第二步 在weex中注册这个模块
WXSDKEngine.registerModule("camera", with: WXCameraModule.self)
这个是swift 语法,因为的我的项目是swift和oc汇编的,以上两部完成后,就可以在js中调用照相机扫描二维码了
下面是XJScanViewController.m 源码,供大家参考
#import "XJScanViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface XJScanViewController () <AVCaptureMetadataOutputObjectsDelegate>
@property (nonatomic, strong) AVCaptureSession * session;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *captureLayer;
@property (nonatomic, strong) UIView *sanFrameView;
@end
@implementation XJScanViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setup];
}
-(void)setup{
#if !(TARGET_IPHONE_SIMULATOR)
self.session = [[AVCaptureSession alloc]init];
[_session setSessionPreset:AVCaptureSessionPresetHigh];
AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init];
if (output && input && device) {
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[_session addInput:input];
[_session addOutput:output];
output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];
}
_captureLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
_captureLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;
_captureLayer.frame=self.view.layer.bounds;
#endif
[self.view.layer addSublayer:_captureLayer];
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(self.view.bounds.size.width/2-25, self.view.bounds.size.height-100, 50, 50)];
[button setBackgroundImage:[UIImage imageNamed:@"cancle.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)back{
[self dismissViewControllerAnimated:true completion:nil];
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
[_captureLayer removeFromSuperlayer];
[_session stopRunning];
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
if (metadataObjects.count > 0) {
AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];
self.callback(@{@"result":@"true",@"data":metadataObject.stringValue});
[self dismissViewControllerAnimated:true completion:nil];
}
}
-(void)viewWillAppear:(BOOL)animated{
[_session startRunning];
}
- (void)dealloc {
[_captureLayer removeFromSuperlayer];
}
不知道大家没有注意到回调函数我传的参数是一个NSDictionary 对象self.callback(@{@"result":@"true",@"data":metadataObject.stringValue});
它会被weex自定转换为js中的对象