iOS设置和获取条形码
2017-01-18 14:13
239 查看
// // Copyright © 2016年 hjl. All rights reserved. // #import "HHBarcodeViewController.h" #import <AVFoundation/AVFoundation.h> @interface HHBarcodeViewController () /** 会话 */ @property (nonatomic, strong) AVCaptureSession *session; //* 预览图层 */ @property (nonatomic, weak) AVCaptureVideoPreviewLayer *previewLayer; @end @implementation HHBarcodeViewController - (void)viewDidLoad { [super viewDidLoad]; // 条形码页面动画 CADisplayLink *disp = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)]; [disp addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; // 1. 实例化拍摄设备 AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; // 2. 设置输入设备 AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; // 3. 实例化拍摄元数据输出 AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init]; // 4. 设置输出数据代理 [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; // 5. 添加拍摄会话 AVCaptureSession *session = [[AVCaptureSession alloc] init]; self.session = session; // 6. 添加会话输入/输出 if ([session canAddInput:input]) { [session addInput:input]; } if ([session canAddOutput:output]) { [session addOutput:output]; } //7 设置输出数据类型,需要将元数据输出添加到会话后,才能指定元数据类型,否则会报错 [output setMetadataObjectTypes:@[ AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeAztecCode, AVMetadataObjectTypeInterleaved2of5Code, AVMetadataObjectTypeITF14Code, AVMetadataObjectTypeDataMatrixCode, ]]; //8 实例化预览图层 AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session]; previewLayer.frame = self.view.bounds; self.previewLayer = previewLayer; //previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; //9 将图层插入当前视图 [self.view.layer insertSublayer:previewLayer atIndex:0]; //10 启动会话 [_session startRunning]; } #pragma 代理 - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{ //1 判断是否获取到数据 if (metadataObjects.count > 0) { //2 停止刷新 [self.session stopRunning]; //3 移除预览图层 [self.previewLayer removeFromSuperlayer]; //4 读取数据 AVMetadataMachineReadableCodeObject *obj = [metadataObjects lastObject]; HHLog(@"%@", obj.stringValue); } } - (void)update{ _barcodeConsY.constant += 3; if(_barcodeConsY.constant >= 224){ _barcodeConsY.constant = 0; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- iOS 获取屏幕尺寸 设置背景图片
- IOS获取preferreces偏好设置plistname名称的方法
- iOS 系统设置隐私中常用的功能权限获取
- iOS 高德地图设置中心点,获取中心点的详细位置和经纬度
- iOS设置和获取二维码扫描
- 获取ios设备设置的国家和语言
- iOS中获取相册、相机、定位、以及麦克风权限设置
- ios获取和设置系统音量
- iOS系统的一些单例类 获取及全局应用全局样式的设置的获取
- iOS中的地理位置的获取及plist设置
- IOS 从系统图库中获取 图片 并设置为头像
- iOS中获取当前时间和日期并设置格式
- ios在应用内获取到设置里当前应用是否可以接受推送
- iOS开发swift如何调用系统相册和相机获取图片设置用户头像
- IOS7下隐私设置中获取各种硬件是否被授权使用
- IOS获取设置的语言和国家以及设备的名称版本
- IOS 平台获取当前的国家/语言设置
- iOS 模拟器 获取位置 设置自定义位置