iOS相机和相册扫码

调用相机扫码

#import <AVFoundation/AVFoundation.h>

@interface QRScanViewController ()<AVCaptureMetadataOutputObjectsDelegate> {
    
}

@property (nonatomic, strong) AVCaptureSession *mSession;

@end

@implementation QRScanViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //获取摄像设备
    AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //创建输入流
    AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    if (!input) return;
    //创建输出流
    AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init];
    //设置代理 在主线程里刷新
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    //设置有效扫描区域
    CGRect scanCrop=[self getScanCrop:_scanWindow.bounds readerViewBounds:CGRectMake(0, 0, kScreenWidth, kScreenHeight-65)];
    output.rectOfInterest = scanCrop;
    //初始化链接对象
    _mSession = [[AVCaptureSession alloc]init];
    //高质量采集率
    [_mSession setSessionPreset:AVCaptureSessionPresetHigh];
    
    [_mSession addInput:input];
    [_mSession addOutput:output];
    //设置扫码支持的编码格式(如下设置条形码和二维码兼容)
    output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];
    
    AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:_mSession];
    layer.videoGravity=AVLayerVideoGravityResizeAspectFill;
    layer.frame=self.view.layer.bounds;
    [self.view.layer insertSublayer:layer atIndex:0];
    //开始捕获
    [_mSession startRunning];
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    if (metadataObjects.count>0) {
        //[session stopRunning];
        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];
        //输出扫描字符串
        NSLog(@"%@",metadataObject.stringValue);
        [self qrcodeDidReadResult:metadataObject.stringValue];
    }
}
//获取扫描结果
- (void)qrcodeDidReadResult:(NSString *)string {
    //停止扫描
    if ([string hasPrefix:@"http://"] || [string hasPrefix:@"https://"]) {
        [self showWebView:string];
        [self.mSession stopRunning];
    }
    else {
        NSLog(@"%@", string);
    }
}

扫描相册图片二维码

@interface QRScanViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
- (void)showAlbumView {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.delegate = self;
    [self presentViewController:imagePicker animated:YES completion:nil];
}
// UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    
    UIImage *pickedImage = info[UIImagePickerControllerEditedImage] ?: info[UIImagePickerControllerOriginalImage];
    CIImage *detectImage = [CIImage imageWithData:UIImagePNGRepresentation(pickedImage)];
    
    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy: CIDetectorAccuracyLow}];
    CIQRCodeFeature *feature = (CIQRCodeFeature *)[detector featuresInImage:detectImage options:nil].firstObject;
    
    [picker dismissViewControllerAnimated:YES completion:^{
        if (feature.messageString) {
            [self qrcodeDidReadResult:feature.messageString];
        }
    }];
}

猜你喜欢

转载自blog.csdn.net/watson2017/article/details/120751183