步骤://1.创建一个输入设备(扫描)摄像头->采集信息AVCaptureDeviceInput
//2.输出设备解析采集到信息AVCaptureMetadataOutput
//3.会话联系输入跟输出AVCa
ptureSession
//4.特殊的视图来展示输入设备采集到的信息AVCaptureVideoPreviewLayer
//5.代理
//6.开启会话
//设置扫描范围
[self.sessionsetSessionPreset:AVCaptureSessionPresetHigh];
[self.sessionstartRunning];
扩展:
AVCaptureDevice*inputDevice = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];
AVF_EXPORTNSString*constAVMediaTypeVideoNS_AVAILABLE(10_7,4_0);
AVF_EXPORTNSString*constAVMediaTypeAudioNS_AVAILABLE(10_7,4_0);
AVF_EXPORTNSString*constAVMediaTypeTextNS_AVAILABLE(10_7,4_0);
AVF_EXPORTNSString*constAVMediaTypeClosedCaptionNS_AVAILABLE(10_7,4_0);
AVF_EXPORTNSString*constAVMediaTypeSubtitleNS_AVAILABLE(10_7,4_0);
AVF_EXPORTNSString*constAVMediaTypeTimecodeNS_AVAILABLE(10_7,4_0);
AVF_EXPORTNSString*constAVMediaTypeMetadataNS_AVAILABLE(10_8,6_0);
AVF_EXPORTNSString*constAVMediaTypeMuxedNS_AVAILABLE(10_7,4_0);
#import "ViewController.h"
@interface ViewController ()
@property (strong ,nonatomic)UIWebView *shaomaView;
//1.创建一个输入设备 (扫描) 摄像头 -> 采集信息
@property(nonatomic,strong)AVCaptureDeviceInput *input;
//2.输出设备 解析采集到信息
//元数据
@property(nonatomic,strong) AVCaptureMetadataOutput *output;
//3.会话 联系 输入跟输出
@property(nonatomic,strong) AVCaptureSession *session;
//4.特殊的视图 来展示输入设备采集到的信息
@property(nonatomic,strong) AVCaptureVideoPreviewLayer *layer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.shaomaView];
//1.输入设备 : 1.摄像头 2.麦克风 3.键盘鼠标
AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
self.input = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];
//2.输出设备
self.output = [[AVCaptureMetadataOutput alloc]init];
//3.会话 联系输入和输出
self.session = [[AVCaptureSession alloc]init];
if ([self.session canAddInput:self.input]) {
[self.session addInput:self.input];
}
if ([self.session canAddOutput:self.output]) {
[self.session addOutput:self.output];
}
//4.特殊的视图
self.layer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:self.session];
self.layer.frame = self.view.bounds;
[self.view.layer addSublayer:self.layer];
//5.代理
[self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
self.output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
//6.开启会话
//设置扫描范围
[self.session setSessionPreset:AVCaptureSessionPresetHigh];
[self.session startRunning];
}
/**
* 扫描到信息就会调用
*
* @param captureOutput
* @param metadataObjects
* @param connection
*/
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
[self.layer removeFromSuperlayer];
[self.session stopRunning];
for (AVMetadataMachineReadableCodeObject * objc in metadataObjects) {
NSLog(@"%@",objc.stringValue);
//跳转控制器
//http://weixinpay.www.baidu.com push
[self.shaomaView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:objc.stringValue]]];
[self.shaomaView reload];
}
}
- (UIWebView *)shaomaView{
if (!_shaomaView) {
_shaomaView = [[UIWebView alloc]initWithFrame:self.view.frame];
}
return _shaomaView;
}
@end