iOS 二維碼掃描,ios掃描

來源:互聯網
上載者:User

iOS 二維碼掃描,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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.