美文网首页
利用iOS系统方法实现相册二维码识别功能

利用iOS系统方法实现相册二维码识别功能

作者: 远航Yang | 来源:发表于2017-10-25 17:57 被阅读0次

    选择需要识别带有二维码的图片,利用系统CIQRCodeFeature*feature = [featuresobjectAtIndex:0];

    NSString*scannedResult = feature.messageString;能快速识别出图片中二维码的信息。

    主要实现方法:

    #pragma mark - 相册

    - (void)alumbBtnEvent {

    self.detector= [CIDetectordetectorOfType:CIDetectorTypeQRCodecontext:niloptions:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];

    if(![UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {//判断设备是否支持相册

    if(IOS8) {

    UIAlertController*alert = [UIAlertControlleralertControllerWithTitle:@"提示"message:@"未开启访问相册权限,请在设置->隐私->照片中进行设置!"preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction*cancel = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction*_Nonnullaction) {

    }];

    UIAlertAction*confirm = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {

    }];

    [alert addAction:cancel];

    [alert addAction:confirm];

    [self presentViewController:alertanimated:YEScompletion:nil];

    }

    else{

    UIAlertController*alert = [UIAlertControlleralertControllerWithTitle:@"提示"message:@"您的系统暂不支持此功能!"preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction*confirm = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnullaction) {

    }];

    [alert addAction:confirm];

    [self presentViewController:alertanimated:YEScompletion:nil];

    }

    return;

    }

    UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];

    mediaUI.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;

    mediaUI.mediaTypes= [UIImagePickerControlleravailableMediaTypesForSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];

    mediaUI.allowsEditing=NO;

    mediaUI.delegate=self;

    [selfpresentViewController:mediaUIanimated:YEScompletion:^{

    [[UIApplicationsharedApplication]setStatusBarStyle:UIStatusBarStyleDefaultanimated:YES];

    }];

    }

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

    UIImage*image = [infoobjectForKey:UIImagePickerControllerEditedImage];

    if(!image){

    image = [infoobjectForKey:UIImagePickerControllerOriginalImage];

    }

    NSArray*features = [self.detectorfeaturesInImage:[CIImageimageWithCGImage:image.CGImage]];

    if(features.count>=1) {

    [picker dismissViewControllerAnimated:YEScompletion:^{

    [[UIApplicationsharedApplication]setStatusBarStyle:UIStatusBarStyleLightContentanimated:YES];

    CIQRCodeFeature*feature = [featuresobjectAtIndex:0];

    NSString*scannedResult = feature.messageString;

    //播放扫描二维码的声音

    SystemSoundIDsoundID;

    //AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

    NSString*strSoundFile = [[NSBundlemainBundle]pathForResource:@"qrcode_found"ofType:@"wav"];

    AudioServicesCreateSystemSoundID((__bridgeCFURLRef)[NSURLfileURLWithPath:strSoundFile],&soundID);

    AudioServicesPlaySystemSound(soundID);

    [selfaccordingQcode:scannedResult];

    }];

    }

    else{

    [pickerdismissViewControllerAnimated:YEScompletion:^{

    [[UIApplicationsharedApplication]setStatusBarStyle:UIStatusBarStyleLightContentanimated:YES];

    UIAlertController*alert = [UIAlertControlleralertControllerWithTitle:@"提示"message:@"该图片没有包含一个二维码!"preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction*confirm = [UIAlertActionactionWithTitle:@"知道了"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {

    }];

    [alert addAction:confirm];

    [self presentViewController:alertanimated:YEScompletion:nil];

    }];

    }

    }

    - (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker {

    [pickerdismissViewControllerAnimated:YEScompletion:^{

    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContentanimated:YES];

    }];

    }

    相关文章

      网友评论

          本文标题:利用iOS系统方法实现相册二维码识别功能

          本文链接:https://www.haomeiwen.com/subject/deqcpxtx.html