程胖出品——二維碼掃描(原生掃描+生成二維碼+讀取相冊(cè)二維碼)

寫在前面:這篇文章暫分四部分來(lái)介紹原生二維碼掃描。

蘋果原生二維碼的功能確實(shí)很強(qiáng)大,掃描速度快,不需要對(duì)項(xiàng)目進(jìn)行配置、簡(jiǎn)單好用......我覺得后一項(xiàng)優(yōu)勢(shì)就已經(jīng)讓我有足夠的理由拋棄ZBar/ZXing等第三方了——呵呵,誰(shuí)配誰(shuí)知道.....
但是很難在網(wǎng)上無(wú)法找到一個(gè)既能掃描,又可以生成二維碼圖片,還可以從相冊(cè)中讀取二維碼,而且還是蘋果原生代碼的項(xiàng)目。所以,自己動(dòng)手寫了一個(gè)。當(dāng)然中間代碼和思路也多有借鑒,如有雷同,哈哈......
參考大神:artifeng和張國(guó)兵(因?yàn)闆]找到鏈接,這里以名稱代替了先,后面找到會(huì)添加上)。
核心內(nèi)容已經(jīng)展示完畢,如有需要會(huì)再次補(bǔ)充。
時(shí)間倉(cāng)促,如有錯(cuò)誤敬請(qǐng)指正,共同進(jìn)步。

A 相機(jī)掃描二維碼

相機(jī)掃描,第一項(xiàng)當(dāng)然是獲得相機(jī)設(shè)備了,所以模擬器是做不了二維碼掃描的。
1 device 獲取攝像設(shè)備

- (AVCaptureDevice *)device
{
    if (_device == nil) {
        //AVMediaTypeVideo是打開相機(jī)
        //AVMediaTypeAudio是打開麥克風(fēng)
        _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    }
    return _device;
}
  • 其中需要注意的是
    AVMediaTypeVideo是打開相機(jī)
    AVMediaTypeAudio是打開麥克風(fēng)

2 input 創(chuàng)建輸入流

- (AVCaptureDeviceInput *)input
{
    if (_input == nil) {
        _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
    }
    return _input;
}
  • 如果無(wú)法獲取設(shè)備,那么也無(wú)法獲取輸入流,這里可以進(jìn)行判斷
if (!input) return;
  • 或者直接對(duì)獲取設(shè)備進(jìn)行判斷
if (self.device == nil) {
        [self showAlertViewWithMessage:@"未檢測(cè)到相機(jī)"];
        return;
    }

3 output 創(chuàng)建輸出流

- (AVCaptureMetadataOutput *)output
{
    if (_output == nil) {
        _output = [[AVCaptureMetadataOutput alloc]init];
        [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
        //限制掃描區(qū)域(上下左右)
        [_output setRectOfInterest:[self rectOfInterestByScanViewRect:_imageView.frame]];
    }
    return _output;
}
  • output如果不打開就無(wú)法輸出掃描得到的信息
  • 這里需要設(shè)置輸出對(duì)象解析數(shù)據(jù)時(shí)感興趣的范圍。默認(rèn)值是CGRect(x: 0,y: 0, width: 1,height: 1)。通過(guò)對(duì)這個(gè)值的觀察,我們發(fā)現(xiàn)傳入的是比例。注意:參照是以橫屏的左上角作為原點(diǎn),而不是豎屏。掃描區(qū)域封裝成方法,可以方便復(fù)用。
- (CGRect)rectOfInterestByScanViewRect:(CGRect)rect {
    CGFloat width = CGRectGetWidth(self.view.frame);
    CGFloat height = CGRectGetHeight(self.view.frame);
    
    CGFloat x = (height - CGRectGetHeight(rect)) / 2 / height;
    CGFloat y = (width - CGRectGetWidth(rect)) / 2 / width;
    
    CGFloat w = CGRectGetHeight(rect) / height;
    CGFloat h = CGRectGetWidth(rect) / width;
    
    return CGRectMake(x, y, w, h);
}
  

4 session 初始化鏈接對(duì)象

  • session是輸入輸出的中間橋梁
- (AVCaptureSession *)session
{
    if (_session == nil) {
        //session
        _session = [[AVCaptureSession alloc]init];
        [_session setSessionPreset:AVCaptureSessionPresetHigh];
        if ([_session canAddInput:self.input]) {
            [_session addInput:self.input];
        }
        if ([_session canAddOutput:self.output]) {
            [_session addOutput:self.output];
        }
    }
    return _session;
}

**5 preview **

- (AVCaptureVideoPreviewLayer *)preview
{
    if (_preview == nil) {
        _preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
    }
    return _preview;
}

6 初始化掃描配置

- (void)scanSetup
{
    //1 添加預(yù)覽圖層
    self.preview.frame = self.view.bounds;
    self.preview.videoGravity = AVLayerVideoGravityResize;
    [self.view.layer insertSublayer:self.preview atIndex:0];
    
    //2 設(shè)置輸出能夠解析的數(shù)據(jù)類型
    //注意:設(shè)置數(shù)據(jù)類型一定要在輸出對(duì)象添加到回話之后才能設(shè)置
    [self.output setMetadataObjectTypes:@[AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypeQRCode]];
    
    //高質(zhì)量采集率
    [self.session setSessionPreset:AVCaptureSessionPresetHigh];
    
    //3 開始掃描
    [self.session startRunning];
    
}

7 打開/關(guān)閉掃描

  • 打開掃描
[self.session starRunning];
  • 關(guān)閉掃描
[self.session stopRunning];

8 代理方法讀取掃描信息

  • 其實(shí)1-7都是配置工作,第8才是重點(diǎn),因?yàn)槿抗ぷ鞫际且@取攝像頭掃描得到的信息。
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    if ([metadataObjects count] > 0) {
        AVMetadataMachineReadableCodeObject *metadataObject = [metadataObjects objectAtIndex:0];
        if ([metadataObject isKindOfClass:[AVMetadataMachineReadableCodeObject class]]) {
            NSString *stringValue = [metadataObject stringValue];
            if (stringValue != nil) {
                [self.session stopRunning];
                //掃描結(jié)果
                self.scannedResult = stringValue;
                NSLog(@"%@",stringValue);
                [self showAlertViewWithMessage:stringValue];
                
            }
        }
        
    }
}
  • 對(duì)于掃描結(jié)果你可以自己處理,包括判斷是字符串還是網(wǎng)頁(yè)鏈接等等

