iOS-原生二維碼

注釋?zhuān)捍似恼陆榻B如何用系統(tǒng)原生二維碼

首先引入系統(tǒng)頭文件
#import <AVFoundation/AVFoundation.h>
然后遵守代理
AVCaptureMetadataOutputObjectsDelegate

代碼如下:

@property (nonatomic,strong) AVCaptureDevice *device;//設(shè)備
@property (nonatomic,strong) AVCaptureDeviceInput *input;//輸入
@property (nonatomic,strong) AVCaptureMetadataOutput *output;//輸出
@property (nonatomic,strong) AVCaptureSession *session;//橋梁
@property (nonatomic,strong) AVCaptureVideoPreviewLayer *previewLayer;//攝像頭顯示圖層

@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,strong) UIView *lineView;
#pragma mark - 二維碼
//初始化
- (instancetype)init
{
    if(self == [super init])
    {
        [self createInstanceView];//創(chuàng)建覆蓋層
        [self setCamera];
    }
    return self;
}
- (void)createInstanceView
{
    UIView *upView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, UI_width, (UI_height-220*UI_width/320)/2-64)];
    upView.alpha = 0.5f;
    upView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:upView];
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(upView.frame), (UI_width-220*UI_width/320)/2, 220*UI_width/320)];
    leftView.alpha = 0.5f;
    leftView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:leftView];
    
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(UI_width-CGRectGetWidth(leftView.frame), CGRectGetMaxY(upView.frame), (UI_width-220*UI_width/320)/2, 220*UI_width/320)];
    rightView.alpha = 0.5f;
    rightView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:rightView];
    
    UIView *downView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(leftView.frame), UI_width, (UI_height-220*UI_width/320)/2)];
    downView.alpha = 0.5f;
    downView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:downView];
}
- (void)setCamera
{
    //Device
    if(!self.device){
        self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    }
    // Input
    if (!self.input) {
        self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
    }
    // Output
    if (!self.output) {
        self.output = [[AVCaptureMetadataOutput alloc]init];
        [self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    }
    // Session
    if (!self.session) {
        self.session = [[AVCaptureSession alloc]init];
        [self.session setSessionPreset:AVCaptureSessionPresetHigh];
    }
    if ([self.session canAddInput:self.input]){
        [self.session addInput:self.input];
    }
    if ([self.session canAddOutput:self.output]){
        [self.session addOutput:self.output];
    }
    self.output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];
    
    CGFloat x = (UI_width-220*UI_width/320)/2;
    CGFloat width = 220*UI_width/320;
    CGFloat height = 220*UI_width/320;
    CGFloat y = (UI_height-220*UI_width/320)/2;
    CGRect scanCrop = [self getScanCrop:CGRectMake(x, y, width, height) readerViewBounds:self.view.frame];
    self.output.rectOfInterest = scanCrop;

    // Preview
    if(!self.previewLayer){
        
        self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
        self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
//        self.previewLayer.frame = CGRectMake((UI_width-220*UI_width/320)/2, (UI_height-220*UI_width/320)/2,220*UI_width/320, 220*UI_width/320);
        self.previewLayer.frame = self.view.frame;
        [self.view.layer insertSublayer:self.previewLayer atIndex:0];
        
    }
    [self.session startRunning];
    
}
-(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds {
    
    CGFloat x,y,width,height;
    
    x = rect.origin.y/UI_height - 0.1;
    y = rect.origin.x/UI_width;
    width = rect.size.height/UI_height;
    height = rect.size.width/UI_width;
    
    return CGRectMake(x, y, width, height);
    
}

#pragma mark AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    if ([metadataObjects count] >0){
        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
        NSLog(@"metadataObject.stringValue = %@ \n metadataObject.corners = %@",metadataObject.stringValue,metadataObject.corners);//掃描后輸出信息
        if([[metadataObject.stringValue substringToIndex:7] isEqualToString:@"http://"])
        {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",metadataObject.stringValue]]];
        }
        else if ([[metadataObject.stringValue substringToIndex:4] isEqualToString:@"www."])
        {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@",metadataObject.stringValue]]];
        }
        else
        {
            [Helper helperPointWithTitle:metadataObject.stringValue];
            [self.session startRunning];
        }
        [self.session stopRunning];
        [self.timer invalidate];
    }
}
//注意惭蹂,在界面消失的時(shí)候關(guān)閉session
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.session stopRunning];
}
//以下是UI,動(dòng)畫(huà)掃描
- (void)createUI
{
    UIImageView *bgImageView = [YYTools createImageViewWithFrame:CGRectMake(0, 0, 265*UI_width/320, 265*UI_width/320) andBackgroundColor:[UIColor clearColor] andImage:@"bg_scanner.png" andObject:nil andSEL:nil andTag:0];
    bgImageView.center = self.view.center;
    [self.view addSubview:bgImageView];
    
    self.lineView = [YYTools createViewWithFrame:CGRectMake((UI_width-270*UI_width/320)/2, (UI_height-220*UI_width/320)/2, 270*UI_width/320, 1) andBackgroundColoc:[UIColor greenColor]];
    self.lineView.layer.cornerRadius = 8;
    [self.view addSubview:self.lineView];//+220*UI_width/320
    if(!self.timer)
    {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(animatedTimer) userInfo:nil repeats:YES];
    }
}
- (void)animatedTimer
{
    if(self.lineView.frame.origin.y == (UI_height-220*UI_width/320)/2)
    {
        [UIView animateWithDuration:1.0f animations:^{
            self.lineView.frame = CGRectMake((UI_width-270*UI_width/320)/2, (UI_height-220*UI_width/320)/2+220*UI_width/320, 270*UI_width/320, 1);
        }];
    }
    else if ((UI_height-220*UI_width/320)/2+220*UI_width/320)
    {
        [UIView animateWithDuration:1.0f animations:^{
            self.lineView.frame = CGRectMake((UI_width-270*UI_width/320)/2, (UI_height-220*UI_width/320)/2, 270*UI_width/320, 1);
        }];
    }
}

