淺談iOS7 AVFoundation 二維碼掃描

來源:互聯網
上載者:User

標籤:blog   code   tar   c   http   int   

 iOS7,AVFoundation中現在已經內建支援一維和二維碼的掃瞄,iOS6及之前的想要掃瞄二維碼,還是需要添加第三方庫ZXing和ZBar。

ZBar產生二維碼:http://blog.csdn.net/cafei111/article/details/8924297

先添加AVFoundation.framework


#import <AVFoundation/AVFoundation.h>

@interface QRcodeViewController :UIViewController<AVCaptureMetadataOutputObjectsDelegate>

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

@end


- (void)setupCamera
{
    // Device
    self.device = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];
    
    // Input
    self.input = [AVCaptureDeviceInputdeviceInputWithDevice:self.deviceerror:nil];
    
    // Output
    self.output = [[AVCaptureMetadataOutputalloc]init];
    [self.outputsetMetadataObjectsDelegate:selfqueue:dispatch_get_main_queue()];
    
    // Session
    self.session = [[AVCaptureSessionalloc]init];
   [self.sessionsetSessionPreset:AVCaptureSessionPresetHigh];
   if ([self.sessioncanAddInput:self.input])
    {
        [self.sessionaddInput:self.input];
    }
   if ([self.sessioncanAddOutput:self.output])
    {
        [self.sessionaddOutput:self.output];
    }
    
    // 條碼類型
    self.output.metadataObjectTypes [email protected][AVMetadataObjectTypeQRCode];
    
    // Preview
    self.preview = [AVCaptureVideoPreviewLayerlayerWithSession:self.session];
    self.preview.videoGravity =AVLayerVideoGravityResizeAspectFill;
    self.preview.frame =CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height);
   [self.view.layeraddSublayer:self.preview];
    
    // Start
    [self.sessionstartRunning];
}

條碼類型有如下幾種:
1
2
3
4
5
6
7
8
9
10
AVMetadataObjectTypeUPCECode
AVMetadataObjectTypeCode39Code
AVMetadataObjectTypeCode39Mod43Code
AVMetadataObjectTypeEAN13Code
AVMetadataObjectTypeEAN8Code
AVMetadataObjectTypeCode93Code
AVMetadataObjectTypeCode128Code
AVMetadataObjectTypePDF417Code
AVMetadataObjectTypeQRCode
AVMetadataObjectTypeAztecCode

掃瞄到二維碼之後,會調用delegate

#pragma mark AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
   NSString *stringValue;
    
   if ([metadataObjectscount] >0) {
       AVMetadataMachineReadableCodeObject *metadataObject = [metadataObjectsobjectAtIndex:0];
        stringValue = metadataObject.stringValue;
    }
    
    [_sessionstopRunning];
    
    [selfdismissViewControllerAnimated:YEScompletion:^{
       UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:nil
                                                       message:stringValue
                                                      delegate:nil
                                             cancelButtonTitle:@"OK"
                                             otherButtonTitles:nil,nil];
        [alertshow];
    }];
}


這個委託方法裡面的字串stringValue就是二維碼的內容

聯繫我們

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