zoukankan      html  css  js  c++  java
  • ZXingObjC二维码扫描

    #import "QRScanViewController.h"
    #import "AppDelegate.h"
    
    @interface QRScanViewController ()
    {
        BOOL infoShowing;
        UIAlertView *alert;
        BOOL upToDown;
        int num;
        UIImageView *lineImageView;
        NSTimer *timer;
    }
    @end
    
    @implementation QRScanViewController
    @synthesize capture;
    @synthesize scanRectView;
    @synthesize backImageView;
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
            [self initViewFrame];
        }
        return self;
    }
    
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.capture = [[ZXCapture alloc] init];
        self.capture.camera = self.capture.back;
        self.capture.focusMode = AVCaptureFocusModeContinuousAutoFocus;
        self.capture.rotation = 90.0f;
        self.capture.layer.frame = self.view.bounds;
        
        [self.view.layer addSublayer:self.capture.layer];
        [self.view bringSubviewToFront:self.backImageView];
        [self.view bringSubviewToFront:self.scanRectView];
        
        
        lineImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"scan_line.png"]];
        lineImageView.frame = CGRectMake(self.scanRectView.frame.origin.x, self.scanRectView.frame.origin.y, self.scanRectView.frame.size.width, 4);
        [self.view addSubview:lineImageView];
    }
    
    - (void)lineAnimation
    {
        if (!([self isViewLoaded] && [self.view superview])) {
            return;
        }
        if (upToDown) {
            num++;
            float temp = 0.0;
            if (4*num > self.scanRectView.frame.size.height-4) {
                temp = self.scanRectView.frame.size.height+self.scanRectView.frame.origin.y;
                upToDown = NO;
            }else{
                temp = self.scanRectView.frame.origin.y + 4*num;
            }
            lineImageView.frame = CGRectMake(self.scanRectView.frame.origin.x, temp, self.scanRectView.frame.size.width, 4);
        }else{
            num--;
            float temp = 0.0;
            if (num <= 0) {
                temp = self.scanRectView.frame.origin.y;
                upToDown = YES;
            }else{
                temp = self.scanRectView.frame.origin.y + 4*num;
            }
            lineImageView.frame = CGRectMake(self.scanRectView.frame.origin.x, temp, self.scanRectView.frame.size.width, 4);
        }
    }
    
    - (void)viewDidAppear:(BOOL)animated
    {
        if (timer) {
            [timer invalidate];
            timer = nil;
        }
        upToDown = YES;
        num = 0;
        lineImageView.frame = CGRectMake(self.scanRectView.frame.origin.x, self.scanRectView.frame.origin.y, self.scanRectView.frame.size.width, 4);
        timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(lineAnimation) userInfo:nil repeats:YES];
    }
    
    - (void)stopTimer
    {
        if (timer) {
            [timer invalidate];
            timer = nil;
        }
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        
        self.capture.delegate = self;
        self.capture.layer.frame = self.view.bounds;
    //    CGAffineTransform captureSizeTransform = CGAffineTransformMakeScale(320 / self.view.frame.size.width, 480 / self.view.frame.size.height);
    //    self.capture.scanRect = CGRectApplyAffineTransform(self.scanRectView.frame, captureSizeTransform);
        if (viewer.device == DEVICE_IPAD || viewer.device == DEVICE_IPAD3) {
            [self showaCapture];
        }else{
            self.capture.rotation = 90.0f;
            CGAffineTransform transform = CGAffineTransformMakeRotation(0);
            [self.capture setTransform:transform];
            CGRect f = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
            self.view.layer.frame=f;
            self.capture.layer.frame = f;
        }
    }
    
    - (void)showaCapture
    {
        CGAffineTransform transform;
        if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
        {
            self.capture.rotation = 180.0f;
            transform = CGAffineTransformMakeRotation(M_PI/2);
        }
        else if (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
        {
            self.capture.rotation = 0.0f;
            transform = CGAffineTransformMakeRotation(-M_PI/2);
        }
        else if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
        {
            self.capture.rotation = 90.0f;
            transform = CGAffineTransformMakeRotation(0);
        }
        else if (self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        {
            self.capture.rotation = 270.0f;
            transform = CGAffineTransformMakeRotation(M_PI);
        }
        
        [self.capture setTransform:transform];
        CGRect f = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
        self.view.layer.frame=f;
        self.capture.layer.frame = f;
    }
    
    
    #pragma mark - ZXCaptureDelegate Methods
    
    //- (void)captureCameraIsReady:(ZXCapture *)capture
    //{
    //
    //}
    //
    //- (void)captureSize:(ZXCapture *)capture (NSNumber *)width height:(NSNumber *)height
    //{
    //    
    //}
    
    - (void)captureResult:(ZXCapture *)capture result:(ZXResult *)result
    {
        if (!result) {
            return;
        }
        NSLog(@"结果是什么= %@",result.text);
        NSString *formatString = [self barcodeFormatToString:result.barcodeFormat];
        if (![formatString isEqualToString:@"QR Code"]) {
            return;
        }
        
        NSString *display = [NSString stringWithFormat:@"Scanned!
    
    Format: %@
    
    Contents:
    %@", formatString, result.text];
        NSLog(@"二维码是%@",display);
    //    [decodedLabel performSelectorOnMainThread:@selector(setText:) withObject:display waitUntilDone:YES];
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        if (infoShowing) {
            return;
        }
        if (alert) {
            [alert release];
            alert = nil;
        }
        alert = [[UIAlertView alloc] initWithTitle:@"信息" message:result.text delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
        alert.delegate = self;
        [alert show];
        infoShowing = YES;
    }
    
    #pragma mark - Private Methods
    
    - (NSString *)barcodeFormatToString:(ZXBarcodeFormat)format {
        switch (format) {
            case kBarcodeFormatAztec:
                return @"Aztec";
                
            case kBarcodeFormatCodabar:
                return @"CODABAR";
                
            case kBarcodeFormatCode39:
                return @"Code 39";
                
            case kBarcodeFormatCode93:
                return @"Code 93";
                
            case kBarcodeFormatCode128:
                return @"Code 128";
                
            case kBarcodeFormatDataMatrix:
                return @"Data Matrix";
                
            case kBarcodeFormatEan8:
                return @"EAN-8";
                
            case kBarcodeFormatEan13:
                return @"EAN-13";
                
            case kBarcodeFormatITF:
                return @"ITF";
                
            case kBarcodeFormatPDF417:
                return @"PDF417";
                
            case kBarcodeFormatQRCode:
                return @"QR Code";
                
            case kBarcodeFormatRSS14:
                return @"RSS 14";
                
            case kBarcodeFormatRSSExpanded:
                return @"RSS Expanded";
                
            case kBarcodeFormatUPCA:
                return @"UPCA";
                
            case kBarcodeFormatUPCE:
                return @"UPCE";
                
            case kBarcodeFormatUPCEANExtension:
                return @"UPC/EAN extension";
                
            default:
                return @"Unknown";
        }
    }
    
    //- (BOOL)shouldAutorotate
    //{
    //    return YES;
    //}
    //
    //- (BOOL)shouldAutomaticallyForwardRotationMethods
    //{
    //    return YES;
    //}
    //
    //- (NSUInteger)supportedInterfaceOrientations
    //{
    //    return UIInterfaceOrientationMaskAll;
    //}
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (void)rotated
    {
            [self setViewFrame];
    }
    
    - (void)setViewFrame{
    }
    
    - (void)initViewFrame{
    }
    
    #pragma mark - UIAlertViewDelegate
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if (buttonIndex != alert.cancelButtonIndex) {
            
        }
        infoShowing = NO;
    }
    
    - (void)dealloc
    {
        [super dealloc];
    }
    /*
    #pragma mark - Navigation
    
    @end
  • 相关阅读:
    Java核心篇,二十三种设计模式(二),结构型——适配器模式
    Java第三十七天,Mybatis框架系列,基于注解的缓存控制
    Java第三十七天,Mybatis框架系列,缓存
    Java第三十七天,Mybatis框架系列,延迟加载(基于 Collection 的实现方式)
    Java第四十五天,SpringBoot框架系列,SpringBoot基础(五)
    第十一篇 Django 【基础篇】
    第十篇 web应用&http协议
    第八篇 (2)jQuery
    第九篇 BootStrap
    第八篇 JavaScript
  • 原文地址:https://www.cnblogs.com/mgbert/p/3954346.html
Copyright © 2011-2022 走看看