使用系统框架识别相册中的二维码

在UIImagePickerController的代理方法中实现

//调用方法对图片的大小做处理

    UIImage *image = [self imageSizeWithScreenImage:info[UIImagePickerControllerOriginalImage]];


 //原生识别相册图片中的二维码

        CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy: CIDetectorAccuracyHigh}];

        // 取得识别结果

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

        if(features.count==0) {

               [self.viewmakeToast:@"未發現QR Code"];

            [self startCapture];

        }else{

            NSString*resultStr;

            for(intindex =0; index < [featurescount]; index ++) {

                CIQRCodeFeature*feature = [featuresobjectAtIndex:index];

               resultStr = feature.messageString;

            }

            if(resultStr) {

        //对识别的结果进行处理                                                                                                                                          [self handleResultString:resultStr];

            _iscloseScan=YES;

            }else{

             [self.viewmakeToast:@"无法识别QR Code"];

            [selfstartCapture];

            }

        }


私有方法对图片的size进行处理

- (UIImage*)imageSizeWithScreenImage:(UIImage*)image {

    CGFloatimageWidth = image.size.width;

    CGFloatimageHeight = image.size.height;

    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;

    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;

   if(imageWidth <= screenWidth && imageHeight <= screenHeight) {

        returnimage;

    }

  CGFloatmax =MAX(imageWidth, imageHeight);

    CGFloatscale = max / (screenHeight *2.0);

 CGSizesize =CGSizeMake(imageWidth / scale, imageHeight / scale);

    UIGraphicsBeginImageContext(size);

    [imagedrawInRect:CGRectMake(0,0, size.width, size.height)];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;

}

你可能感兴趣的:(使用系统框架识别相册中的二维码)