ios二维码扫描

时间:2023-12-18 10:11:26

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