ZBar使用起来也非常简单,将ZBarSDK导入项目,在需要使用ZBar的文件中导入ZBarSDK.h头文件即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #pragma mark 初始化扫描 - ( void )InitScan { readview = [ZBarReaderView new ]; readview.backgroundColor = [UIColor clearColor]; readview.frame = CGRectMake( 0 , 0 , self.view.frame.size.width, self.view.frame.size.height); readview.readerDelegate = self; readview.allowsPinchZoom = YES; //使用手势变焦 readview.trackingColor = [UIColor redColor]; readview.showsFPS = NO; // 显示帧率 YES 显示 NO 不显示 //readview.scanCrop = CGRectMake(0, 0, 1, 1);//将被扫描的图像的区域 UIImage *hbImage=[UIImage imageNamed:@ "pick_bg.png" ]; scanZomeBack=[[UIImageView alloc] initWithImage:hbImage]; //添加一个背景图片 CGRect mImagerect=CGRectMake((readview.frame.size.width- 200 )/ 2.0 , (readview.frame.size.height- 200 )/ 2.0 , 200 , 200 ); [scanZomeBack setFrame:mImagerect]; readview.scanCrop = [self getScanCrop:mImagerect readerViewBounds:readview.bounds]; //将被扫描的图像的区域 [readview addSubview:scanZomeBack]; [readview addSubview:readLineView]; [self.view addSubview:readview]; [readview start]; } |
1 2 3 4 5 6 7 8 9 10 11 12 | #pragma mark 获取扫描区域 -(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); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #pragma mark 扫描动画 -( void )loopDrawLine { CGRect rect = CGRectMake(scanZomeBack.frame.origin.x, scanZomeBack.frame.origin.y, scanZomeBack.frame.size.width, 2 ); if (readLineView) { [readLineView removeFromSuperview]; } readLineView = [[UIImageView alloc] initWithFrame:rect]; [readLineView setImage:[UIImage imageNamed:@ "line.png" ]]; [UIView animateWithDuration: 3.0 delay: 0.0 options: UIViewAnimationOptionCurveEaseIn animations:^{ //修改fream的代码写在这里 readLineView.frame =CGRectMake(scanZomeBack.frame.origin.x, scanZomeBack.frame.origin.y+scanZomeBack.frame.size.height, scanZomeBack.frame.size.width, 2 ); [readLineView setAnimationRepeatCount: 0 ]; } completion:^(BOOL finished){ if (!is_Anmotion) { [self loopDrawLine]; } }]; [readview addSubview:readLineView]; } #pragma mark 获取扫描结果 - ( void )readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image { // 得到扫描的条码内容 const zbar_symbol_t *symbol = zbar_symbol_set_first_symbol(symbols.zbarSymbolSet); NSString *symbolStr = [NSString stringWithUTF8String: zbar_symbol_get_data(symbol)]; if (zbar_symbol_get_type(symbol) == ZBAR_QRCODE) { // 是否QR二维码 } for (ZBarSymbol *symbol in symbols) { [sTxtField setText:symbol.data]; break ; } [readerView stop]; [readerView removeFromSuperview]; } |
联系客服