简单的二维码扫描封装,要在真机上试才不会崩溃

//扫描二维码,条形码

#import <UIKit/UIKit.h>

@protocol AvCapViewDegelate <NSObject>

-(void)qrCode:(NSString *)code;

@end

@interface AVCapView : UIView

@property (nonatomic,strong)id<AvCapViewDegelate>degalate;

@end


#define SCREEN_CONTENT_HEIGHT 300

#define SCANVIEW_EdgeTop 100.0

#define SCANVIEW_EdgeLeft 50.0

#define TINTCOLOR_ALPHA 0.2 //浅色透明度

#define DARKCOLOR_ALPHA 0.5 //深色透明度

#define VIEW_WIDTH [UIScreen mainScreen].bounds.size.width

#define VIEW_HEIGHT [UIScreen mainScreen].bounds.size.height

#import "AVCapView.h"

#import <AVFoundation/AVFoundation.h>

@interface AVCapView ()<AVCaptureMetadataOutputObjectsDelegate>

{

    AVCaptureSession * session;//输入输出的中间桥梁

    UIView *AVCapView;// view 用来放置扫描框、取消按钮、说明 label

    UIView *_QrCodeline;//上下移动绿色的线条

    NSTimer *_timer;

}

@end

@implementation AVCapView

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

       

        self.backgroundColor = [UIColor colorWithRed:54.f/255 green:53.f/255 blue:58.f/255 alpha:1];

        

        UIButton *cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(15, VIEW_HEIGHT - 100, 50, 25)];

        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 318, 280, 60)];

        label.numberOfLines = 0;

        label.text = @"小提示:将条形码或二维码对准上方区域中心即可";

        label.textColor = [UIColor grayColor];

        [cancelBtn setTitle:@"取消" forState: UIControlStateNormal];

        [cancelBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

        [cancelBtn addTarget:self action:@selector(touchAVCancelBtn) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:label];

        [self addSubview:cancelBtn];

        //画上边框

        UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop, VIEW_WIDTH- 2 * SCANVIEW_EdgeLeft, 1)];

        topView.backgroundColor = [UIColor whiteColor];

        [self addSubview:topView];

        

        //画左边框

        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop , 1,VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft )];

        leftView.backgroundColor = [UIColor whiteColor];

        [self addSubview:leftView];

        

        //画右边框

        UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(SCANVIEW_EdgeLeft + VIEW_WIDTH- 2 * SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop , 1,VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft + 1)];

        rightView.backgroundColor = [UIColor whiteColor];

        [self addSubview:rightView];

        

        //画下边框

        UIView *downView = [[UIView alloc] initWithFrame:CGRectMake(SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop + VIEW_WIDTH- 2 * SCANVIEW_EdgeLeft,VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft ,1 )];

        downView.backgroundColor = [UIColor whiteColor];

        [self addSubview:downView];

        

        

        //画中间的基准线

        _QrCodeline = [[UIView alloc] initWithFrame:CGRectMake(SCANVIEW_EdgeLeft + 1, SCANVIEW_EdgeTop, VIEW_WIDTH- 2 * SCANVIEW_EdgeLeft - 1, 2)];

        _QrCodeline.backgroundColor = [UIColor greenColor];

        [self addSubview:_QrCodeline];

        

        

        // 先让基准线运动一次,避免定时器的时差

        [UIView animateWithDuration:1.2 animations:^{

            

            _QrCodeline.frame = CGRectMake(SCANVIEW_EdgeLeft + 1, VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft + SCANVIEW_EdgeTop , VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft - 1, 2);

            

        }];

        

        [self performSelector:@selector(createTimer) withObject:nil afterDelay:0.4];

        

        

        AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

        //创建输入流

        AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];

        //创建输出流

        AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init];

        //设置代理 在主线程里刷新

        [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

        

        //初始化链接对象

        session = [[AVCaptureSession alloc]init];

        //高质量采集率

        [session setSessionPreset:AVCaptureSessionPresetHigh];

        

        [session addInput:input];

        [session addOutput:output];

        //设置扫码支持的编码格式(如下设置条形码和二维码兼容)

        output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];

        

        AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:session];

        layer.videoGravity=AVLayerVideoGravityResizeAspectFill;

        layer.frame = CGRectMake(SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop, VIEW_WIDTH- 2 * SCANVIEW_EdgeLeft, 220);

        [self.layer insertSublayer:layer atIndex:0];

        //开始捕获

        [session startRunning];

        

        

        

        

        

    }

    return self;

}

- (void)createTimer

{

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

}


- (void)stopTimer

{

    if ([_timer isValid] == YES) {

        [_timer invalidate];

        _timer = nil;

    }

    

}


// 滚来滚去 :D :D :D

- (void)moveUpAndDownLine

{

    CGFloat YY = _QrCodeline.frame.origin.y;

    if (YY != VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft + SCANVIEW_EdgeTop ) {

        [UIView animateWithDuration:1.2 animations:^{

            _QrCodeline.frame = CGRectMake(SCANVIEW_EdgeLeft + 1, VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft + SCANVIEW_EdgeTop , VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft - 1,2);

        }];

    }else {

        [UIView animateWithDuration:1.2 animations:^{

            _QrCodeline.frame = CGRectMake(SCANVIEW_EdgeLeft + 1, SCANVIEW_EdgeTop, VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft - 1,2);

        }];

    }

}

#pragma mark AVCaptureMetadataOutputObjectsDelegate

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

    if (metadataObjects.count>0) {

        //[session stopRunning];

        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];

        //输出扫描字符串

         WZPLog(@"%@",metadataObject.stringValue);

        if ([_degalate respondsToSelector:@selector(qrCode:)]) {

            [_degalate qrCode:metadataObject.stringValue];

        }

        [session stopRunning];

        [self stopTimer];

        [self removeFromSuperview];

    }

}

- (void)touchAVCancelBtn{

    [session stopRunning];//摄像也要停止

    [self stopTimer];//定时器要停止

    [self removeFromSuperview];//刚刚创建的 view 要移除

    

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值