#import "ViewController.h" #import <CoreImage/CoreImage.h> #import <AVFoundation/AVFoundation.h> @interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate> @property (weak, nonatomic) IBOutlet UIImageView *codeImage; @property (weak, nonatomic) IBOutlet UIButton *makeCode; @property (weak, nonatomic) IBOutlet UITextField *codeTetxt; @property(nonatomic ,strong) CIFilter *filter; @property(nonatomic ,strong) AVCaptureMetadataOutput * meta ; @property(nonatomic ,strong)AVCaptureSession * session; @property(nonatomic ,strong)AVCaptureDeviceInput * input; @property(nonatomic ,strong)AVCaptureVideoPreviewLayer * previewLayer; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //生成二维码 [self make2dCode]; //开始扫描二维码 [self statScan]; } -(void)statScan { //创建获取捕捉设备 设置类型 AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; //获取输入设备 AVCaptureDeviceInput * input = [[AVCaptureDeviceInput alloc]initWithDevice:device error:nil]; self.input = input; //获取输出设备元数据 AVCaptureMetadataOutput * meta = [[AVCaptureMetadataOutput alloc]init]; self.meta = meta; //创建会话 AVCaptureSession * session = [[AVCaptureSession alloc]init]; self.session = session; if ([session canAddInput:input]) { [session addInput:input]; } if ([session canAddOutput:meta]) { [session addOutput:meta]; } } -(void)make2dCode { //获取内置滤镜 NSLog(@"%@" , [CIFilter filterNamesInCategory:kCICategoryBuiltIn]); //设置滤镜,支持二维码 CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; self.filter = filter; //设置初始值 [filter setDefaults]; // self.makeCode.enabled = NO; //获取封装数据 字符串 inputMessage NSLog(@"%@" ,filter.inputKeys); //监听键盘frame是否变化 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)]; [self.view addGestureRecognizer:tap]; } //当解析完毕调用, 返回一个字符串 - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { AVMetadataMachineReadableCodeObject * objc = [metadataObjects firstObject]; NSLog(@"%@" ,objc.stringValue); [self.session startRunning]; [self.previewLayer removeFromSuperlayer]; [self show2dCodeContent: objc.stringValue]; } -(void)show2dCodeContent:(NSString *) code { UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"扫描成功" message:code preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction * action = [UIAlertAction actionWithTitle:@"确定"style:UIAlertActionStyleDestructive handler:nil]; UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:nil]; [alertC addAction:action]; [alertC addAction:action1]; [self presentViewController:alertC animated:YES completion:nil]; } //生成二维码按钮 - (IBAction)makeCodeClick:(id)sender { [self.view endEditing:YES]; self.view.transform = CGAffineTransformIdentity; //设置二维码内容 [self.filter setValue:[self.codeTetxt.text dataUsingEncoding:NSUTF8StringEncoding] forKey:@"inputMessage"]; //生成二维码图片 放大图片 CIImage * twoDCode = [self.filter.outputImage imageByApplyingTransform:CGAffineTransformMakeScale(5, 5)]; self.codeImage.image = [UIImage imageWithCIImage:twoDCode]; } /* UIKeyboardAnimationDurationUserInfoKey = "0.25"; UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}"; UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}"; UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}"; UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}"; UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}"; UIKeyboardIsLocalUserInfoKey = 1; }*/ //监控键盘frame改变的通知方法 -(void)keyboardDidChangeFrame:(NSNotification *)notify { NSLog(@"%@" , notify.userInfo); CGRect rect = [notify.userInfo[@"UIKeyboardFrameEndUserInfoKey"]CGRectValue]; if (rect.origin.y == [UIScreen mainScreen].bounds.size.height-rect.size.height) { self.view.transform = CGAffineTransformMakeTranslation(0, -rect.size.height); }else if (rect.origin.y == [UIScreen mainScreen].bounds.size.height){ self.view.transform = CGAffineTransformIdentity; } } -(void)tapClick:(UIGestureRecognizer *)tap { [self.view endEditing:YES]; } //textLable事件 - (IBAction)codeTextValueChange:(id)sender { if ([self.codeTetxt hasText]) { self.makeCode.enabled = YES; }else{ self.makeCode.enabled = NO; } } //扫描二维码 - (IBAction)scan2dCode:(id)sender { //只有将输入设备与输出设备建立会话之后, 才能获取输出设备的元数据类型 NSLog(@"---------%@" ,self.meta.availableMetadataObjectTypes); //设置输出元数据类型 二维码 [self.meta setMetadataObjectTypes:@[@"org.iso.QRCode"]]; //设置元数据代理 [self.meta setMetadataObjectsDelegate:self queue:dispatch_get_global_queue(0, 0)]; //设置会话范围(也就是视频范围) [self.session setSessionPreset:AVCaptureSessionPresetHigh]; //开始会话 [self.session startRunning]; //添加一个预览画面, 来展示输入设备画面 AVCaptureVideoPreviewLayer * previewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:self.session]; self.previewLayer = previewLayer; previewLayer.frame = self.view.bounds; [self.view.layer addSublayer:previewLayer]; }