ZBar使用起来也非常简单,将ZBarSDK导入项目,在需要使用ZBar的文件中导入ZBarSDK.h头文件即可,以下是ZBarReaderView的初始化方法:
- (void)viewDidLoad
{
[super viewDidLoad];
_readerView = [[ZBarReaderView alloc] init];
_readerView.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64);
_readerView.layer.cornerRadius = 8.0f;
_readerView.layer.borderWidth= 1.0f;
_readerView.layer.borderColor = [[UIColor blueColor] CGColor];
_readerView.readerDelegate = self;
//关闭闪光灯
_readerView.torchMode = 0;
//扫描区域
CGRect scanMaskRect = CGRectMake(60, 60, 200, 200);
if (TARGET_IPHONE_SIMULATOR) {
ZBarCameraSimulator *cameraSimulator = [[ZBarCameraSimulator alloc] initWithViewController:self];
cameraSimulator.readerView = _readerView;
}
[self.view addSubview:_readerView];
//扫描区域计算
_readerView.scanCrop = [self getScanCropWithScanRect:scanMaskRect andReaderViewBounds:_readerView.bounds];
[_readerView start];
}
- (CGRect)getScanCropWithScanRect:(CGRect)rect andReaderViewBounds:(CGRect)rvBounds
{
CGFloat x,y,width,height;
x = rect.origin.y / rvBounds.size.height;
y = 1 - (rect.origin.x + rect.size.width) / rvBounds.size.width;
width = rect.size.height / rvBounds.size.height;
height = rect.size.width / rvBounds.size.width;
return CGRectMake(x, y, width, height);
}