iOS源生二维码扫描,你想象不到的强大
来源:互联网 发布:ubuntu安装yum 编辑:程序博客网 时间:2024/05/21 17:17
还在用ZBar、ZXing等第三方库来实现二维码扫描吗?那就OUT了。来看下iOS源生的二维码扫描,【测试需真机】
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>//需要添加AVFoundation系统库
@interface ViewController ()<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
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"iOS原生扫描二维码" forState:UIControlStateNormal];
button.frame = CGRectMake(50, 100, self.view.bounds.size.width - 100, 50);
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)buttonClick{
// Device 实例化设备
_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// Input 设备输入
_input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
// Output 设备的输出
_output = [[AVCaptureMetadataOutput alloc]init];
[_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
// Session
_session = [[AVCaptureSession alloc]init];
[_session setSessionPreset:AVCaptureSessionPresetHigh];
if ([_session canAddInput:self.input])
{
[_session addInput:self.input];
}
if ([_session canAddOutput:self.output])
{
[_session addOutput:self.output];
}
// 条码类型 AVMetadataObjectTypeQRCode
_output.metadataObjectTypes =@[AVMetadataObjectTypeCode128Code,AVMetadataObjectTypeUPCECode,AVMetadataObjectTypeCode39Code,AVMetadataObjectTypeCode39Mod43Code,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeCode93Code,AVMetadataObjectTypePDF417Code,AVMetadataObjectTypeQRCode,AVMetadataObjectTypeAztecCode,AVMetadataObjectTypeInterleaved2of5Code,AVMetadataObjectTypeITF14Code,AVMetadataObjectTypeDataMatrixCode];
// Preview 扫描窗口设置
_preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session];
_preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
_preview.frame = CGRectMake(20,150,self.view.bounds.size.width - 40,300);
[self.view.layer insertSublayer:self.preview atIndex:0];
// Start 开始扫描
[_session startRunning];
}
//解析结果的代理方法
#pragma mark AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
//得到解析到的结果
NSString *stringValue;
if ([metadataObjects count] > 0)
{
AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
stringValue = metadataObject.stringValue;
}
[_session stopRunning];//停止扫描
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"结果:%@",stringValue] delegate:self cancelButtonTitle:nil otherButtonTitles:@"知道了",@"重新扫描", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
{
[self dismissViewControllerAnimated:YES completion:^
{
//
}];
}
else
{
//重新扫描
[_session startRunning];
}
}
@end
- iOS源生二维码扫描,你想象不到的强大
- 【木头Cocos2d-x 025】状态机篇(第04章) --事件驱动,你想象不到的强大
- 【木头Cocos2d-x 025】状态机篇(第04章) --事件驱动,你想象不到的强大
- 你想象不到的SQL语句?
- 比你想象中还要强大的枚举类型
- iOS的二维码扫描
- 二维码的扫描--iOS
- iOS源生二维码扫描及生成
- iOS:源生二维码扫描和制作
- iOS系统的二维码扫描
- 患癌父亲为省2元不吸氧:贫穷是你想象不到的绝望
- 比你想象中还要强大的枚举类型---案例篇
- iOS-AVFoundation二维码扫描,二维码扫描,图片的无损放大
- 连杆系统:比你想象中的更强大
- ios二维码的生成与扫描
- IOS之二维码的制作与扫描
- IOS之二维码的制作与扫描
- IOS之二维码的制作与扫描
- Hadoop namenode无法启动常见解决办法
- Discuz 3.2 注册后跳转网页
- Codeforces Round #195 (Div. 2) C--Vasily the Bear and Sequence(贪心)
- 【博客分享】优秀的有趣的博客
- Android学习资料
- iOS源生二维码扫描,你想象不到的强大
- UDT协议——03版原文
- [设计模式]两种方法实现简单工厂模式
- iOS-应用之间调用
- java中图片压缩处理(一)
- 头文件中添加了防止重复包含的宏,依旧报重复定义的问题
- js回调函数的理解总结
- 程序员常用工具软件
- swift-Optional