B 通過(guò)輸入文本創(chuàng)建簡(jiǎn)單二維碼

  • 第一步當(dāng)然是在輸入框中輸入信息。這個(gè)自行配置輸入框
  • 創(chuàng)建二維碼。步驟中描述的比較清楚,不再一一列出
    //關(guān)閉掃描
    [self stopScan];
    //鍵盤下落
    [self.view endEditing:YES];
    
    //1 實(shí)例化二維碼濾鏡
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    //2 回復(fù)濾鏡的默認(rèn)屬性(因?yàn)闉V鏡有可能保存上一次的屬性)
    [filter setDefaults];
    //3 經(jīng)字符串轉(zhuǎn)化為NSData
    NSData *data = [self.textField.text dataUsingEncoding:NSUTF8StringEncoding];
    //4 通過(guò)KVC設(shè)置濾鏡,傳入data,將來(lái)濾鏡就知道要通過(guò)傳入的數(shù)據(jù)生成二維碼
    [filter setValue:data forKey:@"inputMessage"];
    //5 生成二維碼
    CIImage *image = [filter outputImage];
    //補(bǔ)充:CIImage是CoreImage框架中最基本代表圖像的對(duì)象,他不僅包含原圖像數(shù)據(jù),還包含作用在原圖像上的濾鏡鏈
  • 二維碼信息已經(jīng)得到,這時(shí)需要將二維碼信息轉(zhuǎn)化為image才能夠方便我們觀看和保存。這里有兩種方式將二維碼信息轉(zhuǎn)化為imageView。一種高清、一種模糊。一般選擇第一種即可。
//<這樣講CIImage直接轉(zhuǎn)換成UIImage生成的二維碼比較模糊,有點(diǎn)事比較簡(jiǎn)單>
    UIImage *image1 = [UIImage imageWithCIImage:image];
self.imageView.image= [self createNonInterpolatedUIImageFormCIImage:image withSize:100.0];
---------這個(gè)轉(zhuǎn)化方法可以直接復(fù)用---------
//將得到的文本數(shù)據(jù)轉(zhuǎn)化為高清圖片
//由于生成的二維碼是CIImage類型,如果直接轉(zhuǎn)換成UIImage,大小不好控制,圖片模糊
//高清方法:CIImage->CGImageRef->UIImage
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {
    CGRect extent = CGRectIntegral(image.extent);
    //設(shè)置比例
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    // 創(chuàng)建bitmap(位圖);
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    // 保存bitmap到圖片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}
  • 將創(chuàng)建的二維碼圖片保存到相冊(cè)
- (void)saveImageToPhotoLib
{
    UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(saveImage:didFinishSavingWithError:contextInfo:), nil);
}
- (void)saveImage:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error == nil) {
        [self showAlertViewWithTitle:@"保存圖片" withMessage:@"成功"];
    }
    else
    {
        [self showAlertViewWithTitle:@"保存圖片" withMessage:@"失敗"];
    }
}

C 相冊(cè)讀取二維碼

  • 第一步當(dāng)然是要先打開相冊(cè)了
