// ViewController.m
// QRCode
//
// Created by 殷婷婷 on 15-8-1.
// Copyright (c) 2015年 manluotuo. All rights reserved.
//
#import "ViewController.h"
#import "ZBarSDK.h"
@interface ViewController ()<ZBarReaderViewDelegate>
@property(nonatomic,strong)ZBarReaderView *readerView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
ZBarReaderView *readerView = [[ZBarReaderView alloc]init];
readerView.frame = CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height - 44);
readerView.readerDelegate = self;
//关闭闪光灯
readerView.torchMode = 0;
//扫描区域
CGRect scanMaskRect = CGRectMake(60, CGRectGetMidY(readerView.frame) - 126, 200, 200);
//处理模拟器
if (TARGET_IPHONE_SIMULATOR) {
ZBarCameraSimulator *cameraSimulator
= [[ZBarCameraSimulator alloc]initWithViewController:self];
cameraSimulator.readerView = readerView;
}
[self.view addSubview:readerView];
self.readerView = readerView;
//扫描区域计算
readerView.scanCrop = [self getScanCrop:scanMaskRect readerViewBounds:self.readerView.bounds];
[readerView start];
}
-(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds
{
CGFloat x,y,width,height;
x = rect.origin.x / readerViewBounds.size.width;
y = rect.origin.y / readerViewBounds.size.height;
width = rect.size.width / readerViewBounds.size.width;
height = rect.size.height / readerViewBounds.size.height;
return CGRectMake(x, y, width, height);
}
- (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image
{
for (ZBarSymbol *symbol in symbols) {
NSLog(@"%@", symbol.data);
break;
}
[self.readerView stop];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end