IOS --自帶二維碼掃描

具體的能夠參考這篇文章:http://www.appcoda.com/qr-code-ios-programming-tutorial/ios

      最近因爲公司項目須要,我負責二維碼的生成和掃描部分。因爲蘋果規定自2015/02/01後上架的做品必須支持64位,因此通過綜合比較後,二維碼的掃描部分我決定採用蘋果自帶的AVFoundation框架來實現掃描,網上有不少關於ZBar和Zxing的例子,有興趣的朋友能夠去研究下。session

      1.使用前先加入框架頭文件和代理信息,以下:app

#import <AVFoundation/AVFoundation.h>
@interface QRCodeViewController : UIViewController<AVCaptureMetadataOutputObjectsDelegate>

      2.用到以下幾個類框架

@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;

     3.具體用法以下ide

    //Device
    device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
     //Input
    input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
     //判斷是否有輸入
    if (!input)
    {
        NSLog(@"error info:%@",[error localizedDescription]);
        return NO;
    }
     //Session
    session = [[AVCaptureSession alloc] init];
    [session setSessionPreset:AVCaptureSessionPresetHigh];
    [session addInput:input];
     //Output
    output = [[AVCaptureMetadataOutput alloc] init];
    [session addOutput:output];
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    //條碼類型
    [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
     //preview,掃描區域
    preview = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    [preview setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    //設置掃描區域
    [preview setFrame:frameView.layer.bounds];
    //將掃描view放在self.frameView上
    [self.frameView.layer addSublayer:preview];
    //Start
    [session startRunning];

      4.掃描到二維碼後的操做,掃描到二維碼以後就會調用以下方法atom

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    NSString *stringValue;
    if (metadataObjects !=nil && [metadataObjects count] >0)
    {
        AVMetadataMachineReadableCodeObject *metadataObject = [metadataObjects objectAtIndex:0];
        //判斷取到的對象類型
        if ([[metadataObject type] isEqualToString:AVMetadataObjectTypeQRCode])
        {
            [scanResult performSelectorOnMainThread:@selector(setText:) withObject:[metadataObject stringValue] waitUntilDone:NO];
        stringValue = [metadataObject stringValue];
        NSLog(@"掃描結果:%@",stringValue);
        
//        //直接跳往處理二維碼controller
//        ProcessRQViewController *processRQViewController = [[ProcessRQViewController alloc] init];
//        //將取到的二維碼的值傳過去
//        processRQViewController.RQResult = stringValue;
//        //[self.navigationController pushViewController:processRQViewController animated:true];
//        [self presentViewController:processRQViewController animated:YES completion:NULL];
        
//        //直接跳往下載圖片的頁面
//        ProcessImageViewController *processImageViewController = [[ProcessImageViewController alloc] init];
//        //將取到的二維碼的值傳過去
//        processImageViewController.ImageResult = stringValue;
//        //跳轉
//        [self presentViewController:processImageViewController animated:YES completion:NULL];
}

      5.至此結束spa

       本人親測,好多二維碼均可以掃描出來。可是在2月13號這天,項目經理讓我用這個app掃描本公司營業執照二維碼的時候,只能掃出營業執照前面的數字(本公司的營業執照:數字(一連串)+空一格+漢字(好幾個)+空一格+一個鏈接地址)。找出bug後來不及修改,就直接過年了,現在過年來了,要及時堵住這個bug了。代理

相關文章
相關標籤/搜索