iOS-掃碼帶給我的坑

公司很多項(xiàng)目是有掃描二維碼的功能的,例如掃碼簽到、掃碼添加好友、掃碼安裝相應(yīng)App等等。公司項(xiàng)目采用了QRCodeReader這個(gè)第三方庫(kù)來(lái)處理掃碼功能,我來(lái)說(shuō)一說(shuō)掃碼遇到的一些問題。

一、QRCodeReader本身沒有做掃描動(dòng)畫的效果,所以用戶用起來(lái)會(huì)產(chǎn)生錯(cuò)覺,這個(gè)東西為什么沒有微信掃碼那樣會(huì)上下動(dòng)的線啊,是不是失靈了,沒有用,掃不了?????!?。?br> 作為iOS開發(fā)人員的我,是不愿也不允許這樣的事故發(fā)生在我經(jīng)手的項(xiàng)目里。本打算使用原生二維碼AVFoundation來(lái)重新做下掃碼功能(詳情可查看大神簡(jiǎn)書:http://www.itdecent.cn/p/2180e940a589 ),但是發(fā)現(xiàn)可以在QRCodeReader基礎(chǔ)上進(jìn)行修改,而且QRCodeReader的簡(jiǎn)單封裝也帶來(lái)很多便利性。
解決步驟:
①在QRCodeReaderViewController.m文件里自定義configView方法,用以添加動(dòng)畫背景圖片以及動(dòng)畫效果。

-(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;方法里調(diào)用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;
}

③到此,要做的動(dòng)畫效果就完美解決了。

二、近來(lái)公司準(zhǔn)備做關(guān)于條形碼的掃描功能。本來(lái)以為很簡(jiǎn)單嘛,以前是掃碼支持的類型就只有AVMetadataObjectTypeQRCode(二維碼),想著把AVMetadataObjectTypeEAN13Code、AVMetadataObjectTypeEAN8Code、AVMetadataObjectTypeCode128Code這三種類型添加上,不就支持條形碼的掃描了嘛,想法是美好的,現(xiàn)實(shí)是殘酷的!雖然是已經(jīng)支持條形碼掃描,但是識(shí)別電腦上的條形碼是相當(dāng)?shù)穆?,?jiǎn)直慘不忍睹!可是識(shí)別實(shí)物,例如筆記本上的條形碼,卻反映很快。查閱資料(http://www.cnblogs.com/coolcold/p/5775276.html )后,發(fā)現(xiàn)原來(lái)是焦距不夠帶來(lái)的嚴(yán)重問題。有了思路,就有解決方案。
解決方案:在QRCodeReader.m文件里找到toggleTorch方法,在toggleTorch方法里放大設(shè)備的焦距即可。

- (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];
}

放大焦距后,識(shí)別條形碼的效率也是剛剛的。嘿嘿。。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容