1.添加AVFoundation.framework框架

2,控制器中实现

//第一步添加AVFoundation.framework框架

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

#import "RYPreView.h"

@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>

//定义输入设备,用户采集信息,

@property(nonatomic,strong)AVCaptureDeviceInput*input;

//定义输出设备,处理采集的信息,获得数据

@property(nonatomic,strong)AVCaptureMetadataOutput*outPut;

//定义连接输入输出设备的会话

@property(nonatomic,strong)AVCaptureSession*session;

//定义展示信息的图层

@property(nonatomic,strong)RYPreView*preview;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//1,定义输入设备,摄像头

AVCaptureDevice *device=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

self.input=[AVCaptureDeviceInput deviceInputWithDevice:device error:NULL];

//2.输出设备

self.outPut=[[AVCaptureMetadataOutput alloc]init];

//3.定义session会话

self.session=[[AVCaptureSession alloc]init];

//展示大小

[self.session setSessionPreset:AVCaptureSessionPreset640x480];

//添加输入输出设备

if([self.session canAddInput:self.input])

{

[self.session addInput:self.input];

}

if ([self.session canAddOutput:self.outPut]) {

[self.session addOutput:self.outPut];

}

//指定输出设备代理

[self.outPut setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

//设置元数据类型 二维码

[self.outPut setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];

//需要一个特殊的layer来处理展示的数据 AVCaptureVideoPreviewLayer

//方法一可以再这里创建并设置大小

//方法二

RYPreView *preview=[[RYPreView alloc]initWithFrame:self.view.bounds];

preview.session=self.session;

[self.view addSubview:preview];

//开启会话

[self.session startRunning];

}

//解析元数据调用

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection

{

//停止会话

[self.session stopRunning];

//移除视图layer

[self.preview removeFromSuperview];

//获取信息

for (AVMetadataMachineReadableCodeObject* objc in metadataObjects) {

NSLog(@"%@",objc.stringValue);

}

}

@end

#import <UIKit/UIKit.h>

#import <AVFoundation/AVFoundation.h>

@interface RYPreView : UIView

//定义session,实现session与当前layer绑定

@property(nonatomic,strong)AVCaptureSession*session;

@end

#import "RYPreView.h"

@interface RYPreView()

@property(nonatomic,strong)UIImageView*imageCover;

@property(nonatomic,strong)UIImageView*lineImage;//

@property(nonatomic,strong)NSTimer*timer;//动画时间间距

@end

@implementation RYPreView

//自定义需要layer的类型 session 需要绑定的layer类型为AVCaptureVideoPreviewLayer

+(Class)layerClass

{

return [AVCaptureVideoPreviewLayer class];

}

//重写set方法

-(void)setSession:(AVCaptureSession *)session

{

_session=session;

AVCaptureVideoPreviewLayer *layer=(AVCaptureVideoPreviewLayer*)self.layer;

layer.session=session;

}

-(instancetype)initWithFrame:(CGRect)frame

{

if (self=[super initWithFrame:frame]) {

[self addSubviewWithFrame];

}

return self;

}

//初始化自定义控件

-(void)addSubviewWithFrame

{

//设置背景图片

self.imageCover = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pick_bg.png"]];

//设置位置到界面的中间

self.imageCover.frame = CGRectMake(self.bounds.size.width * 0.5 - 140, self.bounds.size.height * 0.5 - 140, 280, 280);

//添加到视图上

[self addSubview:self.imageCover];

//初始化二维码的扫描线的位置

self.lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(30, 10, 220, 2)];

self.lineImage.image = [UIImage imageNamed:@"line.png"];

[self.imageCover addSubview:self.lineImage];

//开启定时器

_timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(animation) userInfo:nil repeats:YES];

}

- (void)animation

{

[UIView animateWithDuration:2.8 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{

self.lineImage.frame = CGRectMake(30, 260, 220, 2);

} completion:^(BOOL finished) {

self.lineImage.frame = CGRectMake(30, 10, 220, 2);

}];

}

@end

