如何更准确的识别出相册的二维码

最近公司碰到一个需求:聊天禁止二维码。然后碰到微信的个人名片二维码有一张识别不出来(个人二维码有很多样式的)

识别二维码 

我是写的一个UIImage的类目实现

- (NSString *)qrimageToStr{ CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:nil]; UIImage *image = [self imageCompressForWidth:640]; NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:image.CGImage]];

    __block NSString *message = nil;

    [features enumerateObjectsUsingBlock:^(CIFeature * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        if ([obj isKindOfClass:[CIQRCodeFeature class]]) {

            message = [(CIQRCodeFeature *)obj messageString];

            *stop=YES;

        }

    }];

    return message;

}

最开始发现不能识别二维码就以为是识别精度问题,然后就设置了options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}

发现还是识别不出来,于是想到下压缩下图片试试,没想到一试就ok了。

压缩图片

- (UIImage *)imageCompressForWidth:(CGFloat)defineWidth

{

    CGSize imageSize = self.size;

    CGFloat width = imageSize.width;

    CGFloat height = imageSize.height;

    CGFloat targetWidth = defineWidth;

    NSInteger targetHeight = (targetWidth / width) * height;

    UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));

    [self drawInRect:CGRectMake(0,0,targetWidth,  targetHeight)];

    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;

}

识别二维码时,压缩图片会提高精度和效率。

当二维码识别不出来时可以试试压缩图片

你可能感兴趣的:(如何更准确的识别出相册的二维码)