#import "ScanViewController.h" #import #import "ViewController.h" #import "JSBridge.h" #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width #define SCREEN_BOUNDS [UIScreen mainScreen].bounds #define TOP (SCREEN_HEIGHT-220)/2 #define LEFT (SCREEN_WIDTH-220)/2 #define kScanRect CGRectMake(LEFT, TOP, 220, 220) @interface ScanViewController (){ int num; BOOL upOrdown; NSTimer * timer; CAShapeLayer *cropLayer; } @property (strong,nonatomic)AVCaptureDevice * device; @property (strong,nonatomic)AVCaptureDeviceInput * input; @property (strong,nonatomic)AVCaptureMetadataOutput * output; @property (strong,nonatomic)AVCaptureSession * session; @property (strong,nonatomic)AVCaptureVideoPreviewLayer * preview; @property (strong,nonatomic)UIImageView * line; @end @implementation ScanViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } -(void)configView{ UIImageView * imageView = [[UIImageView alloc]initWithFrame:kScanRect]; imageView.image = [UIImage imageNamed:@"pick_bg"]; [self.view addSubview:imageView]; upOrdown = NO; num =0; _line = [[UIImageView alloc] initWithFrame:CGRectMake(LEFT, TOP+10, 220, 2)]; _line.image = [UIImage imageNamed:@"line.png"]; [self.view addSubview:_line]; timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation1) userInfo:nil repeats:YES]; UIImage* pImageButtonBack = [UIImage imageNamed:@"close.png"]; float offset = 30; float width = pImageButtonBack.size.width; float height = pImageButtonBack.size.height; UIButton* pButtonBack = [[UIButton alloc] initWithFrame:CGRectMake(0,0,width,height)]; pButtonBack.titleEdgeInsets = UIEdgeInsetsMake(44.0f, 0.0f, 0.0f, 0.0f); //[pButtonBack setTitle:@"后退" forState:UIControlStateNormal]; [pButtonBack setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; pButtonBack.titleLabel.font = [UIFont systemFontOfSize:13.0]; [pButtonBack setBackgroundImage:pImageButtonBack forState:UIControlStateNormal]; pButtonBack.center = CGPointMake(offset,offset); [pButtonBack addTarget:self action:@selector(buttonClickBack:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:pButtonBack]; } -(void)buttonClickBack:(id)sender { [self dismissViewControllerAnimated:NO completion:nil]; } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear: animated]; [self configView]; //[self setCropRect:kScanRect]; [self performSelector:@selector(setupCamera) withObject:nil afterDelay:0.3]; } -(void)animation1 { if (upOrdown == NO) { num ++; _line.frame = CGRectMake(LEFT, TOP+10+2*num, 220, 2); if (2*num == 200) { upOrdown = YES; } } else { num --; _line.frame = CGRectMake(LEFT, TOP+10+2*num, 220, 2); if (num == 0) { upOrdown = NO; } } } - (void)setCropRect:(CGRect)cropRect{ cropLayer = [[CAShapeLayer alloc] init]; CGMutablePathRef path = CGPathCreateMutable(); CGPathAddRect(path, nil, cropRect); CGPathAddRect(path, nil, self.view.bounds); [cropLayer setFillRule:kCAFillRuleEvenOdd]; [cropLayer setPath:path]; [cropLayer setFillColor:[UIColor blackColor].CGColor]; [cropLayer setOpacity:0.6]; [cropLayer setNeedsDisplay]; [self.view.layer addSublayer:cropLayer]; } - (void)setupCamera { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if (device==nil) { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"设备没有摄像头" preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]]; [self presentViewController:alert animated:YES completion:nil]; return; } // Device _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; // Input _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil]; // Output _output = [[AVCaptureMetadataOutput alloc]init]; [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; //设置扫描区域 CGFloat top = TOP/SCREEN_HEIGHT; CGFloat left = LEFT/SCREEN_WIDTH; CGFloat width = 220/SCREEN_WIDTH; CGFloat height = 220/SCREEN_HEIGHT; ///top 与 left 互换 width 与 height 互换 [_output setRectOfInterest:CGRectMake(top,left, height, width)]; // Session _session = [[AVCaptureSession alloc]init]; [_session setSessionPreset:AVCaptureSessionPresetHigh]; if ([_session canAddInput:self.input]) { [_session addInput:self.input]; } if ([_session canAddOutput:self.output]) { [_session addOutput:self.output]; } // 条码类型 AVMetadataObjectTypeQRCode [_output setMetadataObjectTypes:[NSArray arrayWithObjects:AVMetadataObjectTypeQRCode, nil]]; // Preview _preview =[AVCaptureVideoPreviewLayer layerWithSession:_session]; _preview.videoGravity = AVLayerVideoGravityResizeAspectFill; _preview.frame =self.view.layer.bounds; [self.view.layer insertSublayer:_preview atIndex:0]; // Start [_session startRunning]; } #pragma mark AVCaptureMetadataOutputObjectsDelegate - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { NSString *stringValue; if ([metadataObjects count] >0) { //停止扫描 [_session stopRunning]; [timer setFireDate:[NSDate distantFuture]]; AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0]; stringValue = metadataObject.stringValue; [JSBridge onScanResult:stringValue]; NSLog(@"scan result:%@",stringValue); [self dismissViewControllerAnimated:NO completion:nil]; } else { NSLog(@"no scan result"); [self dismissViewControllerAnimated:NO completion:nil]; return; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (BOOL)shouldAutorotate { return YES;//支持转屏 } //------------------------------------------------------------------------------- - (BOOL)prefersStatusBarHidden { return YES;//隐藏为YES,显示为NO } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } @end