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 width:(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!\n\nFormat: %@\n\nContents:\n%@", 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
时间: 2024-10-31 11:11:13

ZXingObjC二维码扫描的相关文章

二维码扫描

1.将ZBar的SDK导入工程 SDK下载地址:https://i.cnblogs.com/Files.aspx 或者去官网下载:https://github.com/bmorton/ZBarSDK 2.设置依赖库 需要添加AVFoundation  CoreMedia  CoreVideo QuartzCore libiconv 3.修改工程配置 1)      Framework Search Path 2)      如果使用xcode7.0以上 ,还需设置: /* 4. 版本说明  1.

iOS使用AVFoundation实现二维码扫描

实现过程如下: Step1:需要导入:AVFoundation Framework 包含头文件: #import <AVFoundation/AVFoundation.h> Step2:设置捕获会话 设置 AVCaptureSession 和 AVCaptureVideoPreviewLayer 成员 1 2 3 4 5 6 7 8 9 10 #import <AVFoundation/AVFoundation.h> static const char *kScanQRCodeQu

【转】 iOS使用AVFoundation实现二维码扫描

原文:http://strivingboy.github.io/blog/2014/11/08/scan-qrcode/ 关于二维码扫描有不少优秀第三方库如: ZBar SDK 里面有详细的文档,相应介绍也非常多,如:http://rdcworld-iphone.blogspot.in/2013/03/how-to-use-barcode-scanner-br-and-qr-in.html ZXing google推出的开源项目,相应介绍如:http://blog.devtang.com/blo

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

iOS开发-二维码扫描和应用跳转 序言 前面我们已经调到过怎么制作二维码,在我们能够生成二维码之后,如何对二维码进行扫描呢? 在iOS7之前,大部分应用中使用的二维码扫描是第三方的扫描框架,例如ZXing或者ZBar.使用时集成麻烦,出错也不方便调试.在iOS7之后,苹果自身提供了二维码的扫描功能,从效率上来说,原生的二维码远高于这些第三方框架.本文讲解如何使用原生框架实现二维码扫描功能,并且进行扫描后的项目跳转.ps:本期的源代码会在文章结尾给出链接 扫描相关类 二维码扫描需要获取摄像头并读取

Swift:使用系统AVFoundation实现二维码扫描和生成

系统提供的AVCaptureSession仅仅适用于iOS7.0以上的系统.之前的请用Zbar来替代 下载地址:http://download.csdn.net/detail/huobanbengkui/8881097 配置project: 引入: import Foundation import AVFoundation 接受AVCaptureMetadataOutputObjectsDelegate(如: class QrcodeVC: UIViewController,AVCaptureM

iOS 系统二维码扫描(可限制扫描区域)

使用 AVFoundation系统库来进行二维码扫描并且限制扫描二维码的范围.(因为默认的是全屏扫描) -(void)beginCode { //1.摄像头设备 AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; /* // Media types AVMediaTypeVideo AVMediaTypeAudio AVMediaTypeText AVMediaTypeC

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

Ios二维码扫描 这里给大家介绍的时如何使用系统自带的二维码扫描方法和一些简单的动画! 操作步骤: 1).首先你需要搭建UI界面如图:下图我用了俩个imageview和一个label 2).你需要在你当前的控制器中导入 #import <AVFoundation/AVFoundation.h> <AVCaptureMetadataOutputObjectsDelegate>代理 3).在@interface中定义 @property (nonatomic,strong)AVCapt

二维码扫描开源库ZXing定制化

(抱歉文章还在修改但是不小心发布了= =) 最近在用ZXing这个开源库做二维码的扫描模块,开发过程的一些代码修改和裁剪的经验和大家分享一下. 我的代码库: https://github.com/SickWorm/ZXingDialog 代码没有在github维护,所以没有log.但是所有修改的地方我都加上了“@ch”的注释,以方便定位 官方源码: https://github.com/zxing/zxing 实现功能: 1.功能裁剪(只保留QRCode二维码扫描功能,去掉条形码等其他码扫描功能

ios 二维码扫描

https://github.com/bmorton/ZBarSDK 我是用ZBar做的二维码扫描 其他的SDK也可以 首先是导入库文件: 1.AVFoundation.framework 2.CoreMedia.framework 3.CoreVideo.framework 4.QuartzCore.framework 5.libiconv.dylib 这个5个库文件 在ViewController.h 导入#import "ZBarSDK.h" 并且 继承 <ZBarRead