Chinaunix

标题: iOS 原生态API实现二维码的扫描功能 [打印本页]

作者: wyfem    时间: 2015-07-30 13:05
标题: iOS 原生态API实现二维码的扫描功能
本帖最后由 wyfem 于 2015-07-30 13:06 编辑

AVCaptureSession *session = [[AVCaptureSession alloc] init];
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error = nil;
   
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device
                                                                        error:&error];
    if (input) {
        [session addInput:input];
    } else {
        NSLog(@"Error: %@", error);
    }
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    //设置扫码支持的编码格式(如下设置条形码和二维码兼容)
    [output setMetadataObjectTypes[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code]];
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    [session addOutpututput];
    [session startRunning];

[Objective-C]代码
  1. //
  2. //  ViewController.m
  3. //  QRCode
  4. //
  5. //  Created by chenchen on 15/7/30.
  6. //  Copyright (c) 2015年 BSY. All rights reserved.
  7. //
  8. #import <AVFoundation/AVFoundation.h>
  9. #import "ViewController.h"
  10. @interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>

  11. @end

  12. @implementation ViewController

  13. - (void)viewDidLoad {
  14.     [super viewDidLoad];
  15.    
  16.      
  17.      
  18.     AVCaptureSession *session = [[AVCaptureSession alloc] init];
  19.     AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  20.     NSError *error = nil;
  21.      
  22.     AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device
  23.                                                                         error:&error];
  24.     if (input) {
  25.         [session addInput:input];
  26.     } else {
  27.         NSLog(@"Error: %@", error);
  28.     }
  29.     AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
  30.     //设置扫码支持的编码格式(如下设置条形码和二维码兼容)
  31.     [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code]];
  32.     [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
  33.     [session addOutput:output];
  34.     [session startRunning];
  35. }

  36. #pragma mark - AVCaptureMetadataOutputObjectsDelegate
  37. - (void)captureOutput:(AVCaptureOutput *)captureOutput
  38. didOutputMetadataObjects:(NSArray *)metadataObjects
  39.        fromConnection:(AVCaptureConnection *)connection
  40. {
  41.     NSString *QRCode = nil;
  42.     for (AVMetadataObject *metadata in metadataObjects) {
  43.         if ([metadata.type isEqualToString:AVMetadataObjectTypeQRCode]) {
  44.             // This will never happen; nobody has ever scanned a QR code... ever
  45.             QRCode = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
  46.             break;
  47.         }
  48.     }
  49.      
  50.     NSLog(@"QR Code: %@", QRCode);
  51. }
  52. - (void)didReceiveMemoryWarning {
  53.     [super didReceiveMemoryWarning];
  54.     // Dispose of any resources that can be recreated.
  55. }

  56. @end
复制代码

作者: renxiao2003    时间: 2015-08-11 15:38
我做Android的二维码扫描难死了。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2