ios 原生sdk 识别图片中的二维码

时间:2022-11-19 07:47:32


UIImageView*tempImageView=(UIImageView*)gesture.view;

        if(tempImageView.image){

           //1. 初始化扫描仪,设置设别类型和识别质量

            CIDetector*detector = [CIDetectordetectorOfType:CIDetectorTypeQRCodecontext:niloptions:@{CIDetectorAccuracy : CIDetectorAccuracyHigh}];

            //2. 扫描获取的特征组

            NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:tempImageView.image.CGImage]];

            //3. 获取扫描结果

            CIQRCodeFeature *feature = [featuresobjectAtIndex:0];

            NSString *scannedResult = feature.messageString;

            UIAlertView * alertView = [[UIAlertViewalloc]initWithTitle:@"扫描结果"message:scannedResult delegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];

            [alertView show];

        }else {

            

            UIAlertView * alertView = [[UIAlertViewalloc]initWithTitle:@"扫描结果"message:@"您还没有生成二维码"delegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];

            [alertView show];

        }