效果圖如下:


B8846C919DED31A0493B7AE0E0C8EF06.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市氮凝,隨后出現(xiàn)的幾起案子销钝,更是在濱河造成了極大的恐慌酵镜,老刑警劉巖贮庞,帶你破解...
    沈念sama閱讀 217,509評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件试幽,死亡現(xiàn)場(chǎng)離奇詭異萨脑,居然都是意外死亡隐轩,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén)渤早,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)职车,“玉大人,你說(shuō)我怎么就攤上這事鹊杖°擦椋” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,875評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵骂蓖,是天一觀的道長(zhǎng)积瞒。 經(jīng)常有香客問(wèn)我登下,道長(zhǎng)茫孔,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,441評(píng)論 1 293
  • 正文 為了忘掉前任被芳,我火速辦了婚禮缰贝,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘畔濒。我一直安慰自己剩晴,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布篓冲。 她就那樣靜靜地躺著李破,像睡著了一般宠哄。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上嗤攻,一...
    開(kāi)封第一講書(shū)人閱讀 51,365評(píng)論 1 302
  • 那天毛嫉,我揣著相機(jī)與錄音,去河邊找鬼妇菱。 笑死承粤,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的闯团。 我是一名探鬼主播辛臊,決...
    沈念sama閱讀 40,190評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼房交!你這毒婦竟也來(lái)了彻舰?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,062評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤候味,失蹤者是張志新(化名)和其女友劉穎刃唤,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體白群,經(jīng)...
    沈念sama閱讀 45,500評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡尚胞,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評(píng)論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了帜慢。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片笼裳。...
    茶點(diǎn)故事閱讀 39,834評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖粱玲,靈堂內(nèi)的尸體忽然破棺而出躬柬,到底是詐尸還是另有隱情,我是刑警寧澤抽减,帶...
    沈念sama閱讀 35,559評(píng)論 5 345
  • 正文 年R本政府宣布楔脯,位于F島的核電站,受9級(jí)特大地震影響胯甩,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜堪嫂,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評(píng)論 3 328
  • 文/蒙蒙 一偎箫、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧皆串,春花似錦淹办、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,779評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)姥宝。三九已至,卻和暖如春恐疲,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背碳蛋。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,912評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工肃弟, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留零蓉,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,958評(píng)論 2 370
  • 正文 我出身青樓感论,卻偏偏與公主長(zhǎng)得像紊册,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子囊陡,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容