ios實現二維碼掃描

來源:互聯網
上載者:User

標籤:

1 包含

#import <AVFoundation/AVFoundation.h>

2 匯入AVFoundation庫檔案

 1 #import "ViewController.h" 2 #import <AVFoundation/AVFoundation.h> 3 @interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate> 4 - (IBAction)scan:(UIButton *)sender; 5 @property (strong,nonatomic)AVCaptureDevice *device; 6 @property (strong,nonatomic)AVCaptureDeviceInput *input; 7 @property (strong,nonatomic)AVCaptureMetadataOutput *output; 8 @property (strong,nonatomic)AVCaptureSession *session; 9 @property (strong,nonatomic)AVCaptureVideoPreviewLayer *preview;10 @end11 12 @implementation ViewController13 14 - (void)viewDidLoad {15     [super viewDidLoad];16     // Do any additional setup after loading the view, typically from a nib.17     18 }19 20 - (void)setupCamera21 {22     // Device23     _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];24     25     // Input26     _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];27     28     // Output29     _output = [[AVCaptureMetadataOutput alloc]init];30     [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];31     32     // Session33     _session = [[AVCaptureSession alloc]init];34     [_session setSessionPreset:AVCaptureSessionPresetHigh];35     if ([_session canAddInput:self.input])36     {37         [_session addInput:self.input];38     }39     40     if ([_session canAddOutput:self.output])41     {42         [_session addOutput:self.output];43     }44     45     // 條碼類型 AVMetadataObjectTypeQRCode46     _output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];47     48     // Preview49     _preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session];50     _preview.videoGravity = AVLayerVideoGravityResizeAspectFill;51     _preview.frame =CGRectMake(20,110,280,280);52     [self.view.layer insertSublayer:self.preview atIndex:0];53     54     55     56     // Start57     [_session startRunning];58 }59 #pragma mark AVCaptureMetadataOutputObjectsDelegate60 - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection61 {62     NSString *stringValue = [[NSString alloc] init];63     64     if (metadataObjects.count >0) {65         AVMetadataMachineReadableCodeObject *metadataObject = [metadataObjects objectAtIndex:0];66         stringValue = metadataObject.stringValue;67     }68     69     [_session stopRunning];70     NSLog(@"%@",stringValue);71     [self dismissViewControllerAnimated:YES completion:^{72         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil73                                                       message:stringValue74                                                      delegate:nil75                                             cancelButtonTitle:@"OK"76                                             otherButtonTitles:nil,nil];77         [alert show];78     }];79 }80 - (IBAction)scan:(UIButton *)sender {81     [self setupCamera];82 }

有待完善。。。。。

ios實現二維碼掃描

聯繫我們

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