iOS-扫码带给我的坑

公司很多项目是有扫描二维码的功能的,例如扫码签到、扫码添加好友、扫码安装相应App等等。公司项目采用了QRCodeReader这个第三方库来处理扫码功能,我来说一说扫码遇到的一些问题。

一、QRCodeReader本身没有做扫描动画的效果,所以用户用起来会产生错觉,这个东西为什么没有微信扫码那样会上下动的线啊,是不是失灵了,没有用,扫不了啊???!!!
作为iOS开发人员的我,是不愿也不允许这样的事故发生在我经手的项目里。本打算使用原生二维码AVFoundation来重新做下扫码功能(详情可查看大神:http://www.jianshu.com/p/2180e940a589 ),但是发现可以在QRCodeReader基础上进行修改,而且QRCodeReader的简单封装也带来很多便利性。
解决步骤:
①在QRCodeReaderViewController.m文件里自定义configView方法,用以添加动画背景图片以及动画效果。

-(void)configView{
    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 211, 211)];
    _imageView.center=self.view.center;
    _imageView.image = [UIImage imageNamed:@"pick_bg"];
    [self.view addSubview:_imageView];

    upOrdown = NO;
    num =0;
    _line = [[UIImageView alloc] initWithFrame:CGRectMake(self.cameraView.frame.origin.x+50, self.cameraView.frame.origin.y+100, self.cameraView.frame.size.width -100, 2)];
    _line.image = [UIImage imageNamed:@"line.png"];
    [self.view addSubview:_line];

    timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(animation1) userInfo:nil repeats:YES];

}
-(void)animation1
{
    if (upOrdown == NO) {
        num ++;
        _line.frame = CGRectMake(_imageView.frame.origin.x, _imageView.frame.origin.y+2*num, _imageView.frame.size.width, 2);
        if (2*num >= _imageView.frame.size.height) {
            upOrdown = YES;
        }
    }
    else {
        num --;
        _line.frame = CGRectMake(_imageView.frame.origin.x,_imageView.frame.origin.y+2*num,_imageView.frame.size.width, 2);
        if (num <= 0) {
            upOrdown = NO;
        }
   }

}

②在QRCodeReaderViewController.m文件里- (id)initWithCancelButtonTitle:(nullable NSString *)cancelTitle codeReader:(nonnull QRCodeReader *)codeReader startScanningAtLoad:(BOOL)startScanningAtLoad showSwitchCameraButton:(BOOL)showSwitchCameraButton showTorchButton:(BOOL)showTorchButton;方法里调用configView方法。

- (id)initWithCancelButtonTitle:(nullable NSString *)cancelTitle codeReader:(nonnull QRCodeReader *)codeReader startScanningAtLoad:(BOOL)startScanningAtLoad showSwitchCameraButton:(BOOL)showSwitchCameraButton showTorchButton:(BOOL)showTorchButton
{
    if ((self = [super init])) {
    self.view.backgroundColor   = [UIColor blackColor];
    self.codeReader             = codeReader;
    self.startScanningAtLoad    = startScanningAtLoad;
    self.showSwitchCameraButton = showSwitchCameraButton;
    self.showTorchButton        = showTorchButton;

    if (cancelTitle == nil) {
      cancelTitle = NSLocalizedString(@"Cancel", @"Cancel");
    }

    [self setupUIComponentsWithCancelButtonTitle:cancelTitle];
    [self setupAutoLayoutConstraints];

    [_cameraView.layer insertSublayer:_codeReader.previewLayer atIndex:0];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];

    __weak typeof(self) weakSelf = self;

    [codeReader setCompletionWithBlock:^(NSString *resultAsString) {
      if (weakSelf.completionBlock != nil) {
        weakSelf.completionBlock(resultAsString);
      }

      if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(reader:didScanResult:)]) {
        [weakSelf.delegate reader:weakSelf didScanResult:resultAsString];
      }
    }];
      [self configView];
  }
  return self;
}

③到此,要做的动画效果就完美解决了。

二、近来公司准备做关于条形码的扫描功能。本来以为很简单嘛,以前是扫码支持的类型就只有AVMetadataObjectTypeQRCode(二维码),想着把AVMetadataObjectTypeEAN13Code、AVMetadataObjectTypeEAN8Code、AVMetadataObjectTypeCode128Code这三种类型添加上,不就支持条形码的扫描了嘛,想法是美好的,现实是残酷的!虽然是已经支持条形码扫描,但是识别电脑上的条形码是相当的慢,简直惨不忍睹!可是识别实物,例如笔记本上的条形码,却反映很快。查阅资料(http://www.cnblogs.com/coolcold/p/5775276.html )后,发现原来是焦距不够带来的严重问题。有了思路,就有解决方案。
解决方案:在QRCodeReader.m文件里找到toggleTorch方法,在toggleTorch方法里放大设备的焦距即可。

- (void)toggleTorch
{
  NSError *error = nil;

  [_defaultDevice lockForConfiguration:&error];

  if (error == nil) {
    AVCaptureTorchMode mode = _defaultDevice.torchMode;

    _defaultDevice.torchMode = mode == AVCaptureTorchModeOn ? AVCaptureTorchModeOff : AVCaptureTorchModeOn;
  }

  //_defaultDevice放大焦距
  if (_defaultDevice.activeFormat.videoMaxZoomFactor > 2) {
      _defaultDevice.videoZoomFactor = 2;
  }else{
      _defaultDevice.videoZoomFactor = _defaultDevice.activeFormat.videoMaxZoomFactor;
  }

  [_defaultDevice unlockForConfiguration];
}

放大焦距后,识别条形码的效率也是刚刚的。嘿嘿。。

你可能感兴趣的:(iOS-扫码带给我的坑)