你的位置:首页 > 操作系统

[操作系统]iOS 二维码扫描


// 导入 AVFoundation.framwork 框架
#import "HDCodeViewController.h"#import "HDNormalViewController.h"#import <AVFoundation/AVFoundation.h>@interface HDCodeViewController ()<AVCaptureMetadataOutputObjectsDelegate>@property(strong,nonatomic)UIView *codeView;@property(strong,nonatomic)UIImageView *myLine;@end@implementation HDCodeViewController{ AVCaptureSession *_session; NSString *_theString; NSTimer *_timer; BOOL _upOrdown; NSInteger _num;}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self initUI]; [self prepareCamara]; [self initData];}-(void)dealloc{ [_timer invalidate]; _timer = nil; _session = nil;}-(void)initData{ _num = 0;}#pragma mark - UI Stuff Methods-(void)initUI{ self.codeView = [[UIView alloc]initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, SCREEN_HEIGHT - 44)]; self.codeView.backgroundColor = [UIColor clearColor]; [self.view addSubview:self.codeView]; UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-300)/2, 20, 300, 300)]; imageView.image = [UIImage imageNamed:@"code_square"]; [self.codeView addSubview:imageView]; self.myLine = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-300)/2, 20, 300, 2)]; self.myLine.contentMode = UIViewContentModeScaleToFill; self.myLine.image = [UIImage imageNamed:@"code_movingBar"]; [self.codeView addSubview:self.myLine]; _timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(moveLine) userInfo:nil repeats:YES];}-(void)moveLine{ CGFloat height = self.myLine.frame.origin.y; CGFloat width = self.myLine.frame.size.width; if (_upOrdown == NO) { _num ++; self.myLine.frame = CGRectMake((SCREEN_WIDTH-300)/2, height+2, width, 2); if (2*_num == 300) { _upOrdown = YES; } } else { _num --; self.myLine.frame = CGRectMake((SCREEN_WIDTH-300)/2, height-2, width, 2); if (_num == 0) { _upOrdown = NO; } }}#pragma Camara Stuff Methods-(void)prepareCamara{ AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc]init]; [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; _session = [[AVCaptureSession alloc]init]; [_session setSessionPreset:AVCaptureSessionPresetHigh]; [_session addInput:input]; [_session addOutput:output]; output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code]; AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:_session]; layer.videoGravity = AVLayerVideoGravityResizeAspectFill; layer.frame = CGRectMake((SCREEN_WIDTH-300)/2, 20, 300, 300); [self.codeView.layer insertSublayer:layer atIndex:0]; [_session startRunning];}-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{ if (metadataObjects.count>0) { //[session stopRunning]; AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ]; _theString = metadataObject.stringValue; NSLog(@"%@",_theString);//扫出来的字符串 }}@end