ios7新增api實現掃描二維碼

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   os   for   sp   

本來用的ZBar開源庫實現的掃描二維碼,但是貌似不支援arm64了,也沒有在更新。

現在不用適配ios7以下,而iOS新增系統API已支援掃碼,參考老外的一篇部落格做了個demo,需要的可以參考下

參考部落格:http://www.appcoda.com/qr-code-ios-programming-tutorial/


#import <AVFoundation/AVFoundation.h>@interface QRCodeReadController : BaseViewController<AVCaptureMetadataOutputObjectsDelegate>@property (weak, nonatomic) IBOutlet UIView *viewPreview;@end
在xib上加一個viewPreview,用來掃碼時動態顯示擷取到的網路攝影機的內容

@interface QRCodeReadController (){    NSInteger maxY;    NSInteger minY;    NSTimer *timer;        UIImageView *line;}@property (nonatomic) BOOL isReading;@property (nonatomic, strong) AVCaptureSession *captureSession;@property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;-(BOOL)startReading;-(void)stopReading;@end@implementation QRCodeReadController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view from its nib.    _isReading = NO;    if ([self startReading]) {        maxY = 280;        minY = 2;        line =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 280, 10)];  // 0 -200        [line setImage:[UIImage imageNamed:@"e0"]];        [_viewPreview addSubview:line];                timer = [NSTimer scheduledTimerWithTimeInterval:1.0/40 target:self selector:@selector(move) userInfo:nil repeats:YES];    };    }/* * * AVCaptureMetadataOutput object. This class in combination with the AVCaptureMetadataOutputObjectsDelegate protocol will manage to intercept any metadata found in the input device (meaning data in a QR code captured by our camera) and translate it to a human readable format. */- (BOOL)startReading {    NSError *error;        AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];        if (!input) {        NSLog(@"%@", [error localizedDescription]);        return NO;    }    _captureSession = [[AVCaptureSession alloc] init];    [_captureSession addInput:input];        AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];    [_captureSession addOutput:captureMetadataOutput];            dispatch_queue_t dispatchQueue;    dispatchQueue = dispatch_queue_create("myQueue", NULL);    [captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];    [captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];            //show to user what the camera of the device sees  using a AVCaptureVideoPreviewLayer    _videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];    [_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];    [_videoPreviewLayer setFrame:_viewPreview.layer.bounds];    [_viewPreview.layer addSublayer:_videoPreviewLayer];        [_captureSession startRunning];    return YES;}-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{    if (metadataObjects != nil && [metadataObjects count] > 0) {        AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];        if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {            [self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];            NSLog(@"metadataObj  string = %@",[metadataObj stringValue]);            _isReading = NO;        }    }}-(void)stopReading{    [_captureSession stopRunning];    _captureSession = nil;        [_videoPreviewLayer removeFromSuperlayer];}// 掃描時,移動掃描線-(void)move{    NSLog(@"+++");    static BOOL flag = TRUE;  // true down  and false up    if (flag) {        if (line.frame.origin.y <maxY) {            line.frame = CGRectMake(                                    line.frame.origin.x, line.frame.origin.y +5,                                    line.frame.size.width, line.frame.size.height                                    );        }else{            flag = !flag;            if (_isReading&&[timer isValid]) {                [timer invalidate];            }        }    }else    {        if (line.frame.origin.y >minY) {            line.frame = CGRectMake(                                    line.frame.origin.x, line.frame.origin.y -5,                                    line.frame.size.width, line.frame.size.height                                    );        }else        {            flag = !flag;        }    }        }





ios7新增api實現掃描二維碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.