ios二维码扫描的更多相关文章

  1. Ios二维码扫描(系统自带的二维码扫描)

    Ios二维码扫描 这里给大家介绍的时如何使用系统自带的二维码扫描方法和一些简单的动画! 操作步骤: 1).首先你需要搭建UI界面如图:下图我用了俩个imageview和一个label 2).你需要在你 ...

  2. iOS二维码扫描IOS7系统实现

    扫描相关类 二维码扫描需要获取摄像头并读取照片信息,因此我们需要导入系统的AVFoundation框架,创建视频会话.我们需要用到一下几个类: AVCaptureSession 会话对象.此类作为硬件 ...

  3. iOS - 二维码扫描和应用跳转

    序言 前面我们已经调到过怎么制作二维码,在我们能够生成二维码之后,如何对二维码进行扫描呢? 在iOS7之前,大部分应用中使用的二维码扫描是第三方的扫描框架,例如ZXing或者ZBar.使用时集成麻烦, ...

  4. ios二维码扫描插件,适配当前主流扫描软件,自定义扫描界面。

            二维码介绍: 二维码(QR(Quick Response)code),又称二维条码,最早起源于日本. 它是用特定的几何图形按一定规律在平面(二维方向)上分布的黑白相间的图形,是所有信息 ...

  5. iOS二维码扫描的实现(Swift)

    随着二维码的普遍使用,二维码扫描也成为了很多app的一个基本功能,本篇主要来介绍一下如何实现一个简单的二维码扫描功能.本文使用了XCode自带的AVFoundation 库,利用Swfit语言实现. ...

  6. iOS 二维码扫描 通过ZBar ZXing等第三方库

    扫描二维码的开源库有很多如 ZBar.ZXing等 ZBar的使用方法: 下载ZBar SDK 地址https://github.com/bmorton/ZBarSDK ZBarSDK是一个开源的SD ...

  7. iOS 二维码扫描

    // 导入 AVFoundation.framwork 框架#import "HDCodeViewController.h" #import "HDNormalViewC ...

  8. 有关iOS系统中调用相机设备实现二维码扫描功能的注意点(3/3)

    今天我们接着聊聊iOS系统实现二维码扫描的其他注意点. 大家还记得前面我们用到的输出数据的类对象吗?AVCaptureMetadataOutput,就是它!如果我们需要实现目前主流APP扫描二维码的功 ...

  9. iOS开发-二维码扫描和应用跳转

    iOS开发-二维码扫描和应用跳转   序言 前面我们已经调到过怎么制作二维码,在我们能够生成二维码之后,如何对二维码进行扫描呢? 在iOS7之前,大部分应用中使用的二维码扫描是第三方的扫描框架,例如Z ...

随机推荐

  1. Nginx如何设置拒绝或允许指定ip访问

    location ~ /druid/ { #deny 192.168.1.1; allow 192.168.1.1; deny all; proxy_pass http://127.0.0.1:808 ...

  2. 1.openstack之mitaka搭建基本配置

    介绍:本次案列为基本的三节点部署 注:搭建所需要的包可以联系QQ:22102107获取 一:网络: 1.管理网络:192.168.11.0/24 2.数据网络:1.1.1.0/24 注:每个节点两个网 ...

  3. Lake Counting_深度搜索_递归

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30414   Accepted: 15195 D ...

  4. [C#]Datatable和json互相转换操作

    #region DataTable 转换为Json字符串实例方法 /// <summary> /// GetClassTypeJosn 的摘要说明 /// </summary> ...

  5. CocoaPods安装教程

    Code4App 原创文章.转载请注明出处:http://code4app.com/article/cocoapods-install-usage CocoaPods是什么? 当你开发iOS应 用时, ...

  6. iOS蓝牙4.0

    iOS的蓝牙用到了  CoreBluetooth 框架 首先导入框架 #import <CoreBluetooth/CoreBluetooth.h> 我们需要一个管理者来管理蓝牙设备,CB ...

  7. 6. ZigZag Conversion

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  8. 关于Windows下的文件后缀名问题

    一.背景说明 有很多的小伙伴对windows下的文件后缀名不能很好地理解作用和区别,更不用说高深的使用了,在这里给大家说一下这些文件后缀名到底有什么区别,有什么作用呢? 二.说明 简单的说来,wind ...

  9. Xcode常用代码块

    Xcode的代码片段(Code Snippets)创建自定义的代码片段,当你重用这些代码片段时,会给你带来很大的方便. 常用的: 1.strong:@property (nonatomic,stron ...

  10. APP消息推送:通知和透传

    目前市场上的消息推送方式有两种:通知和透传.什么是透传?透传即是透明传送,即传送网络无论传输业务如何,只负责将需要传送的业务传送到目的节点,同时保证传输的质量即可,而不对传输的业务进行处理.透传消息, ...