免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1692 | 回复: 1
打印 上一主题 下一主题

[iOS] iOS 原生态API实现二维码的扫描功能 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-30 13:05 |只看该作者 |倒序浏览
本帖最后由 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
复制代码

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
2 [报告]
发表于 2015-08-11 15:38 |只看该作者
我做Android的二维码扫描难死了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP