// 点击“相册”按钮
-(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.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];
}
}