- (void)chooseButtonClick
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        //關(guān)閉掃描
        [self stopScan];
        
        //1 彈出系統(tǒng)相冊(cè)
        UIImagePickerController *pickVC = [[UIImagePickerController alloc]init];
        //2 設(shè)置照片來(lái)源
        /**
         UIImagePickerControllerSourceTypePhotoLibrary,相冊(cè)
         UIImagePickerControllerSourceTypeCamera,相機(jī)
         UIImagePickerControllerSourceTypeSavedPhotosAlbum,照片庫(kù)
         */

        pickVC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        //3 設(shè)置代理
        pickVC.delegate = self;
        //4.隨便給他一個(gè)轉(zhuǎn)場(chǎng)動(dòng)畫
        self.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:pickVC animated:YES completion:nil];
    }
    else
    {
        [self showAlertViewWithTitle:@"打開失敗" withMessage:@"相冊(cè)打開失敗。設(shè)備不支持訪問(wèn)相冊(cè),請(qǐng)?jiān)谠O(shè)置->隱私->照片中進(jìn)行設(shè)置!"];
        
    }
    
}
  • 從相冊(cè)中選取照片并讀取照片上的二維碼信息
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    //1 獲取選擇的圖片
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    //初始化一個(gè)監(jiān)聽器
    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy : CIDetectorAccuracyHigh}];
    [picker dismissViewControllerAnimated:YES completion:^{
        //監(jiān)測(cè)到的結(jié)果數(shù)組
        NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:image.CGImage]];
        if (features.count >= 1) {
            //結(jié)果對(duì)象
            CIQRCodeFeature *feature = [features objectAtIndex:0];
            NSString *scannedResult = feature.messageString;
            [self showAlertViewWithTitle:@"讀取相冊(cè)二維碼" withMessage:scannedResult];
        }
        else
        {
            [self showAlertViewWithTitle:@"讀取相冊(cè)二維碼" withMessage:@"讀取失敗"];
        }
    }];
}

D 掃描特效&其他設(shè)置

  • 掃描方式有很多種,這里貼出來(lái)一種比較簡(jiǎn)單的:通過(guò)定時(shí)器控制View做動(dòng)畫。大家可以充分使用各種技巧來(lái)使得掃描動(dòng)作更加豐富。
- (void)addTimer
{
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.008 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
}
//控制掃描線上下滾動(dòng)
- (void)timerMethod
{
    if (upOrDown == NO) {
        num ++;
        _line.frame = CGRectMake(CGRectGetMinX(_imageView.frame)+5, CGRectGetMinY(_imageView.frame)+5+num, CGRectGetWidth(_imageView.frame)-10, 3);
        if (num == (int)(CGRectGetHeight(_imageView.frame)-10)) {
            upOrDown = YES;
        }
    }
    else
    {
        num --;
        _line.frame = CGRectMake(CGRectGetMinX(_imageView.frame)+5, CGRectGetMinY(_imageView.frame)+5+num, CGRectGetWidth(_imageView.frame)-10, 3);
        if (num == 0) {
            upOrDown = NO;
        }
    }
}
//暫定掃描
- (void)stopScan
{
    //彈出提示框后,關(guān)閉掃描
    [self.session stopRunning];
    //彈出alert,關(guān)閉定時(shí)器
    [_timer setFireDate:[NSDate distantFuture]];
    //隱藏掃描線
    _line.hidden = YES;
}
- (void)starScan
{
    //開始掃描
    [self.session startRunning];
    //打開定時(shí)器
    [_timer setFireDate:[NSDate distantPast]];
    //顯示掃描線
    _line.hidden = NO;
}
  • 模糊界面的設(shè)置
- (void)setOverView {
    CGFloat width = CGRectGetWidth(self.view.frame);
    CGFloat height = CGRectGetHeight(self.view.frame);
    
    CGFloat x = CGRectGetMinX(_imageView.frame);
    CGFloat y = CGRectGetMinY(_imageView.frame);
    CGFloat w = CGRectGetWidth(_imageView.frame);
    CGFloat h = CGRectGetHeight(_imageView.frame);
    
    [self creatView:CGRectMake(0, 0, width, y)];
    [self creatView:CGRectMake(0, y, x, h)];
    [self creatView:CGRectMake(0, y + h, width, height - y - h)];
    [self creatView:CGRectMake(x + w, y, width - x - w, h)];
}

- (void)creatView:(CGRect)rect {
    CGFloat alpha = 0.5;
    UIColor *backColor = [UIColor blueColor];
    UIView *view = [[UIView alloc] initWithFrame:rect];
    view.backgroundColor = backColor;
    view.alpha = alpha;
    [self.view addSubview:view];
}
  • 閃光燈當(dāng)然也不可少了——夜空中最亮的星
- (void)systemLightSwitch:(BOOL)open
{
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([device hasTorch]) {
        [device lockForConfiguration:nil];
        if (open) {
            [device setTorchMode:AVCaptureTorchModeOn];
        } else {
            [device setTorchMode:AVCaptureTorchModeOff];
        }
        [device unlockForConfiguration];
    }
}

這個(gè)當(dāng)然是通過(guò)按鈕控制的了,可自行添加按鈕,只需要每次記錄點(diǎn)擊時(shí)候的BOOL值。

Demo點(diǎn)這里

最后編輯于
?著作權(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)容