ZXingObjc 扫描相册二维码

// 点击“相册”按钮

-(void)openAlbum

{

  //打开之前最好判断应用是否拥有打开系统“照片”的权限,同理,扫描之前也最好要判断,此处不赘述

        if (!_picker) {

            _picker = [[UIImagePickerController alloc] init];

            _picker.delegate = self;

            _picker.allowsEditing = NO;   // 设置选中相册的图片后,不需要编辑(截取部分),直接解析图片

            _picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        }

        [self presentViewController:_picker animated:YES completion:nil];

}

#pragma -mark 选中相册中的图片

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

  // toast 提示“正在处理”

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.parentViewController.view animated:YES];

    hud.mode = MBProgressHUDModeIndeterminate;

    hud.labelText = @"正在处理";

    hud.margin = 15.f;

    hud.yOffset = 15.f;

    hud.removeFromSuperViewOnHide = YES;

    self.hudView = hud;

    

    //可选经过编辑还是未经过编辑,此处设置选择图片是不经过编辑的模式

    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    

    // 相册图片对应尺寸需要按比例缩放到屏幕匹配的大小,保证解析的正确率

    CGFloat newWidth = image.size.width;

    CGFloat newHeight = image.size.height;

    if (image.size.height/image.size.width>self.view.bounds.size.height/self.view.bounds.size.width) {

        newHeight = newHeight>self.view.bounds.size.height?self.view.bounds.size.height:newHeight;

        newWidth = newHeight * image.size.width/image.size.height;

    }else{

        newWidth = newWidth>self.view.bounds.size.width?self.view.bounds.size.width:newWidth;

        newHeight = newWidth * image.size.height/image.size.width;

    }

    CGSize newSize= CGSizeMake(newWidth, newHeight);

    UIGraphicsBeginImageContext(newSize);

    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    [self dismissViewControllerAnimated:YES completion:^{

        [self getInfoWithImage:newImage];

    }];

}

 

#pragma -mark 照片处理

-(void)getInfoWithImage:(UIImage *)img{

    UIImage *loadImage= img;

    CGImageRef imageToDecode = loadImage.CGImage;

    

    ZXLuminanceSource *source = [[ZXCGImageLuminanceSource alloc] initWithCGImage:imageToDecode];

    ZXBinaryBitmap *bitmap = [ZXBinaryBitmap binaryBitmapWithBinarizer:[ZXHybridBinarizer binarizerWithSource:source]];

    

    NSError *error = nil;

    

    ZXDecodeHints *hints = [ZXDecodeHints hints];

    

    ZXMultiFormatReader *reader = [ZXMultiFormatReader reader];

    ZXResult *result = [reader decode:bitmap

                                hints:hints

                                error:&error];

    if (result) {

        [self.hudView hide:YES];

        if(!isDisplaingResult){

            [MobClick event:Event_QRCodeScanSuccess];

            dispatch_async(dispatch_get_main_queue(), ^{

                NSString *resultString = [result.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

                isDisplaingResult = YES;

                

                [MobClick event:Event_scanPhotoInAlbumSucess];

                

                //相册选取图片,根据扫描结果直接打开页面

                if (self.scanType!= RCScanQR||[resultString isURL]||[resultString isBarCode]) {

                    [self openResult:resultString];

                }else{

                    [self displayForResult:resultString type:self.scanType];

                }

            });

        }

    }else{

        [self.hudView hide:YES];

        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];

        hud.mode = MBProgressHUDModeText;

        hud.userInteractionEnabled = NO;

        hud.labelText = @"解析失败";

        hud.margin = 15.f;

        hud.yOffset = 15.0f;

        hud.removeFromSuperViewOnHide = YES;

        [hud hide:YES afterDelay:1];

    }

}

 

转载于:https://www.cnblogs.com/Jacue/p/4826139.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值